PHP code example of innovaweb / transbank

1. Go to this page and download the library: Download innovaweb/transbank library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

innovaweb / transbank example snippets


private $webpay_plus;

public function __construct()
{
    if (env('APP_ENV') == 'production') {

        $this->webpay_plus = new WebpayPlus(
            env('TBK_CC'),
            env('TBK_API_KEY'),
            WebpayPlus::PRODUCTION
        );

    } else {
        $this->webpay_plus = new WebpayPlus();
    }
}

createTransaction($buy_order, $session_id, $amount, $url_return);

commitTransaction($token_ws);

refundTransaction($token, $amount);

getTransactionStatus($token);



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Innovaweb\Transbank\Helpers\HelperTransbankResponseCode;
use Innovaweb\Transbank\WebpayPlus;

class WebpayPlusController extends Controller
{
    private $webpay_plus;

    public function __construct()
    {
        if (env('APP_ENV') == 'production') {

            $this->webpay_plus = new WebpayPlus(
                env('TBK_CC'),
                env('TBK_API_KEY'),
                WebpayPlus::PRODUCTION
            );

        } else {
            $this->webpay_plus = new WebpayPlus();
        }
    }

    public function create(Request $request)
    {
        $response = $this->webpay_plus->createTransaction(
            $request->buy_order,
            'session' . $request->buy_order,
            $request->amount,
            route('response-wp-plus')
        );
        return $this->webpay_plus->redirectHTML();
    }

    public function response(Request $request)
    {
        if ($request->token_ws) {

            $commit = $this->webpay_plus->commitTransaction($request->token_ws);
            $response = $commit['response'];
            
            return [
                $response,
                'vci' => HelperTransbankResponseCode::VCI($response->vci),
                'responseCode' => HelperTransbankResponseCode::ResponseCode($response->responseCode),
                'paymentTypeCode' => HelperTransbankResponseCode::PaymentTypeCode($response->paymentTypeCode),
                'status' => HelperTransbankResponseCode::Status($response->status),
            ];
        } else {
            //transacción anulada
            return $request->all();
        }
    }
}