-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
218 additions
and
23 deletions.
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,39 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/GoFarsi/paygap/client" | ||
"github.com/GoFarsi/paygap/providers/pay" | ||
"log" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
|
||
p, err := pay.New(client.New(), "YOUR_API_KEY", true) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
resp, err := p.CreateTransaction(ctx, &pay.PaymentRequest{ | ||
Amount: 11000, | ||
Redirect: "http://example.com/callback", | ||
Mobile: "09151234567", | ||
FactorNumber: "xxxxx", | ||
Description: "desc test", | ||
ValidCardNumber: "1234123412341234", | ||
}) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println(resp) | ||
|
||
verifyResp, err := p.VerifyTransaction(ctx, &pay.VerifyRequest{ | ||
Token: resp.Token, | ||
}) | ||
|
||
fmt.Println(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
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
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,53 @@ | ||
package pay | ||
|
||
import "github.com/GoFarsi/paygap/client" | ||
|
||
type Pay struct { | ||
client client.Transporter | ||
apiKey string | ||
|
||
host string | ||
paymentEndpoint string | ||
verifyEndpoint string | ||
} | ||
|
||
type ErrorResponse struct { | ||
Status int `json:"status"` | ||
ErrorCode int `json:"errorCode"` | ||
ErrorMessage string `json:"errorMessage"` | ||
} | ||
|
||
type Request struct { | ||
API string `json:"api"` | ||
*PaymentRequest | ||
*VerifyRequest | ||
} | ||
|
||
type PaymentRequest struct { | ||
Amount uint `json:"amount" validate:"required,min=10000"` | ||
Redirect string `json:"redirect" validate:"required,url"` | ||
Mobile string `json:"mobile,omitempty" validate:"omitempty,min=11"` | ||
FactorNumber string `json:"factorNumber,omitempty"` | ||
Description string `json:"description,omitempty" validate:"omitempty,max=255"` | ||
ValidCardNumber string `json:"validCardNumber,omitempty" validate:"omitempty,max=16"` | ||
} | ||
|
||
type PaymentResponse struct { | ||
Status int `json:"status"` | ||
Token string `json:"token"` | ||
} | ||
|
||
type VerifyRequest struct { | ||
Token string `json:"token" validate:"required"` | ||
} | ||
|
||
type VerifyResponse struct { | ||
Status int `json:"status"` | ||
Amount string `json:"amount"` | ||
TransId string `json:"transId"` | ||
FactorNumber string `json:"factorNumber"` | ||
Mobile string `json:"mobile"` | ||
Description string `json:"description"` | ||
CardNumber string `json:"cardNumber"` | ||
Message string `json:"message"` | ||
} |
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,95 @@ | ||
package pay | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/GoFarsi/paygap/client" | ||
"github.com/GoFarsi/paygap/status" | ||
"google.golang.org/grpc/codes" | ||
"net/http" | ||
"reflect" | ||
) | ||
|
||
const ( | ||
PAY_HOST = "https://pay.ir" | ||
|
||
PAYMENT_ENDPOINT = "/pg/send" | ||
VERIFY_ENDPOINT = "/pg/verify" | ||
) | ||
|
||
func New(client client.Transporter, apiKey string, sandbox bool) (*Pay, error) { | ||
if client == nil { | ||
return nil, status.ERR_CLIENT_IS_NIL | ||
} | ||
|
||
pay := new(Pay) | ||
|
||
pay.client = client | ||
pay.host = PAY_HOST | ||
pay.apiKey = apiKey | ||
pay.paymentEndpoint = PAYMENT_ENDPOINT | ||
pay.verifyEndpoint = VERIFY_ENDPOINT | ||
|
||
if sandbox { | ||
pay.apiKey = "test" | ||
} | ||
|
||
if err := client.GetValidator().Struct(pay); err != nil { | ||
return nil, status.New(0, http.StatusBadRequest, codes.InvalidArgument, err.Error()) | ||
} | ||
|
||
return pay, nil | ||
} | ||
|
||
func (p *Pay) CreateTransaction(ctx context.Context, req *PaymentRequest) (*PaymentResponse, error) { | ||
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[*Request, *PaymentResponse](ctx, p, gatewayReq, p.paymentEndpoint) | ||
} | ||
|
||
func (p *Pay) VerifyTransaction(ctx context.Context, req *VerifyRequest) (*VerifyResponse, error) { | ||
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.VerifyRequest = req | ||
|
||
return request[*Request, *VerifyResponse](ctx, p, gatewayReq, p.verifyEndpoint) | ||
} | ||
|
||
func request[RQ any, RS any](ctx context.Context, p *Pay, req RQ, endpoint string) (response RS, err error) { | ||
r, ok := reflect.New(reflect.TypeOf(response).Elem()).Interface().(RS) | ||
if !ok { | ||
return response, errors.New("response type is invalid") | ||
} | ||
|
||
errResp := new(ErrorResponse) | ||
headers := make(map[string]string) | ||
headers["Content-Type"] = "application/json" | ||
|
||
resp, err := p.client.Post(ctx, &client.APIConfig{Host: p.host, Path: endpoint}, headers, req) | ||
if err != nil { | ||
return response, status.New(0, http.StatusInternalServerError, codes.Internal, err.Error()) | ||
} | ||
|
||
if resp.GetHttpResponse().StatusCode != http.StatusOK { | ||
if err := resp.GetJSON(errResp); err != nil { | ||
return response, status.New(0, http.StatusInternalServerError, codes.Internal, err.Error()) | ||
} | ||
return response, status.New(errResp.ErrorCode, http.StatusFailedDependency, codes.OK, errResp.ErrorMessage) | ||
} | ||
|
||
if err := resp.GetJSON(r); err != nil { | ||
return response, status.New(0, http.StatusInternalServerError, codes.Internal, err.Error()) | ||
} | ||
|
||
return r, nil | ||
} |
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