Webpay Plus in golang
Tags: transbank, chile, go, rest
Published: 2020-12-26 20:48:54

### 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](https://miro.medium.com/max/517/1*HgejrsNDKEFBfFXpnPSJNw.png)

#### Flow on Success — REST
![Golang-WebPay](https://miro.medium.com/max/700/1*SS3YhOrCO9d5DxKjGh5ozQ.png)

#### 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

```bash
go get github.com/fenriz07/Golang-Transbank-WebPay-Rest
```

#### Use

##### Initialize environment

There are 2 environments Integration and production

```go
//Import package webpayplus
import (
	"github.com/fenriz07/Golang-Transbank-WebPay-Rest/pkg/webpayplus"
)
```

##### Integration

```go
/*
    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()
```

##### Production

```go
/*
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)
```

### Create a Webpay Plus transaction

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

```go
/ * 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)
```

It returns the following response `struct`:

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

### 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

```go
/* The following parameters must be passed token = token returned by transbank (string)
*/
Commit(token string) (response.TransactionCommitResponse, error)
```

It returns the following response `struct`:

```go
/*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"`
}

```

### 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 ()`

```go
/* The following parameters must be passed token = token returned by transbank (string)
*/
GetStatus(token string) (response.TransactionStatusResponse, error)
```

It returns the following response `struct`:

```go
/*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"`
}

```

### 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 ()`

```go
/* 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)
```

It returns the following response `struct`:

```go
/*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"`
}

```

## Creator

[Fenriz07](https://github.com/fenriz07)