From e454a85a13c2ea36b630798afe48e884134a85a5 Mon Sep 17 00:00:00 2001 From: gentildpinto Date: Fri, 21 Apr 2023 16:01:05 +0100 Subject: [PATCH] remove environments needs and perform some updates --- README.md | 24 +++------- vpos.go | 125 +++++++++++++++++++++------------------------------ vpos_test.go | 3 +- 3 files changed, 60 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index a8279cd..7a598db 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,12 @@ interacting with the API using this package: | `MERCHANT_VPOS_TOKEN` | The API token provided by vPOS | true | | `PAYMENT_CALLBACK_URL` | The URL that will handle payment notifications | false | | `REFUND_CALLBACK_URL` | The URL that will handle refund notifications | false | -| `ENVIRONMENT` | Your application environment, set "production" for production or "development" for sandbox/tests environment | true | Given you have set up all the environment variables above with the correct information, you will now be able to authenticate and communicate effectively with vPOS API Payment Gateway using this package. +**Note:** During development, make sure to use a token created for the sandbox environment + The next section will show the various payment actions that can be performed by you, the merchant. ### How to instantiate vPOS @@ -36,7 +37,6 @@ To create an instance of a vPOS merchant see argument table and a simple example | `supervisorCard` | Merchant Supervisor Card number provided by EMIS | `string` | `paymentCallbackURL` | Merchant application endpoint to accept the callback payment response | `string` | `refundCallbackURL` | Merchant application endpoint to accept the callback refund response | `string` -| `environment` | Your application environment, is either "production" or "development" | `string` #### Example ```go @@ -54,7 +54,6 @@ var ( paymentCallbackURL = os.Getenv("PAYMENT_CALLBACK_URL") refundCallbackURL = os.Getenv("REFUND_CALLBACK_URL") supervisorCard = os.Getenv("GPO_SUPERVISOR_CARD") - environment = os.Getenv("ENVIRONMENT") ) func main() { @@ -63,8 +62,7 @@ func main() { token, paymentCallbackURL, refundCallbackURL, - supervisorCard, - environment + supervisorCard ) } ``` @@ -88,7 +86,6 @@ var ( paymentCallbackURL = os.Getenv("PAYMENT_CALLBACK_URL") refundCallbackURL = os.Getenv("REFUND_CALLBACK_URL") supervisorCard = os.Getenv("GPO_SUPERVISOR_CARD") - environment = os.Getenv("ENVIRONMENT") ) func main() { @@ -97,8 +94,7 @@ func main() { token, paymentCallbackURL, refundCallbackURL, - supervisorCard, - environment + supervisorCard ) transactionType := "payment" // or "authorization" @@ -137,7 +133,6 @@ var ( paymentCallbackURL = os.Getenv("PAYMENT_CALLBACK_URL") refundCallbackURL = os.Getenv("REFUND_CALLBACK_URL") supervisorCard = os.Getenv("GPO_SUPERVISOR_CARD") - environment = os.Getenv("ENVIRONMENT") ) func main() { @@ -146,8 +141,7 @@ func main() { token, paymentCallbackURL, refundCallbackURL, - supervisorCard, - environment + supervisorCard ) parentTransactionID := "9kOmKYUWxN0Jpe4PBoXzE" @@ -184,7 +178,6 @@ var ( paymentCallbackURL = os.Getenv("PAYMENT_CALLBACK_URL") refundCallbackURL = os.Getenv("REFUND_CALLBACK_URL") supervisorCard = os.Getenv("GPO_SUPERVISOR_CARD") - environment = os.Getenv("ENVIRONMENT") ) func main() { @@ -193,8 +186,7 @@ func main() { token, paymentCallbackURL, refundCallbackURL, - supervisorCard, - environment + supervisorCard ) transactionType := "refund" // or "cancelation" @@ -231,7 +223,6 @@ var ( paymentCallbackURL = os.Getenv("PAYMENT_CALLBACK_URL") refundCallbackURL = os.Getenv("REFUND_CALLBACK_URL") supervisorCard = os.Getenv("GPO_SUPERVISOR_CARD") - environment = os.Getenv("ENVIRONMENT") ) func main() { @@ -240,8 +231,7 @@ func main() { token, paymentCallbackURL, refundCallbackURL, - supervisorCard, - environment + supervisorCard ) transactionID := "1jHbXEbRTIbbwaoJ6w06nLcRG7X" diff --git a/vpos.go b/vpos.go index 8b32e5e..5d93898 100644 --- a/vpos.go +++ b/vpos.go @@ -10,43 +10,42 @@ import ( ) const ( - sandboxURL = "https://sandbox.vpos.ao/api/v1" - productionURL = "https://api.vpos.ao/api/v1" - paymentTransaction = "payment" - refundTransaction = "refund" - authorizationTransaction = "authorization" - cancelationTransaction = "cancelation" + baseURL = "https://vpos.ao/api/v1" + TypePayment TransactionType = "payment" + TypeRefund TransactionType = "refund" + TypeAuthorization TransactionType = "authorization" + TypeCancelation TransactionType = "cancelation" ) type ( - VPOS struct { + TransactionType string + VPOS struct { Token string //Your VPOS Token PosID int64 //Your GPO POS ID PaymentCallbackURL string //Your Payment Callback URL RefundCallbackURL string //Your Refund Callback URL SupervisorCard string //Your GPO Supervisor Card - Environment string //Your Environment, set "production" for production or "development" for sandbox environment } PaymentTransaction struct { - Type string `json:"type"` - PosID int64 `json:"pos_id"` - Mobile string `json:"mobile"` - Amount string `json:"amount"` - CallbackURL string `json:"callback_url"` + Type TransactionType `json:"type"` + PosID int64 `json:"pos_id"` + Mobile string `json:"mobile"` + Amount string `json:"amount"` + CallbackURL string `json:"callback_url"` } AuthorizedPaymentTransaction struct { - Type string `json:"type"` - ParentTransactionID string `json:"parent_transaction_id"` - Amount string `json:"amount"` - CallbackURL string `json:"callback_url"` + Type TransactionType `json:"type"` + ParentTransactionID string `json:"parent_transaction_id"` + Amount string `json:"amount"` + CallbackURL string `json:"callback_url"` } RefundTransaction struct { - Type string `json:"type"` - ParentTransactionID string `json:"parent_transaction_id"` - CallbackURL string `json:"call_back_url"` + Type TransactionType `json:"type"` + ParentTransactionID string `json:"parent_transaction_id"` + CallbackURL string `json:"call_back_url"` } TransactionStatus struct { @@ -55,32 +54,27 @@ type ( } Transaction struct { - ID string `json:"id"` - Amount string `json:"amount"` - ClearingPeriod string `json:"clearing_period"` - Mobile string `json:"mobile"` - ParentTransactionID string `json:"parent_transaction_id"` - PosID int64 `json:"pos_id"` - Status string `json:"status"` - StatusDatetime string `json:"status_datetime"` - StatusReason string `json:"status_reason"` - Type string `json:"type"` + ID string `json:"id"` + Amount string `json:"amount"` + ClearingPeriod string `json:"clearing_period"` + Mobile string `json:"mobile"` + ParentTransactionID string `json:"parent_transaction_id"` + PosID int64 `json:"pos_id"` + Status string `json:"status"` + StatusDatetime string `json:"status_datetime"` + StatusReason string `json:"status_reason"` + Type TransactionType `json:"type"` } ) -func NewVPOS(posID int64, token, paymentCallbackURL, refundCallbackURL, supervisorCard, environment string) (*VPOS, error) { - if !(environment == "production" || environment == "development") { - return &VPOS{}, errors.New("invalid environment") - } - +func NewVPOS(posID int64, token, paymentCallbackURL, refundCallbackURL, supervisorCard string) *VPOS { return &VPOS{ Token: token, PosID: posID, PaymentCallbackURL: paymentCallbackURL, RefundCallbackURL: refundCallbackURL, SupervisorCard: supervisorCard, - Environment: environment, - }, nil + } } func GetStatusReason(code int64) (reason string, err error) { @@ -91,11 +85,8 @@ func GetStatusReason(code int64) (reason string, err error) { return } -func (v *VPOS) TransactionRemainingTime(transactionID string) (result int64, err error) { - url := fmt.Sprintf("%s/requests/%s", sandboxURL, transactionID) - if v.Environment == "production" { - url = fmt.Sprintf("%s/requests/%s", productionURL, transactionID) - } +func (v *VPOS) GetRequest(transactionID string) (result int64, err error) { + url := fmt.Sprintf("%s/requests/%s", baseURL, transactionID) response, err := httpGet( url, @@ -120,19 +111,16 @@ func (v *VPOS) TransactionRemainingTime(transactionID string) (result int64, err return int64(floatResult), nil } -//transactionType is either 'payment' or 'authorization' -//if transactionType is 'payment', it will create a new payment transaction -//if transactionType is 'authorization', it will create a new authorization transaction -func (v *VPOS) PaymentTransaction(transactionType, mobile, amount string) (transactionID, idempotencyKey, nonce string, timeRemaining int64, err error) { - if !(transactionType == paymentTransaction || transactionType == authorizationTransaction) { +// transactionType is either 'payment' or 'authorization' +// if transactionType is 'payment', it will create a new payment transaction +// if transactionType is 'authorization', it will create a new authorization transaction +func (v *VPOS) PaymentTransaction(transactionType TransactionType, mobile, amount string) (transactionID, idempotencyKey, nonce string, timeRemaining int64, err error) { + if !(transactionType == TypePayment || transactionType == TypeAuthorization) { err = errors.New("invalid transaction type") return } - url := fmt.Sprintf("%s/transactions", sandboxURL) - if v.Environment == "production" { - url = fmt.Sprintf("%s/transactions", productionURL) - } + url := fmt.Sprintf("%s/transactions/%s", baseURL, transactionID) idempotencyKey, nonce = shortUUID(), shortUUID() @@ -170,21 +158,18 @@ func (v *VPOS) PaymentTransaction(transactionType, mobile, amount string) (trans } transactionID = locationParts[locationPartsSize-1] - timeRemaining, _ = v.TransactionRemainingTime(transactionID) + timeRemaining, _ = v.GetRequest(transactionID) return } -//It creates a new payment transaction from a previously accepted authorization transaction +// It creates a new payment transaction from a previously accepted authorization transaction func (v *VPOS) PaymentWithAuthorization(parent_transaction_id, amount string) (transactionID, idempotencyKey, nonce string, timeRemaining int64, err error) { - url := fmt.Sprintf("%s/transactions", sandboxURL) - if v.Environment == "PRD" { - url = fmt.Sprintf("%s/transactions", productionURL) - } + url := fmt.Sprintf("%s/transactions/%s", baseURL, transactionID) idempotencyKey, nonce = shortUUID(), shortUUID() request := AuthorizedPaymentTransaction{ - Type: paymentTransaction, + Type: TypePayment, ParentTransactionID: parent_transaction_id, Amount: amount, CallbackURL: fmt.Sprintf("%s?nonce=%s", v.PaymentCallbackURL, nonce), @@ -216,24 +201,21 @@ func (v *VPOS) PaymentWithAuthorization(parent_transaction_id, amount string) (t } transactionID = locationParts[locationPartsSize-1] - timeRemaining, _ = v.TransactionRemainingTime(transactionID) + timeRemaining, _ = v.GetRequest(transactionID) return } -//transactionType is either 'refund' or 'cancelation' -//if transactionType is 'refund', it will create a new refund transaction -//if transactionType is 'cancelation', it will create a new cancelation transaction for a previously accepted payment authorization -func (v *VPOS) RefundOrCancelation(transactionType, parentTransactionID string) (transactionID, idempotencyKey, nonce string, timeRemaining int64, err error) { - if !(transactionType == refundTransaction || transactionType == cancelationTransaction) { +// transactionType is either 'refund' or 'cancelation' +// if transactionType is 'refund', it will create a new refund transaction +// if transactionType is 'cancelation', it will create a new cancelation transaction for a previously accepted payment authorization +func (v *VPOS) RefundOrCancelation(transactionType TransactionType, parentTransactionID string) (transactionID, idempotencyKey, nonce string, timeRemaining int64, err error) { + if !(transactionType == TypeRefund || transactionType == TypeCancelation) { err = errors.New("invalid transaction type") return } - url := fmt.Sprintf("%s/transactions", sandboxURL) - if v.Environment == "production" { - url = fmt.Sprintf("%s/transactions", productionURL) - } + url := fmt.Sprintf("%s/transactions/%s", baseURL, transactionID) idempotencyKey, nonce = shortUUID(), shortUUID() @@ -269,16 +251,13 @@ func (v *VPOS) RefundOrCancelation(transactionType, parentTransactionID string) } transactionID = locationParts[locationPartsSize-1] - timeRemaining, _ = v.TransactionRemainingTime(transactionID) + timeRemaining, _ = v.GetRequest(transactionID) return } func (v *VPOS) GetTransaction(transactionID string) (transaction Transaction, err error) { - url := fmt.Sprintf("%s/transactions/%s", sandboxURL, transactionID) - if v.Environment == "PRD" { - url = fmt.Sprintf("%s/transactions/%s", productionURL, transactionID) - } + url := fmt.Sprintf("%s/transactions/%s", baseURL, transactionID) response, err := httpGet( url, diff --git a/vpos_test.go b/vpos_test.go index 9c6189f..5051b36 100644 --- a/vpos_test.go +++ b/vpos_test.go @@ -10,13 +10,12 @@ var ( refundCallbackURL = "" paymentCallbackURL = "" supervidorCard = "" - vpos, _ = NewVPOS( + vpos = NewVPOS( posID, token, paymentCallbackURL, refundCallbackURL, supervidorCard, - "development", ) )