-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Mahdiazadbar/feat/add-vandar-payment-gateway
feat/add-vandar-payment-gateway
- Loading branch information
Showing
4 changed files
with
254 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/GoFarsi/paygap/client" | ||
"github.com/GoFarsi/paygap/providers/vandar" | ||
) | ||
|
||
func main() { | ||
c := client.New() | ||
v, err := vandar.New(c, "4924fc5aeca14bfdaa0d44162baf4fb7ee19d706") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
resp, err := v.RequestPayment(context.Background(), 10000, "http://vandagateway.local:8081/callback", | ||
"09353917307", "", "", "", "") | ||
if err != nil { | ||
log.Printf("%+v\n", err) | ||
} | ||
|
||
//make a break point and Go to payment page :=https://ipg.vandar.io/v3/{token} and token is resp.Token | ||
|
||
//get transaction detail | ||
detailResp, detailErr := v.TransactionDetail(context.Background(), resp.Token) | ||
if detailErr != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
log.Printf("%+v\n", detailResp) | ||
|
||
//and verify Transction | ||
verifyResp, verifyErr := v.TransactionDetail(context.Background(), resp.Token) | ||
if verifyErr != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
log.Printf("%+v\n", verifyResp) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package vandar | ||
|
||
import "github.com/GoFarsi/paygap/client" | ||
|
||
type Vandar struct { | ||
client client.Transporter | ||
APIKey string `validate:"required"` | ||
|
||
baseUrl string | ||
requestEndpoint string | ||
redirectEndpoint string | ||
transactionDetailEndpoint string | ||
verifyEndpoint string | ||
} | ||
|
||
type paymentRequest struct { | ||
ApiKey string `json:"api_key"` | ||
Amount int `json:"amount" validate:"required,min=1000"` | ||
CallBackURL string `json:"callback_url" validate:"required,url"` | ||
MobileNumber string `json:"mobile_number"` | ||
FactorNumber string `json:"factorNumber"` | ||
Description string `json:"description"` | ||
NationalCode string `json:"national_code"` | ||
ValidCardNumber string `json:"valid_card_number"` | ||
} | ||
|
||
type PaymentResponse struct { | ||
Status int `json:"status"` | ||
Token string `json:"token"` | ||
Errors []string `json:"errors"` | ||
} | ||
|
||
type verifyRequest struct { | ||
APIKey string `json:"api_key"` | ||
Token string `json:"token"` | ||
} | ||
|
||
type VerifyResponse struct { | ||
Status int `json:"status"` | ||
Errors []string `json:"errors"` | ||
Amount string `json:"amount"` | ||
RealAmount int `json:"realAmount"` | ||
Wage string `json:"wage"` | ||
TransID int64 `json:"transId"` | ||
FactorNumber string `json:"factorNumber"` | ||
Mobile string `json:"mobile"` | ||
Description string `json:"description"` | ||
CardNumber string `json:"cardNumber"` | ||
PaymentDate string `json:"paymentDate"` | ||
Cid string `json:"cid"` | ||
Message string `json:"message"` | ||
} | ||
|
||
type transactionDetailRequest struct { | ||
APIKey string `json:"api_key"` | ||
Token string `json:"token"` | ||
} | ||
|
||
type TransactionDetailResponse struct { | ||
Status *int `json:"status"` | ||
Amount *string `json:"amount"` | ||
TransId *int `json:"transId"` | ||
RefNumber *string `json:"refnumber"` | ||
TrackingCode *string `json:"trackingCode"` | ||
FactorNumber *string `json:"factorNumber"` | ||
Mobile *string `json:"mobile"` | ||
Description *string `json:"description"` | ||
CardNumber *string `json:"cardNumber"` | ||
CID *string `json:"CID"` | ||
CreatedAt *string `json:"createdAt"` | ||
PaymentDate *string `json:"paymentDate"` | ||
Code *int `json:"code"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package vandar | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/GoFarsi/paygap/client" | ||
"github.com/GoFarsi/paygap/status" | ||
"google.golang.org/grpc/codes" | ||
) | ||
|
||
const API_VERSION = "3" | ||
|
||
const ( | ||
VANDAR_HOST = "https://ipg.vandar.io/api" | ||
) | ||
|
||
const ( | ||
VANDAR_REQUEST_API_ENDPOINT = "/v3/send" | ||
VANDAR_REDIRECT_API_ENDPOINT = "/v3/" | ||
VANDAR_VERIFY_API_ENDPOINT = "/v3/verify" | ||
VANDAR_TRANSACTION_DETAIL_API_END_POINT = "/v3/transaction" | ||
) | ||
|
||
// New create vandar provider object for user factory request methods | ||
func New(client client.Transporter, ApiKey string) (*Vandar, error) { | ||
if client == nil { | ||
return nil, status.ERR_CLIENT_IS_NIL | ||
} | ||
|
||
vandar := &Vandar{ | ||
client: client, | ||
APIKey: ApiKey, | ||
baseUrl: VANDAR_HOST, | ||
requestEndpoint: VANDAR_REQUEST_API_ENDPOINT, | ||
redirectEndpoint: VANDAR_REDIRECT_API_ENDPOINT, | ||
verifyEndpoint: VANDAR_VERIFY_API_ENDPOINT, | ||
transactionDetailEndpoint: VANDAR_TRANSACTION_DETAIL_API_END_POINT, | ||
} | ||
|
||
if err := client.GetValidator().Struct(vandar); err != nil { | ||
return nil, status.New(0, http.StatusBadRequest, codes.InvalidArgument, err.Error()) | ||
} | ||
|
||
return vandar, nil | ||
} | ||
|
||
// RequestPayment create payment request and return status code and authority | ||
// document of field https://vandarpay.github.io/docs/ipg/#step-1 | ||
func (v *Vandar) RequestPayment(ctx context.Context, amount int, callBackUrl, mobileNumber, factorNumber, nationalCode, validCardNumber, description string) (*PaymentResponse, error) { | ||
req := paymentRequest{ | ||
ApiKey: v.APIKey, | ||
Amount: amount, | ||
CallBackURL: callBackUrl, | ||
MobileNumber: mobileNumber, | ||
FactorNumber: factorNumber, | ||
NationalCode: nationalCode, | ||
ValidCardNumber: validCardNumber, | ||
Description: description, | ||
} | ||
|
||
if err := v.client.GetValidator().Struct(req); err != nil { | ||
return nil, status.New(0, http.StatusBadRequest, codes.InvalidArgument, err.Error()) | ||
} | ||
|
||
headers := make(map[string]string) | ||
headers["Content-Type"] = "application/json" | ||
|
||
response := &PaymentResponse{} | ||
resp, err := v.client.Post(ctx, &client.APIConfig{Host: v.baseUrl, Path: v.requestEndpoint, Headers: headers}, req) | ||
if err != nil { | ||
return nil, status.New(0, http.StatusInternalServerError, codes.Internal, err.Error()) | ||
} | ||
|
||
if err := resp.GetJSON(response); err != nil { | ||
return nil, status.New(0, http.StatusInternalServerError, codes.Internal, err.Error()) | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
// VerifyPayment transaction by merchant id, amount and authority to payment provider | ||
// doc https://vandarpay.github.io/docs/ipg/#step-4 | ||
func (v *Vandar) VerifyPayment(ctx context.Context, token string) (*VerifyResponse, error) { | ||
req := &verifyRequest{ | ||
APIKey: v.APIKey, | ||
Token: token, | ||
} | ||
|
||
if err := v.client.GetValidator().Struct(req); err != nil { | ||
return nil, status.New(0, http.StatusBadRequest, codes.InvalidArgument, err.Error()) | ||
} | ||
|
||
headers := make(map[string]string) | ||
headers["Content-Type"] = "application/json" | ||
|
||
response := &VerifyResponse{} | ||
resp, err := v.client.Post(ctx, &client.APIConfig{Host: v.baseUrl, Path: v.verifyEndpoint, Headers: headers}, req) | ||
if err != nil { | ||
return nil, status.New(0, http.StatusInternalServerError, codes.Internal, err.Error()) | ||
} | ||
|
||
if err := resp.GetJSON(response); err != nil { | ||
return nil, status.New(0, http.StatusInternalServerError, codes.Internal, err.Error()) | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
// TransactionDetail Transaction Detail get Transaction status | ||
// doc https://vandarpay.github.io/docs/ipg/#step-3 | ||
func (v *Vandar) TransactionDetail(ctx context.Context, token string) (*TransactionDetailResponse, error) { | ||
req := &transactionDetailRequest{ | ||
APIKey: v.APIKey, | ||
Token: token, | ||
} | ||
|
||
if err := v.client.GetValidator().Struct(req); err != nil { | ||
return nil, status.New(0, http.StatusBadRequest, codes.InvalidArgument, err.Error()) | ||
} | ||
|
||
headers := make(map[string]string) | ||
headers["Content-Type"] = "application/json" | ||
|
||
response := &TransactionDetailResponse{} | ||
resp, err := v.client.Post(ctx, &client.APIConfig{Host: v.baseUrl, Path: v.transactionDetailEndpoint, Headers: headers}, req) | ||
if err != nil { | ||
return nil, status.New(0, http.StatusInternalServerError, codes.Internal, err.Error()) | ||
} | ||
|
||
if err := resp.GetJSON(response); err != nil { | ||
return nil, status.New(0, http.StatusInternalServerError, codes.Internal, err.Error()) | ||
} | ||
|
||
return response, nil | ||
} |