Advertisement
Guest User

Untitled

a guest
Jul 17th, 2013
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.02 KB | None | 0 0
  1. // API Setup perameters
  2. $gatewayURL = 'https://secure.nmi.com/api/v2/three-step';
  3. $APIKey = 'qunjYJExVZX2584dkjt8vS6Vr5bbM54g';
  4.  
  5.  
  6. // If there is no POST data or a token-id, print the initial shopping cart form to get ready for Step One.
  7. if (empty($_POST['DO_STEP_1']) && empty($_GET['token-id'])) {
  8.  
  9. print ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
  10. print '
  11. <html>
  12. <head>
  13. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  14. <title>Collect non-sensitive Customer Info </title>
  15. </head>
  16. <body>
  17. <p><h2>Step One: Collect non-sensitive payment information.<br /></h2></p>
  18.  
  19. <h3> Customer Information</h3>
  20. <h4> Billing Details</h4>
  21.  
  22. <form action="" method="post">
  23. <table>
  24. <tr><td>Customer Vault Id </td><td><input type="text" name="customer-vault-id" value=""></td></tr>
  25. <tr><td>Company</td><td><input type="text" name="billing-address-company" value="Acme, Inc."></td></tr>
  26. <tr><td>First Name </td><td><input type="text" name="billing-address-first-name" value="John"></td></tr>
  27. <tr><td>Last Name </td><td><input type="text" name="billing-address-last-name" value="Smith"></td></tr>
  28. <tr><td>Address </td><td><input type="text" name="billing-address-address1" value="1234 Main St."></td></tr>
  29. <tr><td>Address 2 </td><td><input type="text" name="billing-address-address2" value="Suite 205"></td></tr>
  30. <tr><td>City </td><td><input type="text" name="billing-address-city" value="Beverly Hills"></td></tr>
  31. <tr><td>State/Province </td><td><input type="text" name="billing-address-state" value="CA"></td></tr>
  32. <tr><td>Zip/Postal </td><td><input type="text" name="billing-address-zip" value="90210"></td></tr>
  33. <tr><td>Country </td><td><input type="text" name="billing-address-country" value="US"></td></tr>
  34. <tr><td>Phone Number </td><td><input type="text" name="billing-address-phone" value="555-555-5555"></td></tr>
  35. <tr><td>Fax Number </td><td><input type="text" name="billing-address-fax" value="555-555-5555"></td></tr>
  36. <tr><td>Email Address </td><td><input type="text" name="billing-address-email" value="[email protected]"></td></tr>
  37.  
  38. <tr><td><h4><br /> Shipping Details</h4>
  39. <tr><td>Company</td><td><input type="text" name="shipping-address-company" value="Acme, Inc."></td></tr>
  40. <tr><td>First Name </td><td><input type="text" name="shipping-address-first-name" value="Mary"></td></tr>
  41. <tr><td>Last Name </td><td><input type="text" name="shipping-address-last-name" value="Smith"></td></tr>
  42. <tr><td>Address </td><td><input type="text" name="shipping-address-address1" value="1234 Main St."></td></tr>
  43. <tr><td>Address 2</td><td><input type="text" name="shipping-address-address2" value="Suite 205"></td></tr>
  44. <tr><td>City </td><td><input type="text" name="shipping-address-city" value="Beverly Hills"></td></tr>
  45. <tr><td>State/Province </td><td><input type="text" name="shipping-address-state" value="CA"></td></tr>
  46. <tr><td>Zip/Postal </td><td><input type="text" name="shipping-address-zip" value="90210"></td></tr>
  47. <tr><td>Country</td><td><input type="text" name="shipping-address-country" value="US"></td></tr>
  48. <tr><td>Phone Number </td><td><input type="text" name="shipping-address-phone" value="555-555-5555"></td></tr>
  49. <tr><td colspan="2"> </td>
  50. <tr><td colspan="2" align=center>Total Amount $12.00 </td></tr>
  51. <tr><td colspan="2" align=center><input type="submit" value="Submit Step One"><input type="hidden" name ="DO_STEP_1" value="true"></td></tr>
  52. </table>
  53.  
  54. </form>
  55. </body>
  56. </html>
  57.  
  58. ';
  59. }else if (!empty($_POST['DO_STEP_1'])) {
  60.  
  61. // Initiate Step One: Now that we've collected the non-sensitive payment information, we can combine other order information and build the XML format.
  62. $xmlRequest = new DOMDocument('1.0','UTF-8');
  63.  
  64. $xmlRequest->formatOutput = true;
  65. $xmlSale = $xmlRequest->createElement('sale');
  66.  
  67. // Amount, authentication, and Redirect-URL are typically the bare mininum.
  68. appendXmlNode($xmlSale,'api-key',$APIKey);
  69. appendXmlNode($xmlSale,'redirect-url',$_SERVER['HTTP_REFERER']);
  70. appendXmlNode($xmlSale, 'amount', '12.00');
  71. appendXmlNode($xmlSale, 'ip-address', $_SERVER["REMOTE_ADDR"]);
  72. //appendXmlNode($xmlSale, 'processor-id' , 'processor-a');
  73. appendXmlNode($xmlSale, 'currency', 'USD');
  74. appendXmlNode($xmlSale, 'dup-seconds' , '2');
  75.  
  76. // Some additonal fields may have been previously decided by user
  77. appendXmlNode($xmlSale, 'order-id', '1234');
  78. appendXmlNode($xmlSale, 'order-description', 'Small Order');
  79. appendXmlNode($xmlSale, 'merchant-defined-field-1' , 'Red');
  80. appendXmlNode($xmlSale, 'merchant-defined-field-2', 'Medium');
  81. appendXmlNode($xmlSale, 'tax-amount' , '0.00');
  82. appendXmlNode($xmlSale, 'shipping-amount' , '0.00');
  83.  
  84. /*if(!empty($_POST['customer-vault-id'])) {
  85. appendXmlNode($xmlSale, 'customer-vault-id' , $_POST['customer-vault-id']);
  86. }else {
  87. $xmlAdd = $xmlRequest->createElement('add-customer');
  88. appendXmlNode($xmlAdd, 'customer-vault-id' ,411);
  89. $xmlSale->appendChild($xmlAdd);
  90. }*/
  91.  
  92.  
  93. // Set the Billing and Shipping from what was collected on initial shopping cart form
  94. $xmlBillingAddress = $xmlRequest->createElement('billing');
  95. appendXmlNode($xmlBillingAddress,'first-name', $_POST['billing-address-first-name']);
  96. appendXmlNode($xmlBillingAddress,'last-name', $_POST['billing-address-last-name']);
  97. appendXmlNode($xmlBillingAddress,'address1', $_POST['billing-address-address1']);
  98. appendXmlNode($xmlBillingAddress,'city', $_POST['billing-address-city']);
  99. appendXmlNode($xmlBillingAddress,'state', $_POST['billing-address-state']);
  100. appendXmlNode($xmlBillingAddress,'postal', $_POST['billing-address-zip']);
  101. //billing-address-email
  102. appendXmlNode($xmlBillingAddress,'country', $_POST['billing-address-country']);
  103. appendXmlNode($xmlBillingAddress,'email', $_POST['billing-address-email']);
  104.  
  105. appendXmlNode($xmlBillingAddress,'phone', $_POST['billing-address-phone']);
  106. appendXmlNode($xmlBillingAddress,'company', $_POST['billing-address-company']);
  107. appendXmlNode($xmlBillingAddress,'address2', $_POST['billing-address-address2']);
  108. appendXmlNode($xmlBillingAddress,'fax', $_POST['billing-address-fax']);
  109. $xmlSale->appendChild($xmlBillingAddress);
  110.  
  111.  
  112. $xmlShippingAddress = $xmlRequest->createElement('shipping');
  113. appendXmlNode($xmlShippingAddress,'first-name', $_POST['shipping-address-first-name']);
  114. appendXmlNode($xmlShippingAddress,'last-name', $_POST['shipping-address-last-name']);
  115. appendXmlNode($xmlShippingAddress,'address1', $_POST['shipping-address-address1']);
  116. appendXmlNode($xmlShippingAddress,'city', $_POST['shipping-address-city']);
  117. appendXmlNode($xmlShippingAddress,'state', $_POST['shipping-address-state']);
  118. appendXmlNode($xmlShippingAddress,'postal', $_POST['shipping-address-zip']);
  119. appendXmlNode($xmlShippingAddress,'country', $_POST['shipping-address-country']);
  120. appendXmlNode($xmlShippingAddress,'phone', $_POST['shipping-address-phone']);
  121. appendXmlNode($xmlShippingAddress,'company', $_POST['shipping-address-company']);
  122. appendXmlNode($xmlShippingAddress,'address2', $_POST['shipping-address-address2']);
  123. $xmlSale->appendChild($xmlShippingAddress);
  124.  
  125.  
  126. // Products already chosen by user
  127. $xmlProduct = $xmlRequest->createElement('product');
  128. appendXmlNode($xmlProduct,'product-code' , 'SKU-123456');
  129. appendXmlNode($xmlProduct,'description' , 'test product description');
  130. appendXmlNode($xmlProduct,'commodity-code' , 'abc');
  131. appendXmlNode($xmlProduct,'unit-of-measure' , 'lbs');
  132. appendXmlNode($xmlProduct,'unit-cost' , '5.00');
  133. appendXmlNode($xmlProduct,'quantity' , '1');
  134. appendXmlNode($xmlProduct,'total-amount' , '7.00');
  135. appendXmlNode($xmlProduct,'tax-amount' , '2.00');
  136.  
  137. appendXmlNode($xmlProduct,'tax-rate' , '1.00');
  138. appendXmlNode($xmlProduct,'discount-amount', '2.00');
  139. appendXmlNode($xmlProduct,'discount-rate' , '1.00');
  140. appendXmlNode($xmlProduct,'tax-type' , 'sales');
  141. appendXmlNode($xmlProduct,'alternate-tax-id' , '12345');
  142.  
  143. $xmlSale->appendChild($xmlProduct);
  144.  
  145. $xmlProduct = $xmlRequest->createElement('product');
  146. appendXmlNode($xmlProduct,'product-code' , 'SKU-123456');
  147. appendXmlNode($xmlProduct,'description' , 'test 2 product description');
  148. appendXmlNode($xmlProduct,'commodity-code' , 'abc');
  149. appendXmlNode($xmlProduct,'unit-of-measure' , 'lbs');
  150. appendXmlNode($xmlProduct,'unit-cost' , '2.50');
  151. appendXmlNode($xmlProduct,'quantity' , '2');
  152. appendXmlNode($xmlProduct,'total-amount' , '7.00');
  153. appendXmlNode($xmlProduct,'tax-amount' , '2.00');
  154.  
  155. appendXmlNode($xmlProduct,'tax-rate' , '1.00');
  156. appendXmlNode($xmlProduct,'discount-amount', '2.00');
  157. appendXmlNode($xmlProduct,'discount-rate' , '1.00');
  158. appendXmlNode($xmlProduct,'tax-type' , 'sales');
  159. appendXmlNode($xmlProduct,'alternate-tax-id' , '12345');
  160.  
  161. $xmlSale->appendChild($xmlProduct);
  162.  
  163. $xmlRequest->appendChild($xmlSale);
  164.  
  165. // Process Step One: Submit all transaction details to the Payment Gateway except the customer's sensitive payment information.
  166. // The Payment Gateway will return a variable form-url.
  167. $data = sendXMLviaCurl($xmlRequest,$gatewayURL);
  168.  
  169. // Parse Step One's XML response
  170. $gwResponse = @new SimpleXMLElement($data);
  171. if ((string)$gwResponse->result ==1 ) {
  172. // The form url for used in Step Two below
  173. $formURL = $gwResponse->{'form-url'};
  174. } else {
  175. throw New Exception(print " Error, received " . $data);
  176. }
  177.  
  178. // Initiate Step Two: Create an HTML form that collects the customer's sensitive payment information
  179. // and use the form-url that the Payment Gateway returns as the submit action in that form.
  180. print ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
  181.  
  182.  
  183. print '
  184.  
  185. <html>
  186. <head>
  187. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  188. <title>Collect sensitive Customer Info </title>
  189. </head>
  190. <body>';
  191. // Uncomment the line below if you would like to print Step One's response
  192. // print '<pre>' . (htmlentities($data)) . '</pre>';
  193. print '
  194. <p><h2>Step Two: Collect sensitive payment information and POST directly to payment gateway<br /></h2></p>
  195.  
  196. <form action="'.$formURL. '" method="POST">
  197. <h3> Payment Information</h3>
  198. <table>
  199. <tr><td>Credit Card Number</td><td><INPUT type ="text" name="billing-cc-number" value="4111111111111111"> </td></tr>
  200. <tr><td>Expiration Date</td><td><INPUT type ="text" name="billing-cc-exp" value="1012"> </td></tr>
  201. <tr><td>CVV</td><td><INPUT type ="text" name="cvv" > </td></tr>
  202. <tr><Td colspan="2" align=center><INPUT type ="submit" value="Submit Step Two"></td> </tr>
  203. </table>
  204. </form>
  205. </body>
  206. </html>
  207. ';
  208.  
  209. } elseif (!empty($_GET['token-id'])) {
  210.  
  211. // Step Three: Once the browser has been redirected, we can obtain the token-id and complete
  212. // the transaction through another XML HTTPS POST including the token-id which abstracts the
  213. // sensitive payment information that was previously collected by the Payment Gateway.
  214. $tokenId = $_GET['token-id'];
  215. $xmlRequest = new DOMDocument('1.0','UTF-8');
  216. $xmlRequest->formatOutput = true;
  217. $xmlCompleteTransaction = $xmlRequest->createElement('complete-action');
  218. appendXmlNode($xmlCompleteTransaction,'api-key',$APIKey);
  219. appendXmlNode($xmlCompleteTransaction,'token-id',$tokenId);
  220. $xmlRequest->appendChild($xmlCompleteTransaction);
  221.  
  222.  
  223. // Process Step Three
  224. $data = sendXMLviaCurl($xmlRequest,$gatewayURL);
  225.  
  226.  
  227. $gwResponse = @new SimpleXMLElement((string)$data);
  228. print ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
  229. print '
  230. <html>
  231. <head>
  232. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  233. <title>Step Three - Complete Transaciton</title>
  234. </head>
  235. <body>';
  236.  
  237. print "
  238. <p><h2>Step Three: Script automatically completes the transaction <br /></h2></p>";
  239.  
  240. if ((string)$gwResponse->result == 1 ) {
  241. print " <p><h3> Transaction was Approved, XML response was:</h3></p>\n";
  242. print '<pre>' . (htmlentities($data)) . '</pre>';
  243.  
  244. } elseif((string)$gwResponse->result == 2) {
  245. print " <p><h3> Transaction was Declined.</h3>\n";
  246. print " Decline Description : " . (string)$gwResponse->{'result-text'} ." </p>";
  247. print " <p><h3>XML response was:</h3></p>\n";
  248. print '<pre>' . (htmlentities($data)) . '</pre>';
  249. } else {
  250. print " <p><h3> Transaction caused an Error.</h3>\n";
  251. print " Error Description: " . (string)$gwResponse->{'result-text'} ." </p>";
  252. print " <p><h3>XML response was:</h3></p>\n";
  253. print '<pre>' . (htmlentities($data)) . '</pre>';
  254. }
  255. print "</body></html>";
  256.  
  257.  
  258.  
  259. } else {
  260. print "ERROR IN SCRIPT<BR>";
  261. }
  262.  
  263.  
  264. function sendXMLviaCurl($xmlRequest,$gatewayURL) {
  265. // helper function demonstrating how to send the xml with curl
  266.  
  267.  
  268. $ch = curl_init(); // Initialize curl handle
  269. curl_setopt($ch, CURLOPT_URL, $gatewayURL); // Set POST URL
  270.  
  271. $headers = array();
  272. $headers[] = "Content-type: text/xml";
  273. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // Add http headers to let it know we're sending XML
  274. $xmlString = $xmlRequest->saveXML();
  275. curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
  276. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // Allow redirects
  277. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return into a variable
  278. curl_setopt($ch, CURLOPT_PORT, 443); // Set the port number
  279. curl_setopt($ch, CURLOPT_TIMEOUT, 15); // Times out after 15s
  280. curl_setopt($ch, CURLOPT_POST, 1);
  281. curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlString); // Add XML directly in POST
  282.  
  283. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  284.  
  285.  
  286. // This should be unset in production use. With it on, it forces the ssl cert to be valid
  287. // before sending info.
  288. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  289.  
  290. if (!($data = curl_exec($ch))) {
  291. print "curl error =>" .curl_error($ch) ."\n";
  292. throw New Exception(" CURL ERROR :" . curl_error($ch));
  293.  
  294. }
  295. curl_close($ch);
  296.  
  297. return $data;
  298. }
  299.  
  300. // Helper function to make building xml dom easier
  301. function appendXmlNode($parentNode,$name, $value) {
  302. $tempNode = new DOMElement($name,$value);
  303. $parentNode->appendChild($tempNode);
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement