PHP code example of tpaga / tpaga-php

1. Go to this page and download the library: Download tpaga/tpaga-php 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/ */

    

tpaga / tpaga-php example snippets








$config = new Tpaga\Configuration();

$private_token = 'd13fr8n7vhvkuch3lq2ds5qhjnd2pdd2';
$public_token = 'pk_test_qvbvuthlvqpijnr0elmtg5jh';

$apiClient = new Tpaga\ApiClient($config);

function create_customer() {

    $GLOBALS["config"]->setUsername($GLOBALS["private_token"]);

    $customer = new Tpaga\Model\Customer();
    $customer->setFirstName("Numa");
    $customer->setLastName("Nigerio");
    $customer->setGender("M");
    $customer->setEmail("[email protected]");
    $customer->setPhone("6326800514");

    $customer_api = new Tpaga\Api\CustomerAPI($GLOBALS["apiClient"]);

    try {
        $persisted_customer = $customer_api->createCustomer($customer);
        var_dump($persisted_customer);
        return $persisted_customer;
    } catch (Exception $e) {
        echo 'Caught exception: ', $e->getMessage(), "\n";
    }

}

function tokenize_credit_card() {

    $GLOBALS["config"]->setUsername($GLOBALS["public_token"]);

    $cctoken = new Tpaga\Model\CreditCardToken();
    $cctoken->setPrimaryAccountNumber("4111111111111111");
    $cctoken->setExpirationMonth("08");
    $cctoken->setExpirationYear("2020");
    $cctoken->setCardHolderName("John Smith");

    $cctoken_api = new Tpaga\Api\tokenizeAPI($GLOBALS["apiClient"]);

    try {
        $persisted_cctoken = $cctoken_api->tokenizeCreditCard($cctoken);
        var_dump($persisted_cctoken);
        return $persisted_cctoken;
    } catch (Exception $e) {
        echo 'Caught exception: ', $e->getMessage(), "\n";
    }

}

function associate_token_to_customer($customer_id, $token) {
    $GLOBALS["config"]->setUsername($GLOBALS["private_token"]);

    $assoc_api = new Tpaga\Api\TokenApi($GLOBALS["apiClient"]);

    try {
        $persisted_cc = $assoc_api->addCreditCardToken($customer_id, $token);
        var_dump($persisted_cc);
        return $persisted_cc;
    } catch (Exception $e) {
        echo 'Caught exception: ', $e->getMessage(), "\n";
    }
}

function charge($cc_token) {

    $GLOBALS["config"]->setUsername($GLOBALS["private_token"]);

    $charge = new Tpaga\Model\CreditCardCharge();
    $charge->setCreditCard($cc_token);
    $charge->setAmount(20000);
    $charge->setTaxAmount(200);
    $charge->setCurrency("COP");
    $charge->setDescription("Sample description");
    $charge->setOrderId("Sample order Id");
    $charge->setInstallments(1);
    $charge->setThirdPartyId("Sample TPId" );
    $charge_api = new Tpaga\Api\ChargeAPI($GLOBALS["apiClient"]);

    try {
        $response = $charge_api->addCreditCardCharge($charge);
        var_dump($response);
        return $response;
    } catch (Exception $e) {
        echo 'Caught exception: ', $e->getMessage(), "\n";
    }

}

function refund($charge_id) {

    $GLOBALS["config"]->setUsername($GLOBALS["private_token"]);

    $refund_request = new Tpaga\Model\CreditCardRefund();
    $refund_request->setId($charge_id);
    $refund_api = new Tpaga\Api\RefundApi($GLOBALS["apiClient"]);

    try {
        $response = $refund_api->refundCreditCardCharge($refund_request);
        var_dump($response);
        return $response;
    } catch (Exception $e) {
        echo 'Caught exception: ', $e->getMessage(), "\n";
    }

}

$customer = create_customer();
$cctoken = tokenize_credit_card();
$credit_card = associate_token_to_customer($customer->getId(), $cctoken);
$charge = charge($credit_card->getId());
refund($charge->getId());
bash
composer