DEV Community

Cover image for Webpay Plus in golang
Servio Zambrano
Servio Zambrano

Posted on

Webpay Plus in golang

What is webpay?

Internet transaction system with credit or debit cards on the merchant's website for chile.

November 2, 2020 transbank publishes the new REST API for its Webpay Plus product. Previously and still works your product under the communication protocol SOAP (Simple Object Access Protocol)

Flow on Success - SOAP

Golang-WebPay

Flow on Success — REST

Golang-WebPay

Comparison between SOAP and REST

We can see that transbank simplified the flow, since compared to soap, after confirming a transaction control was returned to transbank for 2 times in the browser, now in the REST version steps 15 and 16 were removed from the flow, as a result webpay does not show your receipt, giving the responsibility of this to the merchant itself.

Integration with golang

Exploring the go ecosystem, there is no package that facilitates integration with the new product, for this reason I decided to release a package for that purpose.
The Package - Unofficial Library for WebPay Plus (REST API)

https://github.com/fenriz07/Golang-Transbank-WebPay-Rest

Features

  • Support for integration and production environment
  • Create transaction.
  • Confirm a transaction.
  • Get the status of a transaction.
  • Reverse or Cancel a payment.
  • Consolidation of responses in structs.
  • Handling http errors.

Usage example (Gorilla):

Project example where https://github.com/fenriz07/ExampleTransbankGoRest is implemented

Installation

go get github.com/fenriz07/Golang-Transbank-WebPay-Rest
Enter fullscreen mode Exit fullscreen mode

Use

Initialize environment

There are 2 environments Integration and production

//Import package webpayplus
import (
    "github.com/fenriz07/Golang-Transbank-WebPay-Rest/pkg/webpayplus"
)
Enter fullscreen mode Exit fullscreen mode
Integration
/*
    Use the SetEnvironmentIntegration function for the development environment.
   Automatically configures the merchant's credentials.
   Configure the client and all transactions will be executed under this environment automatically

*/
webpayplus.SetEnvironmentIntegration()
Enter fullscreen mode Exit fullscreen mode
Production
/*
Use the SetEnvironmentProduction function for the development environment.
   Automatically configures the merchant's credentials.
   Configure the client and all transactions will be executed under this environment automatically

   This function accepts 2 parameters
   1 - APIKeyID (string)
   2 - APIKeySecret (string)
* /

APIKeyID: = "Commercial code"
APIKeySecret: = "Secret key"

webpayplus.SetEnvironmentProduction(APIKeyID,APIKeySecret)
Enter fullscreen mode Exit fullscreen mode

Create a Webpay Plus transaction

To create a transaction, just call the transaction.Create () method

/ * The following parameters must be passed buy 
Order = order number (string) 
sessionID = session (string) 
amount = amount to be charged (int) 
returnURL = return url that webpay will return
* /
Create(buyOrder string, sessionID string, amount int, returnURL string)
Enter fullscreen mode Exit fullscreen mode

It returns the following response struct:

/*TransactionCreateResponse struct with contain skeleton json to createResponse*/
type TransactionCreateResponse struct {
    Token string `json:"token"`
    URL   string `json:"url"`
}
Enter fullscreen mode Exit fullscreen mode

Confirm a Webpay Plus transaction

When the merchant takes back control through return_url you must confirm and obtain the result of a transaction using the transaction.Commit () method

/* The following parameters must be passed token = token returned by transbank (string)
*/
Commit(token string) (response.TransactionCommitResponse, error)
Enter fullscreen mode Exit fullscreen mode

It returns the following response struct:

/*TransactionCommitResponse struct with contain skeleton json to commitResponse*/
type TransactionCommitResponse struct {
    Vci        string `json:"vci"`
    Amount     int    `json:"amount"`
    Status     string `json:"status"`
    BuyOrder   string `json:"buy_order"`
    SessionID  string `json:"session_id"`
    CardDetail struct {
        CardNumber string `json:"card_number"`
    } `json:"card_detail"`
    AccountingDate     string    `json:"accounting_date"`
    TransactionDate    time.Time `json:"transaction_date"`
    AuthorizationCode  string    `json:"authorization_code"`
    PaymentTypeCode    string    `json:"payment_type_code"`
    ResponseCode       int       `json:"response_code"`
    InstallmentsNumber int       `json:"installments_number"`
}

Enter fullscreen mode Exit fullscreen mode

Get status of a Webpay Plus transaction

This operation allows you to obtain the status of the transaction at any time. Under normal conditions, it is probably not required to execute, but in the event of an unexpected error, it allows knowing the status and taking the corresponding actions. transaction.GetStatus ()

/* The following parameters must be passed token = token returned by transbank (string)
*/
GetStatus(token string) (response.TransactionStatusResponse, error)
Enter fullscreen mode Exit fullscreen mode

It returns the following response struct:

/*TransactionCommitResponse struct with contain skeleton json to commitResponse*/
type TransactionCommitResponse struct {
    Vci        string `json:"vci"`
    Amount     int    `json:"amount"`
    Status     string `json:"status"`
    BuyOrder   string `json:"buy_order"`
    SessionID  string `json:"session_id"`
    CardDetail struct {
        CardNumber string `json:"card_number"`
    } `json:"card_detail"`
    AccountingDate     string    `json:"accounting_date"`
    TransactionDate    time.Time `json:"transaction_date"`
    AuthorizationCode  string    `json:"authorization_code"`
    PaymentTypeCode    string    `json:"payment_type_code"`
    ResponseCode       int       `json:"response_code"`
    InstallmentsNumber int       `json:"installments_number"`
}

Enter fullscreen mode Exit fullscreen mode

Reverse or Cancel a Webpay Plus payment

This method allows all enabled merchants to refund or cancel a transaction that was generated in Webpay Plus. transaction.Refund ()

/* The following parameters must be passed
   token = token returned by transbank (string)
   amount = amount to be refunded or canceled (int)
*/
func Refund(token string, amount int) (response.TransactionRefundResponse, error)
Enter fullscreen mode Exit fullscreen mode

It returns the following response struct:

/*TransactionRefundResponse struct with contain skeleton json to refundResponse*/
type TransactionRefundResponse struct {
    Type              string    `json:"type"`
    AuthorizationCode string    `json:"authorization_code"`
    AuthorizationDate time.Time `json:"authorization_date"`
    NullifiedAmount   float64   `json:"nullified_amount"`
    Balance           float64   `json:"balance"`
    ResponseCode      int       `json:"response_code"`
}

Enter fullscreen mode Exit fullscreen mode

Creator

Fenriz07

Top comments (0)