M2_AuthorizeNetDPM

A class that demonstrates the DPM method.

Defined (1)

The class is defined in the following location(s).

/lib/authorize.net/lib/AuthorizeNetDPM.php  
  1. class M2_AuthorizeNetDPM extends M2_AuthorizeNetSIM_Form 
  2.  
  3. const LIVE_URL = 'https://secure2.authorize.net/gateway/transact.dll'; 
  4. const SANDBOX_URL = 'https://test.authorize.net/gateway/transact.dll'; 
  5.  
  6. /** 
  7. * Implements all 3 steps of the Direct Post Method for demonstration 
  8. * purposes. 
  9. */ 
  10. public static function directPostDemo($url, $api_login_id, $transaction_key, $amount = "0.00", $md5_setting = "") 
  11.  
  12. // Step 1: Show checkout form to customer. 
  13. if (!count($_POST) && !count($_GET)) 
  14. $fp_sequence = time(); // Any sequential number like an invoice number. 
  15. echo M2_AuthorizeNetDPM::getCreditCardForm($amount, $fp_sequence, $url, $api_login_id, $transaction_key); 
  16. // Step 2: Handle AuthorizeNet Transaction Result & return snippet. 
  17. elseif (count($_POST))  
  18. $response = new M2_AuthorizeNetSIM($api_login_id, $md5_setting); 
  19. if ($response->isAuthorizeNet()) 
  20. if ($response->approved)  
  21. // Do your processing here. 
  22. $redirect_url = $url . '?response_code=1&transaction_id=' . $response->transaction_id;  
  23. else 
  24. // Redirect to error page. 
  25. $redirect_url = $url . '?response_code='.$response->response_code . '&response_reason_text=' . $response->response_reason_text; 
  26. // Send the Javascript back to AuthorizeNet, which will redirect user back to your site. 
  27. echo M2_AuthorizeNetDPM::getRelayResponseSnippet($redirect_url); 
  28. else 
  29. echo "Error -- not AuthorizeNet. Check your MD5 Setting."; 
  30. // Step 3: Show receipt page to customer. 
  31. elseif (!count($_POST) && count($_GET)) 
  32. if ($_GET['response_code'] == 1) 
  33. echo "Thank you for your purchase! Transaction id: " . htmlentities($_GET['transaction_id']); 
  34. else 
  35. echo "Sorry, an error occurred: " . htmlentities($_GET['response_reason_text']); 
  36.  
  37. /** 
  38. * A snippet to send to AuthorizeNet to redirect the user back to the 
  39. * merchant's server. Use this on your relay response page. 
  40. * @param string $redirect_url Where to redirect the user. 
  41. * @return string 
  42. */ 
  43. public static function getRelayResponseSnippet($redirect_url) 
  44. return "<html><head><script language=\"javascript\"> 
  45. <!-- 
  46. window.location=\"{$redirect_url}\"; 
  47. //--> 
  48. </script> 
  49. </head><body><noscript><meta http-equiv=\"refresh\" content=\"1;url={$redirect_url}\"></noscript></body></html>"; 
  50.  
  51. /** 
  52. * Generate a sample form for use in a demo Direct Post implementation. 
  53. * @param string $amount Amount of the transaction. 
  54. * @param string $fp_sequence Sequential number(ie. Invoice #) 
  55. * @param string $relay_response_url The Relay Response URL 
  56. * @param string $api_login_id Your API Login ID 
  57. * @param string $transaction_key Your API Tran Key. 
  58. * @param bool $test_mode Use the sandbox? 
  59. * @param bool $prefill Prefill sample values(for test purposes). 
  60. * @return string 
  61. */ 
  62. public static function getCreditCardForm($amount, $fp_sequence, $relay_response_url, $api_login_id, $transaction_key, $test_mode = true, $prefill = true) 
  63. $time = time(); 
  64. $fp = self::getFingerprint($api_login_id, $transaction_key, $amount, $fp_sequence, $time); 
  65. $sim = new M2_AuthorizeNetSIM_Form( 
  66. array( 
  67. 'x_amount' => $amount,  
  68. 'x_fp_sequence' => $fp_sequence,  
  69. 'x_fp_hash' => $fp,  
  70. 'x_fp_timestamp' => $time,  
  71. 'x_relay_response'=> "TRUE",  
  72. 'x_relay_url' => $relay_response_url,  
  73. 'x_login' => $api_login_id,  
  74. ); 
  75. $hidden_fields = $sim->getHiddenFieldString(); 
  76. $post_url = ($test_mode ? self::SANDBOX_URL : self::LIVE_URL); 
  77.  
  78. $form = ' 
  79. <style> 
  80. fieldset { 
  81. overflow: auto; 
  82. border: 0; 
  83. margin: 0; 
  84. padding: 0; } 
  85.  
  86. fieldset div { 
  87. float: left; } 
  88.  
  89. fieldset.centered div { 
  90. text-align: center; } 
  91.  
  92. label { 
  93. color: #183b55; 
  94. display: block; 
  95. margin-bottom: 5px; } 
  96.  
  97. label img { 
  98. display: block; 
  99. margin-bottom: 5px; } 
  100.  
  101. input.text { 
  102. border: 1px solid #bfbab4; 
  103. margin: 0 4px 8px 0; 
  104. padding: 6px; 
  105. color: #1e1e1e; 
  106. -webkit-border-radius: 5px; 
  107. -moz-border-radius: 5px; 
  108. border-radius: 5px; 
  109. -webkit-box-shadow: inset 0px 5px 5px #eee; 
  110. -moz-box-shadow: inset 0px 5px 5px #eee; 
  111. box-shadow: inset 0px 5px 5px #eee; } 
  112. .submit { 
  113. display: block; 
  114. background-color: #76b2d7; 
  115. border: 1px solid #766056; 
  116. color: #3a2014; 
  117. margin: 13px 0; 
  118. padding: 8px 16px; 
  119. -webkit-border-radius: 12px; 
  120. -moz-border-radius: 12px; 
  121. border-radius: 12px; 
  122. font-size: 14px; 
  123. -webkit-box-shadow: inset 3px -3px 3px rgba(0, 0, 0, .5), inset 0 3px 3px rgba(255, 255, 255, .5), inset -3px 0 3px rgba(255, 255, 255, .75); 
  124. -moz-box-shadow: inset 3px -3px 3px rgba(0, 0, 0, .5), inset 0 3px 3px rgba(255, 255, 255, .5), inset -3px 0 3px rgba(255, 255, 255, .75); 
  125. box-shadow: inset 3px -3px 3px rgba(0, 0, 0, .5), inset 0 3px 3px rgba(255, 255, 255, .5), inset -3px 0 3px rgba(255, 255, 255, .75); } 
  126. </style> 
  127. <form method="post" action="'.$post_url.'"> 
  128. '.$hidden_fields.' 
  129. <fieldset> 
  130. <div> 
  131. <label>Credit Card Number</label> 
  132. <input type="text" class="text" size="15" name="x_card_num" value="'.($prefill ? '6011000000000012' : '').'"></input> 
  133. </div> 
  134. <div> 
  135. <label>Exp.</label> 
  136. <input type="text" class="text" size="4" name="x_exp_date" value="'.($prefill ? '04/17' : '').'"></input> 
  137. </div> 
  138. <div> 
  139. <label>CCV</label> 
  140. <input type="text" class="text" size="4" name="x_card_code" value="'.($prefill ? '782' : '').'"></input> 
  141. </div> 
  142. </fieldset> 
  143. <fieldset> 
  144. <div> 
  145. <label>First Name</label> 
  146. <input type="text" class="text" size="15" name="x_first_name" value="'.($prefill ? 'John' : '').'"></input> 
  147. </div> 
  148. <div> 
  149. <label>Last Name</label> 
  150. <input type="text" class="text" size="14" name="x_last_name" value="'.($prefill ? 'Doe' : '').'"></input> 
  151. </div> 
  152. </fieldset> 
  153. <fieldset> 
  154. <div> 
  155. <label>Address</label> 
  156. <input type="text" class="text" size="26" name="x_address" value="'.($prefill ? '123 Main Street' : '').'"></input> 
  157. </div> 
  158. <div> 
  159. <label>City</label> 
  160. <input type="text" class="text" size="15" name="x_city" value="'.($prefill ? 'Boston' : '').'"></input> 
  161. </div> 
  162. </fieldset> 
  163. <fieldset> 
  164. <div> 
  165. <label>State</label> 
  166. <input type="text" class="text" size="4" name="x_state" value="'.($prefill ? 'MA' : '').'"></input> 
  167. </div> 
  168. <div> 
  169. <label>Zip Code</label> 
  170. <input type="text" class="text" size="9" name="x_zip" value="'.($prefill ? '02142' : '').'"></input> 
  171. </div> 
  172. <div> 
  173. <label>Country</label> 
  174. <input type="text" class="text" size="22" name="x_country" value="'.($prefill ? 'US' : '').'"></input> 
  175. </div> 
  176. </fieldset> 
  177. <input type="submit" value="BUY" class="submit buy"> 
  178. </form>'; 
  179. return $form; 
  180.