PHP code example of 2checkoutv2 / 2checkout-php-sdk
1. Go to this page and download the library: Download 2checkoutv2/2checkout-php-sdk 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/ */
2checkoutv2 / 2checkout-php-sdk example snippets
use Tco\TwocheckoutFacade;
$config = array(
'sellerId' => YOUR_MERCHANT_CODE, // REQUIRED
'secretKey' => YOUR_SECRET_KEY, // REQUIRED
'buyLinkSecretWord' => YOUR_SECRET_WORD,
'jwtExpireTime' => 30,
'curlVerifySsl' => 1
);
$tco = new TwocheckoutFacade($config);
$result = $tco->apiCore()->call( '/orders/', $params, 'POST' );
$dynamicOrderParams = array(
'Country' => 'US',
'Currency' => 'USD',
'CustomerIP' => '91.220.121.21',
'ExternalReference' => 'CustOrd101',
'Language' => 'en',
'Source' => 'tcolib.local',
'BillingDetails' =>
array(
'Address1' => 'Street 1',
'City' => 'Cleveland',
'State' => 'Ohio',
'CountryCode' => 'US',
'Email' => '[email protected] ',
'FirstName' => 'John',
'LastName' => 'Doe',
'Zip' => '20034',
),
'Items' =>
array(
0 =>
array(
'Name' => 'Dynamic product2',
'Description' => 'Product 2',
'Quantity' => 3, //units
'IsDynamic' => true,
'Tangible' => false,
'PurchaseType' => 'PRODUCT',
'Price' =>
array(
'Amount' => 6, //value
'Type' => 'CUSTOM',
),
),
1 =>
array(
'Name' => 'Dynamic product',
'Description' => 'Test description',
'Quantity' => 4,
'IsDynamic' => true,
'Tangible' => false,
'PurchaseType' => 'PRODUCT',
'Price' =>
array(
'Amount' => 1,
'Type' => 'CUSTOM',
)
),
),
'PaymentDetails' =>
array(
'Type' => 'TEST', //'TEST' or 'EES_TOKEN_PAYMENT'
'Currency' => 'USD',
'CustomerIP' => '91.220.121.21',
'PaymentMethod' =>
array(
'RecurringEnabled' => false,
'HolderNameTime' => 1,
'CardNumberTime' => 1,
),
),
);
$order = $tco->order();
$response = $order->place( $dynamicOrderParams );
$orderData = $tco->order()->getOrder( array( 'RefNo' => $response['refNo'] ) );
$orderCatalogProductParams = array(
'Country' => 'br',
'Currency' => 'brl',
'CustomerIP' => '91.220.121.21',
'ExternalReference' => 'CustOrderCatProd100',
'Language' => 'en',
'BillingDetails' =>
array(
'Address1' => 'Test Address',
'City' => 'LA',
'CountryCode' => 'BR',
'Email' => '[email protected] ',
'FirstName' => 'Customer',
'FiscalCode' => '056.027.963-98',
'LastName' => '2Checkout',
'Phone' => '556133127400',
'State' => 'DF',
'Zip' => '70403-900',
),
'Items' =>
array(
0 =>
array(
'Code' => 'E377076E6A_COPY1', //Get the code from CPANEL at Setup->Products->Code
column
'Quantity' => '1',
),
),
'PaymentDetails' =>
array(
'Type' => 'TEST',
'Currency' => 'USD',
'CustomerIP' => '91.220.121.21',
'PaymentMethod' =>
array(
'CCID' => '123',
'CardNumber' => '4111111111111111',
'CardNumberTime' => '12',
'CardType' => 'VISA',
'ExpirationMonth' => '12',
'ExpirationYear' => '2023',
'HolderName' => 'John Doe',
'HolderNameTime' => '12',
'RecurringEnabled' => true,
'Vendor3DSReturnURL' => 'www.test.com',
'Vendor3DSCancelURL' => 'www.test.com',
),
),
);
'RecurringOptions' =>
array(
'CycleLength' => 1,
'CycleUnit' => 'MONTH',
'CycleAmount' => 3,
'ContractLength' => 3,
'ContractUnit' => 'Year',
),
'Items' =>
array(
0 =>
array(
'Name' => 'Dynamic product',
'Description' => 'Product Description test',
'Quantity' => 3, //units
'IsDynamic' => true,
'Tangible' => false,
'PurchaseType' => 'PRODUCT',
'Price' =>
array(
'Amount' => 6, //amount in currency units
'Type' => 'CUSTOM',
),
//Dynamic product subscription.
'RecurringOptions' =>
array(
'CycleLength' => 1, //The length of the recurring billing cycle.
'CycleUnit' => 'MONTH', //Unit of measuring billing cycles (years, months).
'CycleAmount' => 3, //The amount to be billed on each renewal.
'ContractLength' => 3, //The contact length for which the recurring option will apply.
'ContractUnit' => 'Year', //Unit of measuring contact length (years, months).
)
)
)
/**
* This will return the following array data:
* return an array having this structure:
* array(
* 'item_code' => array(
* 0 => 'subscription1_code',
* 1=> 'Subscription2_code'
* )
* )
**/
$productsRefWithSubscriptionRefArray = $tco->subscription()->getSubscriptionsByOrderRefNo( $orderData['RefNo'] );
$subscriptionSearch['SubscriptionReference'] = reset($productsRefWithSubscriptionRefArray)[0];
$subscriptionData = $tco->subscription()->searchSubscriptions($subscriptionSearch);
$buyLinkParameters = array (
'address' => 'Street 1',
'city' => 'Cleveland',
'country' => 'US',
'name' => 'John Doe',
'phone' => '0018143519403',
'zip' => '20034',
'email' => '[email protected] ',
'company-name' => 'Verifone',
'state' => 'Ohio',
'ship-name' => 'John Doe',
'ship-address' => 'Street 1',
'ship-city' => 'Cleveland',
'ship-country' => 'US',
'ship-email' => '[email protected] ',
'ship-state' => 'Ohio',
'prod' => 'Colored Pencil',
'price' => 2,
'qty' => 1,
'type' => 'PRODUCT',
'tangible' => 0, // int 0 or 1
'src' => 'phpLibrary',
'return-url' => 'http://tcolib.local/Examples/Controllers/BuyLink/paymentCallback.php',
'return-type' => 'redirect',
'expiration' => 1617117946, // this is a dynamic unixtimestamp value
'order-ext-ref' => 'CustOrd101',
'item-ext-ref' => '20210330102546',
'customer-ext-ref' => '[email protected] ',
'currency' => 'usd',
'language' => 'en',
'test' => 1, //int (0 or 1)
'merchant' => '', //one needs a valid merchant id
'dynamic' => 1, //always int (0 or 1)
'recurrence' => '1:MONTH',
'duration' => '12:MONTH',
'renewal-price' => 2,
);
$buyLinkSignature = $tco->getBuyLinkSignature($buyLinkParameters);
$buyLinkParameters['signature'] = $buyLinkSignature;
$redirectTo = 'https://secure.2checkout.com/checkout/buy/?' . ( http_build_query( $buyLinkParameters ) );
//Now one can just redirect to the generated url.
$orderData = $tco->order()->getOrder( array( 'RefNo' => $this->lastRefNo ) );
//validate if requested order is valid
// Constructing FULL Refund Details
$refundData = array(
'RefNo' => $orderData['RefNo'], //Required
'refundParams' => array(
"amount" => $orderData["GrossPrice"], //Required
"comment" => 'REFUND Example',
"reason" => 'Other' //Required, default reason is "Other"
)
);
$response = $tco->order()->issueRefund( $refundData );
$params = $_POST;
if ( ! isset( $params['REFNOEXT'] ) && ( ! isset( $params['REFNO'] ) && empty( $params['REFNO'] ) ) ) {
throw new TcoException( 'Cannot identify order in Ipn request.' );
}
$validIpn = $tco->validateIpnResponse($params);
$responseToken = $tco->generateIpnResponse($params);
echo $responseToken;