Logo

dev-resources.site

for different kinds of informations.

Webpay Plus in golang

Published at
12/26/2020
Categories
transbank
chile
go
rest
Author
fenriz07
Categories
4 categories in total
transbank
open
chile
open
go
open
rest
open
Author
8 person written this
fenriz07
open
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

chile Article's
30 articles in total
Favicon
Como hice dinero (USD 2.500) con Telegram en Chile
Favicon
¿Trabajo Remoto o Híbrido?
Favicon
Super Club Net: Libro de Introducción a HTML, CSS y JS
Favicon
Desafío de Nombres y Apellidos
Favicon
La revista Club Nintendo y su curso de HTML
Favicon
Historia de la BeamVM
Favicon
Introducción a Elixir
Favicon
Como resolver problemas de cors rapidamente
Favicon
Documenta tu Evolución Profesional
Favicon
¿Me conviene un Bootcamp Tecnológico?
Favicon
Arqueología Tech: Rolplay un juego de cartas hecho en Visual Basic 6
Favicon
Mi peor experiencia como desarrollador de software 😢 chile
Favicon
Aplicando MVVM en Phoenix LiveView
Favicon
API Rest patentes de autos en Chile
Favicon
Elixir para React.js Devs
Favicon
API días feriados
Favicon
Eligiendo un computador para desarrollo
Favicon
OpenAPI: API pública para Chile
Favicon
¿Por qué elegir Elixir y Phoenix?
Favicon
NodeSchool Santiago! 🎉
Favicon
Oportunidades laborales en Chile para desarrolladores e ingenieros de software
Favicon
Employment opportunities in Chile for software developers and engineers
Favicon
JSConf Chile , la guía del viajero : Barrio Italia
Favicon
JSConf Chile Guía del viajero : Barrio Lastarria
Favicon
Número de Documento en vez de RUT para Identificar
Favicon
¿Cómo conseguir que tu empresa auspicie la JSConf?
Favicon
Anunciando la JSConf Chile! 🇨🇱🎉
Favicon
Cómo validar un R.U.T. Chileno
Favicon
Sobre renuncia voluntaria (thread)
Favicon
Webpay Plus in golang

Featured ones: