In today’s, tutorial I will explain how to integrate the CyberSource payment gateway in your PHP application. I have created a simple soap API to charge Credit Card from the payment gateway and it will return the success or failure messages as a return from the gateway.
Cybersource is a global payment gateway which provides credit card payment gateway and CyberSource fraud detection for online payments. It was founded in 1994 and acquired small business payment services provider, Authorize.Net, in November 2007. By using CyberSource you can enable to accept payments from the globe and also can migrate the fraud detection to secure online payments. Below diagram will explain to you how CyberSource payment gateway work and It is based on a simple order API.
Download the “cybersource-sdk-php” from GitHub and unzip on your web directory. Now go to the cybersource-sdk-php/samples folder and create a new file simpleorderapi.php
file. copy paste below code to test test payment.
You can also install CyberSource payment using composer. create a folder in your web directory and create a composer.json file. Add dependency in the composer.json file.
1 2 3 | "require": { "cybersource/sdk-php": "*" }, |
Please make sure you have already installed composer in your system. If you have not installed, then download from the composer official web site. After that open window command prompt and go to the folder where you have kept you, composer.json file and run below command.
1 | composer.phar install |
It will automatically download the cybersource-sdk-php
in your local folder and then follow the below steps.
Before running your script, you need to create sandbox account on CyberSource for transation_key and merchant_id for test payment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | <?php /** * Test script to show how the EPS_CYBERSOURCE class can be used. */ require 'class.eps_cybersource.php'; /** * These should go in a config file somewhere on the box. */ $trans_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $merchant_id = 'xxxxxxxxxxxxx'; $url = 'https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.53.wsdl'; /** * These are sent from some GUI and assembled into the applicable arrays. */ $bill_array = array('firstName'=>'Prem','lastName'=>'Tiwari','street1'=>'1295 Charleston Road', 'city'=>'Mountain View','state'=>'CA','postalCode' => '94043','country'=>'US', $card_array = array('accountNumber'=>'4111111111111111','expirationMonth'=>'12', 'expirationYear'=>'2020','cvNumber'=>'123'); $custom_array = array('one','two','three','four'); /** * Authorize a transaction. */ try { $soap = new EPS_CYBERSOURCE($url, $merchant_id,$trans_key); $soap->setMerchantDefinedData($custom_array); $soap->setReferenceCode(array('CSTEST','YYYY','J','-','RNDM')); $soap->setCCRequest($bill_array,$card_array,$item_array); $soap->ccAuthorize(); } catch (SoapFault $e) { exit($e->getMessage()); } print_r($soap->reply); /** * Capture the successful authorization. * A single ccCapture() could have been done instead of a ccAuthorize() followed by a ccCapture(). */ if ($soap->success) $soap->ccCapture(); /** * These return values would be stored locally. */ $tok = $soap->reply->requestToken; $id = $soap->reply->requestID; $rc = $soap->reply->merchantReferenceCode; $amount = $soap->reply->amount; $currency = $soap->reply->currency; print_r($soap->reply); $trans_array = array('requestToken'=>$tok, 'requestID'=>$id, 'referenceCode'=>$rc, 'amount'=>$amount, 'currency'=>$currency); /** * Reverse the capture or authorization. */ if ($soap->success) { unset($soap); try { $soap = new EPS_CYBERSOURCE($url, $merchant_id,$trans_key); $soap->setCCReversalRequest($tok,$id,$rc,$amount); $soap->ccReverse('c'); } catch (SoapFault $e) { exit($e->getMessage()); } } print_r($soap->reply); /** * Credit the account. */ if ($soap->success) { unset($soap); try { $soap = new EPS_CYBERSOURCE($url, $merchant_id,$trans_key); $soap->setCCCreditRequest($bill_array,$card_array); $soap->setReferenceCode(array('CR','YYYY','J','-',array('RNDM',5,5))); $soap->ccCredit('1.50'); } catch (SoapFault $e) { exit($e->getMessage()); } } print_r($soap->reply); /** * Get some help on the XML schema. */ print_r($soap->getHelp()); echo "current version: " . $soap->getHelpVersion() . "\n"; print_r($soap->getHelp('item')); unset($soap); ?> |
If you still need any help to integrate CyberSource payment gateway in your application , then feel free to add your comments in below comment section. I will be happy to help to integrate CyberSource payment gateway in your website. Do you like & share this article with your friends, and don’t forget to subscribe our newsletter and follow us on Facebook and Twitter to learn cool tutorials.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: credit card payment gateway, cybersource credit card, cybersource gateway, cybersource payment, cybersource payment gateway, cybersource php sdk