PHP code example of 2checkout / 2checkout-php
1. Go to this page and download the library: Download 2checkout/2checkout-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/ */
2checkout / 2checkout-php example snippets
Twocheckout::format('json');
// Your sellerId(account number) and privateKey are 996');
Twocheckout::sellerId('901248204');
// Your username and password are verification (Please don't do this in your production environment)
Twocheckout::verifySSL(false); // this is set to true by default
// To use your sandbox account set sandbox to true
Twocheckout::sandbox(true);
// All methods return an Array by default or you can set the format to 'json' to get a JSON response.
Twocheckout::format('json');
Twocheckout::privateKey('BE632CB0-BB29-11E3-AFB6-D99C28100996');
Twocheckout::sellerId('901248204');
try {
$charge = Twocheckout_Charge::auth(array(
"sellerId" => "901248204",
"merchantOrderId" => "123",
"token" => 'MjFiYzIzYjAtYjE4YS00ZmI0LTg4YzYtNDIzMTBlMjc0MDlk',
"currency" => 'USD',
"total" => '10.00',
"billingAddr" => array(
"name" => 'Testing Tester',
"addrLine1" => '123 Test St',
"city" => 'Columbus',
"state" => 'OH',
"zipCode" => '43123',
"country" => 'USA',
"email" => '[email protected] ',
"phoneNumber" => '555-555-5555'
),
"shippingAddr" => array(
"name" => 'Testing Tester',
"addrLine1" => '123 Test St',
"city" => 'Columbus',
"state" => 'OH',
"zipCode" => '43123',
"country" => 'USA',
"email" => '[email protected] ',
"phoneNumber" => '555-555-5555'
)
));
$this->assertEquals('APPROVED', $charge['response']['responseCode']);
} catch (Twocheckout_Error $e) {
$this->assertEquals('Unauthorized', $e->getMessage());
}
Array
(
[validationErrors] =>
[exception] =>
[response] => Array
(
[type] => AuthResponse
[lineItems] => Array
(
[0] => Array
(
[options] => Array
(
)
[price] => 10.00
[quantity] => 1
[recurrence] =>
[startupFee] =>
[productId] =>
[tangible] => N
[name] => 123
[type] => product
[description] =>
[duration] =>
)
)
[transactionId] => 205181140830
[billingAddr] => Array
(
[addrLine1] => 123 Test St
[addrLine2] =>
[city] => Columbus
[zipCode] => 43123
[phoneNumber] => 555-555-5555
[phoneExtension] =>
[email] => [email protected]
[name] => Testing Tester
[state] => OH
[country] => USA
)
[shippingAddr] => Array
(
[addrLine1] => 123 Test St
[addrLine2] =>
[city] => Columbus
[zipCode] => 43123
[phoneNumber] =>
[phoneExtension] =>
[email] =>
[name] => Testing Tester
[state] => OH
[country] => USA
)
[merchantOrderId] => 123
[orderNumber] => 205181140821
[recurrentInstallmentId] =>
[responseMsg] => Successfully authorized the provided credit card
[responseCode] => APPROVED
[total] => 10.00
[currencyCode] => USD
[errors] =>
)
)
Twocheckout::username('testlibraryapi901248204');
Twocheckout::password('testlibraryapi901248204PASS');
$args = array(
'sale_id' => 4834917619
);
try {
$result = Twocheckout_Sale::stop($args);
} catch (Twocheckout_Error $e) {
$e->getMessage();
}
[response_code] => OK
[response_message] => Array
(
[0] => 4834917634
[1] => 4834917646
[2] => 4834917658
)
$params = array(
'sid' => '1817037',
'mode' => '2CO',
'li_0_name' => 'Test Product',
'li_0_price' => '0.01'
);
Twocheckout_Charge::form($params, 'auto');
<form id="2checkout" action="https://www.2checkout.com/checkout/spurchase" method="post">
<input type="hidden" name="sid" value="1817037"/>
<input type="hidden" name="mode" value="2CO"/>
<input type="hidden" name="li_0_name" value="Test Product"/>
<input type="hidden" name="li_0_price" value="0.01"/>
<input type="submit" value="Click here if you are not redirected automatically" /></form>
<script type="text/javascript">document.getElementById('2checkout').submit();</script>
$params = array();
foreach ($_REQUEST as $k => $v) {
$params[$k] = $v;
}
$passback = Twocheckout_Return::check($params, "tango");
[response_code] => Success
[response_message] => Hash Matched
$params = array();
foreach ($_POST as $k => $v) {
$params[$k] = $v;
}
$passback = Twocheckout_Notification::check($params, "tango");
[response_code] => Success
[response_message] => Hash Matched
Twocheckout::username('testlibraryapi901248204');
Twocheckout::password('testlibraryapi901248204PASS');
$params = array(
'sale_id' => 4774380224,
'category' => 1,
'comment' => 'Order never sent.'
);
try {
$sale = Twocheckout_Sale::refund($params);
} catch (Twocheckout_Error $e) {
$e->getMessage();
}
shell
git clone https://github.com/2Checkout/2checkout-php.git