PHP code example of samir-hussein / 2checkout
1. Go to this page and download the library: Download samir-hussein/2checkout 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/ */
samir-hussein / 2checkout example snippets
use TwoCheckout\TwoCheckOut;
merchantCode',
'privateKey' => 'privateKey',
'demo' => true,
];
$init = new TwoCheckOut($config);
$sale = TwoCheckOut::createSale([
"merchantOrderId" => "123",
"currency" => "EGP",
"token" => $_POST['token'],
"lineItems" => [
[
"name" => "watch",
"price" => "4.99",
"type" => "product",
"quantity" => "1",
"recurrence" => "4 Year",
"startupFee" => "0"
]
],
"billingAddr" => [
"name" => $_POST['Cardplaceholder'],
"addrLine1" => "123 test blvd",
"city" => "nasr city",
"state" => "cairo",
"zipCode" => "43123",
"country" => "EGY",
"email" => "[email protected] ",
"phoneNumber" => "123456789"
],
]);
object(stdClass)#4 (3) {
["validationErrors"]=>
NULL
["exception"]=>
NULL
["response"]=>
object(stdClass)#5 (13) {
["type"]=>
string(12) "AuthResponse"
["currencyCode"]=>
string(3) "EGP"
["recurrentInstallmentId"]=>
NULL
["responseMsg"]=>
string(48) "Successfully authorized the provided credit card"
["lineItems"]=>
array(1) {
[0]=>
object(stdClass)#6 (11) {
["duration"]=>
string(7) "Forever"
["options"]=>
array(0) {
}
["price"]=>
string(4) "4.99"
["quantity"]=>
string(1) "1"
["recurrence"]=>
string(6) "4 Year"
["startupFee"]=>
string(4) "0.00"
["productId"]=>
string(0) ""
["tangible"]=>
string(1) "N"
["name"]=>
string(5) "watch"
["type"]=>
string(7) "product"
["description"]=>
string(0) ""
}
}
["transactionId"]=>
string(12) "250763260588"
["billingAddr"]=>
object(stdClass)#7 (10) {
["addrLine1"]=>
string(13) "123 test blvd"
["addrLine2"]=>
NULL
["city"]=>
string(9) "nasr city"
["zipCode"]=>
string(5) "43123"
["phoneNumber"]=>
string(9) "123456789"
["phoneExtension"]=>
NULL
["email"]=>
string(15) "[email protected] "
["name"]=>
string(8) "John Doe"
["state"]=>
string(5) "cairo"
["country"]=>
string(3) "EGY"
}
["shippingAddr"]=>
object(stdClass)#8 (10) {
["addrLine1"]=>
NULL
["addrLine2"]=>
NULL
["city"]=>
NULL
["zipCode"]=>
NULL
["phoneNumber"]=>
NULL
["phoneExtension"]=>
NULL
["email"]=>
NULL
["name"]=>
NULL
["state"]=>
NULL
["country"]=>
NULL
}
["merchantOrderId"]=>
string(3) "123"
["orderNumber"]=>
string(12) "250763260589"
["responseCode"]=>
string(8) "APPROVED"
["total"]=>
string(4) "4.99"
["errors"]=>
NULL
}
}
// in providers
TwoCheckOut\TwoCheckoutServiceProvider::class,
// in aliases
'TwoCheckOut' => TwoCheckOut\Facades\TwoCheckOut::class,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form id="myCCForm" action="/payment" method="post">
@csrf
<input id="token" name="token" type="hidden" value="">
<div>
<label>
<span>Card Number</span>
</label>
<input id="ccNo" type="text" size="20" value="" autocomplete="off" bel>
<span>CVC</span>
</label>
<input id="cvv" size="4" type="text" value="" autocomplete="off" .
myForm.submit();
};
// Called when token creation fails.
var errorCallback = function(data) {
if (data.errorCode === 200) {
tokenRequest();
} else {
alert(data.errorMsg);
}
};
var tokenRequest = function() {
// Setup token request arguments
var args = {
sellerId: "{{config('2checkout.merchantCode')}}",
publishableKey: "{{config('2checkout.publishableKey')}}",
ccNo: $("#ccNo").val(),
cvv: $("#cvv").val(),
expMonth: $("#expMonth").val(),
expYear: $("#expYear").val()
};
// Make the token request
TCO.requestToken(successCallback, errorCallback, args);
};
$(function() {
// Pull in the public encryption key for our environment
TCO.loadPubKey();
$("#myCCForm").submit(function(e) {
// Call our token request function
tokenRequest();
// Prevent form from submitting
return false;
});
});
</script>
</body>
</html>
namespace App\Http\Controllers;
use TwoCheckOut\Facades\TwoCheckOut;
use Illuminate\Http\Request;
class TwoCheckOutController extends Controller
{
public function index(Request $request)
{
$sale = TwoCheckOut::createSale([
"merchantOrderId" => "123",
"currency" => "EGP",
"token" => $request->token,
"lineItems" => [
[
"name" => "watch",
"price" => "4.99",
"type" => "product",
"quantity" => "1",
"recurrence" => "4 Year",
"startupFee" => "0"
]
],
"billingAddr" => [
"name" => $request->Cardplaceholder,
"addrLine1" => "123 test blvd",
"city" => "nasr city",
"state" => "cairo",
"zipCode" => "43123",
"country" => "EGY",
"email" => "[email protected] ",
"phoneNumber" => "123456789"
],
]);
}
}
{#444 ▼
+"validationErrors": null
+"exception": null
+"response": {#450 ▼
+"type": "AuthResponse"
+"currencyCode": "EGP"
+"recurrentInstallmentId": null
+"responseMsg": "Successfully authorized the provided credit card"
+"lineItems": array:1 [▼
0 => {#454 ▼
+"duration": "Forever"
+"options": []
+"price": "4.99"
+"quantity": "1"
+"recurrence": "4 Year"
+"startupFee": "0.00"
+"productId": ""
+"tangible": "N"
+"name": "watch"
+"type": "product"
+"description": ""
}
]
+"transactionId": "250763328730"
+"billingAddr": {#451 ▼
+"addrLine1": "123 test blvd"
+"addrLine2": null
+"city": "nasr city"
+"zipCode": "43123"
+"phoneNumber": "123456789"
+"phoneExtension": null
+"email": "[email protected] "
+"name": "John Doe"
+"state": "cairo"
+"country": "EGY"
}
+"shippingAddr": {#455 ▼
+"addrLine1": null
+"addrLine2": null
+"city": null
+"zipCode": null
+"phoneNumber": null
+"phoneExtension": null
+"email": null
+"name": null
+"state": null
+"country": null
}
+"merchantOrderId": "123"
+"orderNumber": "250763328731"
+"responseCode": "APPROVED"
+"total": "4.99"
+"errors": null
}
}
bash
php artisan vendor:publish --provider="TwoCheckOut\TwoCheckoutServiceProvider"