Skip to content

Commit

Permalink
feat: payping payment methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mimani68 committed Feb 2, 2023
1 parent 655f969 commit 575c2c9
Show file tree
Hide file tree
Showing 10 changed files with 4,764 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/
.idea
.idea
.vscode
41 changes: 41 additions & 0 deletions _example/payping/payping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"context"
"fmt"
"log"

"github.com/GoFarsi/paygap/client"
"github.com/GoFarsi/paygap/providers/payping"
)

func main() {
p, err := payping.New(client.New(), "YOUR_API_KEY")
if err != nil {
log.Fatal(err)
}

request := &payping.PaymentRequest{
Amount: 11000,
PayerIdentity: "124500",
PayerName: "Ali Hesami",
ClientRefId: "example-arbitary-code",
ReturnUrl: "http://example.com/callback",
Description: "desc test",
}
resp, err := p.RequestPayment(context.Background(), request)

if err != nil {
log.Fatal(err)
}

fmt.Println(resp)

verify := &payping.VerifyRequest{
Amount: request.Amount,
RefId: resp.Code,
}
verifyResp, err := p.VerifyPayment(context.Background(), verify)

fmt.Println(verifyResp)
}
7 changes: 6 additions & 1 deletion providers/payping/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package payping
import (
"context"
"errors"
"fmt"
"net/http"
"reflect"

Expand All @@ -17,8 +18,12 @@ func request[RQ any, RS any](ctx context.Context, payping *Payping, req RQ, base
return response, errors.New("response type is invalid")
}

if payping.apiToken == "" || len(payping.apiToken) < 10 {
return response, errors.New("jwt token is invalid")
}

headers := make(map[string]string)
headers["X-API-KEY"] = payping.apiKey
headers["Authorization"] = fmt.Sprintf("Bearer %s", payping.apiToken)
headers["Content-Type"] = "application/json"

// TODO: can review if SANDBOX was available
Expand Down
4 changes: 2 additions & 2 deletions providers/payping/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type PaymentResponse struct {
}

type VerifyRequest struct {
RefId uint `json:"refId" validate:"required"`
Amount string `json:"Amount" validate:"required,min=100,max=50000000"`
RefId string `json:"refId" validate:"required"`
Amount int32 `json:"Amount" validate:"required,min=100,max=50000000"`
}

type VerifyResponse struct {
Expand Down
39 changes: 27 additions & 12 deletions providers/payping/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,59 @@ import (

// refrence: https://docs.payping.ir/#operation/CreateSinglePayment
func (p *Payping) RequestPayment(ctx context.Context, req *PaymentRequest) (*PaymentResponse, error) {
// return &PaymentResponse{}, nil
if err := p.client.GetValidator().Struct(req); err != nil {
return nil, status.New(0, http.StatusBadRequest, codes.InvalidArgument, err.Error())
}

gatewayReq := new(Request)
gatewayReq.API = p.apiKey
gatewayReq.PaymentRequest = req

return request[*PaymentRequest, *PaymentResponse](ctx, p, gatewayReq, p.paymentEndpoint)
return request[*PaymentRequest, *PaymentResponse](ctx, p, req, p.baseUrl, p.paymentEndpoint, nil)
}

// refrence: https://docs.payping.ir/#operation/VerifyPayment
func (p *Payping) VerifyPayment(ctx context.Context, req *VerifyRequest) (*VerifyResponse, error) {
return &VerifyResponse{}, nil
if err := p.client.GetValidator().Struct(req); err != nil {
return nil, status.New(0, http.StatusBadRequest, codes.InvalidArgument, err.Error())
}
return request[*VerifyRequest, *VerifyResponse](ctx, p, req, p.baseUrl, p.paymentEndpoint, nil)
}

// refrence: https://docs.payping.ir/#operation/CreateMultiPayment
func (p *Payping) RequestSharePayment(ctx context.Context, req *SharePaymentRequest) (*PaymentResponse, error) {
return &PaymentResponse{}, nil
if err := p.client.GetValidator().Struct(req); err != nil {
return nil, status.New(0, http.StatusBadRequest, codes.InvalidArgument, err.Error())
}
return request[*SharePaymentRequest, *PaymentResponse](ctx, p, req, p.baseUrl, p.paymentEndpoint, nil)
}

// refrence: https://docs.payping.ir/#operation/CreateBlockPayment
func (p *Payping) RequestBlockingPayment(ctx context.Context, req *BlockedPaymentRequest) (*PaymentResponse, error) {
return &PaymentResponse{}, nil
if err := p.client.GetValidator().Struct(req); err != nil {
return nil, status.New(0, http.StatusBadRequest, codes.InvalidArgument, err.Error())
}
return request[*BlockedPaymentRequest, *PaymentResponse](ctx, p, req, p.baseUrl, p.paymentEndpoint, nil)
}

// refrence: https://docs.payping.ir/#operation/UnBlockPayment
func (p *Payping) ReleasingBlockedPayment(ctx context.Context, req *ReleasingBlockedPaymentRequest) error {
return nil
if err := p.client.GetValidator().Struct(req); err != nil {
return status.New(0, http.StatusBadRequest, codes.InvalidArgument, err.Error())
}
_, err := request[*ReleasingBlockedPaymentRequest, error](ctx, p, req, p.baseUrl, p.paymentEndpoint, nil)
return err
}

// refrence: https://docs.payping.ir/#operation/CreateIdPosPayment
func (p *Payping) PaymentWithTracingId(ctx context.Context, req *PaymentWithTracerIdRequest) (*PaymentWithTracerIdResponse, error) {
return &PaymentWithTracerIdResponse{}, nil
if err := p.client.GetValidator().Struct(req); err != nil {
return nil, status.New(0, http.StatusBadRequest, codes.InvalidArgument, err.Error())
}
return request[*PaymentWithTracerIdRequest, *PaymentWithTracerIdResponse](ctx, p, req, p.baseUrl, p.paymentEndpoint, nil)
}

// refrence: https://docs.payping.ir/#operation/CancelPayment
func (p *Payping) PaymentSuspending(ctx context.Context, req *PaymentSuspedingRequest) error {
return nil
if err := p.client.GetValidator().Struct(req); err != nil {
return status.New(0, http.StatusBadRequest, codes.InvalidArgument, err.Error())
}
_, err := request[*PaymentSuspedingRequest, error](ctx, p, req, p.baseUrl, p.paymentEndpoint, nil)
return err
}
40 changes: 26 additions & 14 deletions providers/payping/payping-factory.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,50 @@
package payping

import (
"errors"
"log"
"net/http"

"github.com/GoFarsi/paygap/client"
"github.com/GoFarsi/paygap/status"
"google.golang.org/grpc/codes"
)

const API_VERSION = "2"

const (
PAYPING_HOST = "https://api.payping.ir/v2/pay"
PAYPING_HOST = "https://api.payping.ir"
)

const (
PAYPING_REQUEST_API_ENDPOINT = "/pg/v4/payment/request.json"
PAYPING_VERIFY_API_ENDPOINT = "/pg/v4/payment/verify.json"
PAYPING_UNVERIFIED_TRANSACTION_API_ENDPOINT = "/pg/v4/payment/unVerified.json"
PAYPING_REQUEST_API_ENDPOINT = "/v2/pay"
PAYPING_VERIFY_API_ENDPOINT = "/v2/pay/verify"
PAYPING_MULTI_PAYMENT_API_ENDPOINT = "/v2/pay/multi"
PAYPING_BLOCK_MONEY_PAYMENT_API_ENDPOINT = "/v2/pay/BlockMoney"
PAYPING_UNBLOCK_MONEY_PAYMENT_API_ENDPOINT = "/v2/pay/UnBlockMoney"
PAYPING_POS_PAYMENT_API_ENDPOINT = "/v1/pos"
)

// New create payping provider object for user factory request methods
func New(client client.Transporter, merchantID string, sandbox bool) (*Payping, error) {
// New create payping provider object
func New(client client.Transporter, apiToken string) (*Payping, error) {
if client == nil {
return nil, status.ERR_CLIENT_IS_NIL
}

payping := &Payping{
client: client,
merchantID: merchantID,
baseUrl: PAYPING_HOST,
requestEndpoint: PAYPING_REQUEST_API_ENDPOINT,
verifyEndpoint: PAYPING_VERIFY_API_ENDPOINT,
unverifiedEndpoint: PAYPING_UNVERIFIED_TRANSACTION_API_ENDPOINT,
client: client,
apiToken: apiToken,

baseUrl: PAYPING_HOST,
paymentEndpoint: PAYPING_REQUEST_API_ENDPOINT,
verifyEndpoint: PAYPING_VERIFY_API_ENDPOINT,
multiplePaymentEndpoint: PAYPING_MULTI_PAYMENT_API_ENDPOINT,
blockMoneyPaymentEndpoint: PAYPING_BLOCK_MONEY_PAYMENT_API_ENDPOINT,
unBlockMoneyPaymentEndpoint: PAYPING_UNBLOCK_MONEY_PAYMENT_API_ENDPOINT,
posEndpoint: PAYPING_POS_PAYMENT_API_ENDPOINT,
}

if payping.apiToken == "" || len(payping.apiToken) < 10 {
log.Fatal("jwt token is invalid")
return nil, errors.New("jwt token is invalid")
}

if err := client.GetValidator().Struct(payping); err != nil {
Expand Down
16 changes: 10 additions & 6 deletions providers/payping/payping.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import (
)

type Payping struct {
client client.Transporter
merchantID string `validate:"required"`
client client.Transporter

baseUrl string
requestEndpoint string
verifyEndpoint string
unverifiedEndpoint string
baseUrl string
paymentEndpoint string
verifyEndpoint string
multiplePaymentEndpoint string
blockMoneyPaymentEndpoint string
unBlockMoneyPaymentEndpoint string
posEndpoint string

apiToken string
}
Loading

0 comments on commit 575c2c9

Please sign in to comment.