Posted By


mafhh14 on 07/29/11

Tagged


Statistics


Viewed 899 times
Favorited by 0 user(s)

PHP – Cybersource Payment Gateway


/ Published in: PHP
Save to your folder(s)

This PHP class can be used to process credit card payments via Cybersource.

It can send HTTP requests to Cybersource SOAP Web services API server to perform several types
of operations to process credit card payments.

Currently it can request a payment authorization, capture the result of a payment request and
request the reversal of a previous payment request.

This class would require php_soap and php_openssl extensions.


Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  * Test script to show how the EPS_CYBERSOURCE class can be used.
  4.  */
  5.  
  6. require 'class.eps_cybersource.php';
  7.  
  8. /**
  9.  * These should go in a config file somewhere on the box.
  10.  */
  11. $trans_key = 'your SOAP transaction key';
  12. $merchant_id = 'your merchant id';
  13. $url = 'https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.53.wsdl';
  14.  
  15. /**
  16.  * These are sent from some GUI and assembled into the applicable arrays.
  17.  */
  18. $bill_array = array('firstName'=>'John','lastName'=>'Doe','street1'=>'1295 Charleston Road',
  19. 'city'=>'Mountain View','state'=>'CA','postalCode' => '94043','country'=>'US',
  20. 'email'=> '[email protected]','ipaddress'=>'10.7.111.111');
  21. $card_array = array('accountNumber'=>'4111111111111111','expirationMonth'=>'12',
  22. 'expirationYear'=>'2020','cvNumber'=>'123');
  23. $item_array = array(
  24. array('unitPrice'=>'.50','quantity'=>2,'productName'=>'product one'),
  25. array('unitPrice'=>'2.5','quantity'=>1,'productName'=>'product two'));
  26. $custom_array = array('one','two','three','four');
  27.  
  28. /**
  29.  * Authorize a transaction.
  30.  */
  31. try
  32. {
  33. $soap = new EPS_CYBERSOURCE($url, $merchant_id,$trans_key);
  34. $soap->setMerchantDefinedData($custom_array);
  35. $soap->setReferenceCode(array('CSTEST','YYYY','J','-','RNDM'));
  36. $soap->setCCRequest($bill_array,$card_array,$item_array);
  37. $soap->ccAuthorize();
  38. }
  39. catch (SoapFault $e)
  40. {
  41. exit($e->getMessage());
  42. }
  43.  
  44. print_r($soap->reply);
  45.  
  46. /**
  47.  * Capture the successful authorization.
  48.  * A single ccCapture() could have been done instead of a ccAuthorize() followed by a ccCapture().
  49.  */
  50. if ($soap->success) $soap->ccCapture();
  51.  
  52. /**
  53.  * These return values would be stored locally.
  54.  */
  55. $tok = $soap->reply->requestToken;
  56. $id = $soap->reply->requestID;
  57. $rc = $soap->reply->merchantReferenceCode;
  58. $amount = $soap->reply->amount;
  59. $currency = $soap->reply->currency;
  60.  
  61. print_r($soap->reply);
  62.  
  63. $trans_array = array('requestToken'=>$tok,
  64. 'requestID'=>$id,
  65. 'referenceCode'=>$rc,
  66. 'amount'=>$amount,
  67. 'currency'=>$currency);
  68.  
  69. /**
  70.  * Reverse the capture or authorization.
  71.  */
  72. if ($soap->success)
  73. {
  74. unset($soap);
  75. try
  76. {
  77. $soap = new EPS_CYBERSOURCE($url, $merchant_id,$trans_key);
  78. $soap->setCCReversalRequest($tok,$id,$rc,$amount);
  79. $soap->ccReverse('c');
  80. }
  81. catch (SoapFault $e)
  82. {
  83. exit($e->getMessage());
  84. }
  85. }
  86.  
  87. print_r($soap->reply);
  88.  
  89. /**
  90.  * Credit the account.
  91.  */
  92. if ($soap->success)
  93. {
  94. unset($soap);
  95. try
  96. {
  97. $soap = new EPS_CYBERSOURCE($url, $merchant_id,$trans_key);
  98. $soap->setCCCreditRequest($bill_array,$card_array);
  99. $soap->setReferenceCode(array('CR','YYYY','J','-',array('RNDM',5,5)));
  100. $soap->ccCredit('2.53');
  101. }
  102. catch (SoapFault $e)
  103. {
  104. exit($e->getMessage());
  105. }
  106. }
  107.  
  108. print_r($soap->reply);
  109.  
  110. /**
  111.  * Get some help on the XML schema.
  112.  */
  113. print_r($soap->getHelp());
  114.  
  115. echo "current version: " . $soap->getHelpVersion() . "\n";
  116.  
  117. print_r($soap->getHelp('item'));
  118.  
  119. unset($soap);
  120.  
  121. ?>

URL: http://www.phpmoot.com/php-cybersource-payment-gateway/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.