How to build a payment gateway using PHP and its framework

Maniar Dhruvin

Maniar Dhruvin, B.Tech from Institute Of Technology, Nirma University

Répondu il y a 34w · L'auteur dispose de réponses 102 et de vues de réponses 99.7k

Creating a Token

Open the ‘index.html’ file that we created, and add a basic credit card form that allows our buyer to enter in their card number, expiration month and year and CVC.

Notice that we have a no ‘name’ attributes on the input elements that collect the credit card information. This will ensure that no sensitive card data touches your server when the form is submitted. Also, we include a hidden input element for the token which we will submit to our server to make the authorization request.

Now we can add our JavaScript to make the token request call. Replace ‘sandbox-seller-id’ and ‘sandbox-publishable-key’ with your credentials.

Let’s take a second to look at what we did here. First we pulled in a jQuery library to help us with manipulating the document. (The 2co.js library does NOT require jQuery.)

Next we pulled in the 2co.js library so that we can make our token request with the card details.

This library provides us with 2 functions, one to load the public encryption key, and one to make the token request.

The ‘TCO.loadPubKey(String environment, Function callback)’’ function must be used to asynchronously load the public encryption key for the ‘production’ or ‘sandbox’ environment. In this example, we are going to call this as soon as the document is ready so it is not necessary to provide a callback.

TCO.loadPubKey('sandbox');

The the ‘TCO.requestToken(Function callback, Function callback, Object arguments)’ function is used to make the token request. This function takes 3 arguments:

  • Your success callback function which accepts one argument and will be called when the request is successful.
  • Your error callback function which accepts one argument and will be called when the request results in an error.
  • An object containing the credit card details and your credentials.
  • sellerId : 2Checkout account number
  • publishableKey : Payment API publishable key
  • ccNo : Credit Card Number
  • expMonth : Card Expiration Month
  • expYear : Card Expiration Year
  • Cvv : Card Verification Code
Leggi:  Quels sont les avantages des applications de m-commerce?

TCO.requestToken(successCallback, errorCallback, args);

In our example we created ‘tokenRequest’ function to setup our arguments by pulling the values entered on the credit card form and we make the token request.

var tokenRequest = function() {// Setup token request argumentsvar args = {sellerId: "sandbox-seller-id",publishableKey: "sandbox-publishable-key,ccNo: $("#ccNo").val(),cvv: $("#cvv").val(),expMonth: $("#expMonth").val(),expYear: $("#expYear").val()};// Make the token requestTCO.requestToken(successCallback, errorCallback, args);};

We then call this function from a submit handler function that we setup on the form.

$("#myCCForm").submit(function(e) {// Call our token request functiontokenRequest();// Prevent form from submittingreturn false;});

The ‘successCallback’ function is called if the token request is successful. In this function we set the token as the value for our ‘token’ hidden input element and we submit the form to our server.

var successCallback = function(data) {var myForm = document.getElementById('myCCForm');// Set the token as the value for the token inputmyForm.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();};

The ‘errorCallback’ function is called if the token request fails. In our example function, we check for error code 200, which indicates that the ajax call has failed. If the error code was 200, we automatically re-attempt the tokenization, otherwise, we alert with the error message.

var errorCallback = function(data) {if (data.errorCode === 200) {tokenRequest();} else {alert(data.errorMsg);}};


Create the Sale

Once the credit card information has been tokenized and passed to the correct page, we can make the authorization call. For this example, this will take place in the payment.php file.

First thing is to include the PHP library and set your credentials. Replace ‘sandbox-seller-id’ and ‘sandbox-private-key’ with your credentials.

Leggi:  Comment accepter les cartes de crédit avec PayPal

require_once("2checkout-php/lib/Twocheckout.php");Twocheckout::privateKey('sandbox-private-key');Twocheckout::sellerId('sandbox-seller-id);Twocheckout::sandbox(true);

These values are the credentials from your sandbox test account. To use with your production account, you would need to use your production credentials, and set the sandbox value to false.

Then, we’ll need to create the array with our sale parameters, and submit it for authorization. For this example, we are creating an intangible order for $10.

try {$charge = Twocheckout_Charge::auth(array("merchantOrderId" => "123","token" => $_POST['token'],"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')));if ($charge['response']['responseCode'] == 'APPROVED') {echo "Thanks for your Order!";echo "

Return Parameters:

";echo "

";print_r($charge);echo "

";}} catch (Twocheckout_Error $e) {print_r($e->getMessage());}

Notice that we are using the token that is being posted to this page. Most likely, you would also want to populate the rest of these parameters from your customer’s information but for the sake of this tutorial, we have hard coded the values in.

Once the authorization call has been made, we check if the authorization is successful. If it is, we are outputting a message to the buyer stating this fact. In this example we are also outputting all of the return parameters from the sale so you can view the return structure.

2Checkout also can throw errors from the authorization call, so the entire call is included in a try-catch block, to find and handle any authorization errors.

Important note: a token can only be used for one authorization call, and will expire after 30 minutes if not used.

And that’s it! Implement the code as shown, and you should have a working Payment API example.


Run the example application

In your browser, navigate to the ‘payment-api’ directory in either your server or your localhost, and you should see a payment form where you can enter credit card information.

Leggi:  Where can I buy the best ethereum mining rig online?

For your testing, you can use these values for a successful authorization

Credit Card Number: 4000000000000002

Expiration date: 10/2020

cvv: 123

And these values for a failed authorization:

Credit Card Number: 4333433343334333

Expiration date: 10/2020

cvv:123

Reference : Traitement des paiements en ligne | Accepter les paiements en ligne | 2Checkout

Narasimhan M.G.

Narasimhan M.G., 30+ years as Enterprise Architect

Répondu il y a 34w · L'auteur dispose de réponses 792 et de vues de réponses 310.4k

Search github

A2A.

Krunal Lathiya

Krunal Lathiya, BE Blogger & Web Development, VVP Engineering College, Rajkot (2015)

Répondu il y a 34w

If you are using let’s say Laravel PHP Framework then there are lots of packages, you can install through Composer - A PHP Dependency Manager.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.