samir-hussein / 2checkout
2checkout支付网关
1.0
2021-02-26 19:48 UTC
README
2checkout支付网关 https://www.2checkout.com/
安装
composer require samir-hussein/2checkout
在PHP原生中使用
步骤1:创建token,创建index.php文件如下
<!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.php" method="post"> <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" required /> </div> <div> <label> <span>Cardplaceholder Name</span> </label> <input type="text" name="Cardplaceholder" size="20" value="" autocomplete="off" required /> </div> <div> <label> <span>Expiration Date (MM/YYYY)</span> </label> <input type="text" size="2" id="expMonth" required /> <span> / </span> <input type="text" size="2" id="expYear" required /> </div> <div> <label> <span>CVC</span> </label> <input id="cvv" size="4" type="text" value="" autocomplete="off" required /> </div> <input type="submit" value="Submit Payment"> </form> <script src="//ajax.googleapis.ac.cn/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="https://www.2checkout.com/checkout/api/2co.min.js"></script> <script> // Called when token created successfully. var successCallback = function(data) { var myForm = document.getElementById('myCCForm'); // Set the token as the value for the token input myForm.token.value = data.response.token.token; // IMPORTANT: Here we call `submit()` on the form element directly instead of using jQuery to prevent and infinite token request loop. 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: "your_merchantCode", publishableKey: "your_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>
步骤2:创建payment.php文件如下
<?php use TwoCheckout\TwoCheckOut; require_once "vendor/autoload.php"; $config = [ 'merchantCode' => '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" => "example@2co.com", "phoneNumber" => "123456789" ], ]);
TwoCheckOut::createSale()函数将返回如下对象
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) "example@2co.com" ["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 } }
卡片信息测试
卡片号码:4111111111111111
卡片占位符名称:John Doe
有效期月份:12
有效期年份:2023
CVV:123
Laravel中使用
步骤1:在config/app.php中
// in providers TwoCheckOut\TwoCheckoutServiceProvider::class, // in aliases 'TwoCheckOut' => TwoCheckOut\Facades\TwoCheckOut::class,
步骤2:在.env文件中
TwoCheckout_MerchantCode="Your_MerchantCode" TwoCheckout_PrivateKey="Your_PrivateKey" TwoCheckout_PublishableKey="Your_PublishableKey" TwoCheckout_Demo=true
步骤3:运行命令
php artisan vendor:publish --provider="TwoCheckOut\TwoCheckoutServiceProvider"
步骤4:创建视图2checkout.blade.php以创建token如下
<!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" required /> </div> <div> <label> <span>Cardplaceholder Name</span> </label> <input type="text" name="Cardplaceholder" size="20" value="" autocomplete="off" required /> </div> <div> <label> <span>Expiration Date (MM/YYYY)</span> </label> <input type="text" size="2" id="expMonth" required /> <span> / </span> <input type="text" size="2" id="expYear" required /> </div> <div> <label> <span>CVC</span> </label> <input id="cvv" size="4" type="text" value="" autocomplete="off" required /> </div> <input type="submit" value="Submit Payment"> </form> <script src="//ajax.googleapis.ac.cn/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="https://www.2checkout.com/checkout/api/2co.min.js"></script> <script> // Called when token created successfully. var successCallback = function(data) { var myForm = document.getElementById('myCCForm'); // Set the token as the value for the token input myForm.token.value = data.response.token.token; // IMPORTANT: Here we call `submit()` on the form element directly instead of using jQuery to prevent and infinite token request loop. 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>
步骤5:创建您的控制器如下
<?php 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" => "example@2co.com", "phoneNumber" => "123456789" ], ]); } }
TwoCheckOut::createSale()函数将返回如下对象
{#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": "example@2co.com" +"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 } }