PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/marketpress/marketpress-includes/plugins-gateway/authorizenet-aim.php

https://github.com/bfay/maniacal-kitten
PHP | 991 lines | 772 code | 130 blank | 89 comment | 118 complexity | bbe050f9d73e35a48a18ac3efcf60b73 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. MarketPress Authorize.net AIM Gateway Plugin
  4. Author: S H Mohanjith (Incsub)
  5. */
  6. class MP_Gateway_AuthorizeNet_AIM extends MP_Gateway_API {
  7. //private gateway slug. Lowercase alpha (a-z) and dashes (-) only please!
  8. var $plugin_name = 'authorizenet-aim';
  9. //name of your gateway, for the admin side.
  10. var $admin_name = '';
  11. //public name of your gateway, for lists and such.
  12. var $public_name = '';
  13. //url for an image for your checkout method. Displayed on checkout form if set
  14. var $method_img_url = '';
  15. //url for an submit button image for your checkout method. Displayed on checkout form if set
  16. var $method_button_img_url = '';
  17. //whether or not ssl is needed for checkout page
  18. var $force_ssl = true;
  19. //always contains the url to send payment notifications to if needed by your gateway. Populated by the parent class
  20. var $ipn_url;
  21. //whether if this is the only enabled gateway it can skip the payment_form step
  22. var $skip_form = false;
  23. //credit card vars
  24. var $API_Username, $API_Password, $API_Signature, $SandboxFlag, $returnURL, $cancelURL, $API_Endpoint, $version, $currencyCode, $locale;
  25. /****** Below are the public methods you may overwrite via a plugin ******/
  26. /**
  27. * Runs when your class is instantiated. Use to setup your plugin instead of __construct()
  28. */
  29. function on_creation() {
  30. global $mp;
  31. $settings = get_option('mp_settings');
  32. //set names here to be able to translate
  33. $this->admin_name = __('Authorize.net Checkout', 'mp');
  34. $this->public_name = __('Credit Card', 'mp');
  35. $this->method_img_url = $mp->plugin_url . 'images/credit_card.png';
  36. $this->method_button_img_url = $mp->plugin_url . 'images/cc-button.png';
  37. $this->version = "63.0"; //api version
  38. //set credit card vars
  39. if ( isset( $settings['gateways']['authorizenet-aim'] ) ) {
  40. $this->API_Username = $settings['gateways']['authorizenet-aim']['api_user'];
  41. $this->API_Password = $settings['gateways']['authorizenet-aim']['api_pass'];
  42. $this->API_Signature = $settings['gateways']['authorizenet-aim']['api_sig'];
  43. $this->currencyCode = $settings['gateways']['authorizenet-aim']['currency'];
  44. $this->locale = $settings['gateways']['authorizenet-aim']['locale'];
  45. //set api urls
  46. if (!empty($settings['gateways']['authorizenet-aim']['custom_api'])) {
  47. $this->API_Endpoint = esc_url_raw($settings['gateways']['authorizenet-aim']['custom_api']);
  48. } else if ($settings['gateways']['authorizenet-aim']['mode'] == 'sandbox') {
  49. $this->API_Endpoint = "https://test.authorize.net/gateway/transact.dll";
  50. $this->force_ssl = false;
  51. } else {
  52. $this->API_Endpoint = "https://secure.authorize.net/gateway/transact.dll";
  53. }
  54. }
  55. }
  56. /**
  57. * Return fields you need to add to the top of the payment screen, like your credit card info fields
  58. *
  59. * @param array $cart. Contains the cart contents for the current blog, global cart if $mp->global_cart is true
  60. * @param array $shipping_info. Contains shipping info and email in case you need it
  61. */
  62. function payment_form($cart, $shipping_info) {
  63. global $mp;
  64. $content = '';
  65. if (isset($_GET['cancel'])) {
  66. $content .= '<div class="mp_checkout_error">' . __('Your credit card transaction has been canceled.', 'mp') . '</div>';
  67. }
  68. $settings = get_option('mp_settings');
  69. $meta = get_user_meta($current_user->ID, 'mp_billing_info', true);
  70. $email = (!empty($_SESSION['mp_billing_info']['email'])) ? $_SESSION['mp_billing_info']['email'] : (!empty($meta['email'])?$meta['email']:$_SESSION['mp_shipping_info']['email']);
  71. $name = (!empty($_SESSION['mp_billing_info']['name'])) ? $_SESSION['mp_billing_info']['name'] : (!empty($meta['name'])?$meta['name']:$_SESSION['mp_shipping_info']['name']);
  72. $address1 = (!empty($_SESSION['mp_billing_info']['address1'])) ? $_SESSION['mp_billing_info']['address1'] : (!empty($meta['address1'])?$meta['address1']:$_SESSION['mp_shipping_info']['address1']);
  73. $address2 = (!empty($_SESSION['mp_billing_info']['address2'])) ? $_SESSION['mp_billing_info']['address2'] : (!empty($meta['address2'])?$meta['address2']:$_SESSION['mp_shipping_info']['address2']);
  74. $city = (!empty($_SESSION['mp_billing_info']['city'])) ? $_SESSION['mp_billing_info']['city'] : (!empty($meta['city'])?$meta['city']:$_SESSION['mp_shipping_info']['city']);
  75. $state = (!empty($_SESSION['mp_billing_info']['state'])) ? $_SESSION['mp_billing_info']['state'] : (!empty($meta['state'])?$meta['state']:$_SESSION['mp_shipping_info']['state']);
  76. $zip = (!empty($_SESSION['mp_billing_info']['zip'])) ? $_SESSION['mp_billing_info']['zip'] : (!empty($meta['zip'])?$meta['zip']:$_SESSION['mp_shipping_info']['zip']);
  77. $country = (!empty($_SESSION['mp_billing_info']['country'])) ? $_SESSION['mp_billing_info']['country'] : (!empty($meta['country'])?$meta['country']:$_SESSION['mp_shipping_info']['country']);
  78. if (!$country)
  79. $country = $settings['base_country'];
  80. $phone = (!empty($_SESSION['mp_billing_info']['phone'])) ? $_SESSION['mp_billing_info']['phone'] : (!empty($meta['phone'])?$meta['phone']:$_SESSION['mp_shipping_info']['phone']);
  81. $content .= '<style type="text/css">
  82. .cardimage {
  83. height: 23px;
  84. width: 157px;
  85. display: inline-table;
  86. }
  87. .nocard {
  88. background-position: 0px 0px !important;
  89. }
  90. .visa_card {
  91. background-position: 0px -23px !important;
  92. }
  93. .mastercard {
  94. background-position: 0px -46px !important;
  95. }
  96. .discover_card {
  97. background-position: 0px -69px !important;
  98. }
  99. .amex {
  100. background-position: 0px -92px !important;
  101. }
  102. </style>
  103. <script type="text/javascript">
  104. function cc_card_pick(card_image, card_num){
  105. if (card_image == null) {
  106. card_image = "#cardimage";
  107. }
  108. if (card_num == null) {
  109. card_num = "#card_num";
  110. }
  111. numLength = jQuery(card_num).val().length;
  112. number = jQuery(card_num).val();
  113. if (numLength > 10)
  114. {
  115. if((number.charAt(0) == "4") && ((numLength == 13)||(numLength==16))) { jQuery(card_image).removeClass(); jQuery(card_image).addClass("cardimage visa_card"); }
  116. else if((number.charAt(0) == "5" && ((number.charAt(1) >= "1") && (number.charAt(1) <= "5"))) && (numLength==16)) { jQuery(card_image).removeClass(); jQuery(card_image).addClass("cardimage mastercard"); }
  117. else if(number.substring(0,4) == "6011" && (numLength==16)) { jQuery(card_image).removeClass(); jQuery(card_image).addClass("cardimage amex"); }
  118. else if((number.charAt(0) == "3" && ((number.charAt(1) == "4") || (number.charAt(1) == "7"))) && (numLength==15)) { jQuery(card_image).removeClass(); jQuery(card_image).addClass("cardimage discover_card"); }
  119. else { jQuery(card_image).removeClass(); jQuery(card_image).addClass("cardimage nocard"); }
  120. }
  121. }
  122. jQuery(document).ready( function() {
  123. jQuery(".noautocomplete").attr("autocomplete", "off");
  124. });
  125. </script>';
  126. $content .= '<table class="mp_cart_billing">
  127. <thead><tr>
  128. <th colspan="2">'.__('Enter Your Billing Information:', 'mp').'</th>
  129. </tr></thead>
  130. <tbody>
  131. <tr>
  132. <td align="right">'.__('Email:', 'mp').'*</td><td>
  133. '.apply_filters( 'mp_checkout_error_email', '' ).'
  134. <input size="35" name="email" type="text" value="'.esc_attr($email).'" /></td>
  135. </tr>
  136. <tr>
  137. <td align="right">'.__('Full Name:', 'mp').'*</td><td>
  138. '.apply_filters( 'mp_checkout_error_name', '' ).'
  139. <input size="35" name="name" type="text" value="'.esc_attr($name).'" /> </td>
  140. </tr>
  141. <tr>
  142. <td align="right">'.__('Address:', 'mp').'*</td><td>
  143. '.apply_filters( 'mp_checkout_error_address1', '' ).'
  144. <input size="45" name="address1" type="text" value="'.esc_attr($address1).'" /><br />
  145. <small><em>'.__('Street address, P.O. box, company name, c/o', 'mp').'</em></small>
  146. </td>
  147. </tr>
  148. <tr>
  149. <td align="right">'.__('Address 2:', 'mp').'&nbsp;</td><td>
  150. <input size="45" name="address2" type="text" value="'.esc_attr($address2).'" /><br />
  151. <small><em>'.__('Apartment, suite, unit, building, floor, etc.', 'mp').'</em></small>
  152. </td>
  153. </tr>
  154. <tr>
  155. <td align="right">'.__('City:', 'mp').'*</td><td>
  156. '.apply_filters( 'mp_checkout_error_city', '' ).'
  157. <input size="25" name="city" type="text" value="'.esc_attr($city).'" /></td>
  158. </tr>
  159. <tr>
  160. <td align="right">'.__('State/Province/Region:', 'mp').'*</td><td>
  161. '.apply_filters( 'mp_checkout_error_state', '' ).'
  162. <input size="15" name="state" type="text" value="'.esc_attr($state).'" /></td>
  163. </tr>
  164. <tr>
  165. <td align="right">'.__('Postal/Zip Code:', 'mp').'*</td><td>
  166. '.apply_filters( 'mp_checkout_error_zip', '' ).'
  167. <input size="10" id="mp_zip" name="zip" type="text" value="'.esc_attr($zip).'" /></td>
  168. </tr>
  169. <tr>
  170. <td align="right">'.__('Country:', 'mp').'*</td><td>
  171. '.apply_filters( 'mp_checkout_error_country', '' ).'
  172. <select id="mp_" name="country">';
  173. foreach ((array)$settings['shipping']['allowed_countries'] as $code) {
  174. $content .= '<option value="'.$code.'"'.selected($country, $code, false).'>'.esc_attr($mp->countries[$code]).'</option>';
  175. }
  176. $content .= '</select>
  177. </td>
  178. </tr>
  179. <tr>
  180. <td align="right">'.__('Phone Number:', 'mp').'</td><td>
  181. <input size="20" name="phone" type="text" value="'.esc_attr($phone).'" /></td>
  182. </tr>
  183. <tr>
  184. <td align="right">'.__('Credit Card Number:', 'mp').'*</td>
  185. <td>
  186. '.apply_filters( 'mp_checkout_error_card_num', '' ).'
  187. <input name="card_num" onkeyup="cc_card_pick(\'#cardimage\', \'#card_num\');"
  188. id="card_num" class="credit_card_number input_field noautocomplete"
  189. type="text" size="22" maxlength="22" />
  190. <div class="hide_after_success nocard cardimage" id="cardimage" style="background: url('.$mp->plugin_url.'images/card_array.png) no-repeat;"></div></td>
  191. </tr>
  192. <tr>
  193. <td align="right">'.__('Expiration Date:', 'mp').'*</td>
  194. <td>
  195. '.apply_filters( 'mp_checkout_error_exp', '' ).'
  196. <label class="inputLabel" for="exp_month">'.__('Month', 'mp').'</label>
  197. <select name="exp_month" id="exp_month">
  198. '.$this->_print_month_dropdown().'
  199. </select>
  200. <label class="inputLabel" for="exp_year">'.__('Year', 'mp').'</label>
  201. <select name="exp_year" id="exp_year">
  202. '.$this->_print_year_dropdown('', true).'
  203. </select>
  204. </td>
  205. </tr>
  206. <tr>
  207. <td align="right">'.__('Security Code:', 'mp').'</td>
  208. <td>'.apply_filters( 'mp_checkout_error_card_code', '' ).'
  209. <input id="card_code" name="card_code" class="input_field noautocomplete"
  210. style="width: 70px;" type="text" size="4" maxlength="4" /></td>
  211. </tr>
  212. </tbody>
  213. </table>';
  214. return $content;
  215. }
  216. function _print_year_dropdown($sel='', $pfp = false) {
  217. $localDate=getdate();
  218. $minYear = $localDate["year"];
  219. $maxYear = $minYear + 15;
  220. $output = "<option value=''>--</option>";
  221. for($i=$minYear; $i<$maxYear; $i++) {
  222. if ($pfp) {
  223. $output .= "<option value='". substr($i, 0, 4) ."'".($sel==(substr($i, 0, 4))?' selected':'').
  224. ">". $i ."</option>";
  225. } else {
  226. $output .= "<option value='". substr($i, 2, 2) ."'".($sel==(substr($i, 2, 2))?' selected':'').
  227. ">". $i ."</option>";
  228. }
  229. }
  230. return($output);
  231. }
  232. function _print_month_dropdown($sel='') {
  233. $output = "<option value=''>--</option>";
  234. $output .= "<option " . ($sel==1?' selected':'') . " value='01'>01 - Jan</option>";
  235. $output .= "<option " . ($sel==2?' selected':'') . " value='02'>02 - Feb</option>";
  236. $output .= "<option " . ($sel==3?' selected':'') . " value='03'>03 - Mar</option>";
  237. $output .= "<option " . ($sel==4?' selected':'') . " value='04'>04 - Apr</option>";
  238. $output .= "<option " . ($sel==5?' selected':'') . " value='05'>05 - May</option>";
  239. $output .= "<option " . ($sel==6?' selected':'') . " value='06'>06 - Jun</option>";
  240. $output .= "<option " . ($sel==7?' selected':'') . " value='07'>07 - Jul</option>";
  241. $output .= "<option " . ($sel==8?' selected':'') . " value='08'>08 - Aug</option>";
  242. $output .= "<option " . ($sel==9?' selected':'') . " value='09'>09 - Sep</option>";
  243. $output .= "<option " . ($sel==10?' selected':'') . " value='10'>10 - Oct</option>";
  244. $output .= "<option " . ($sel==11?' selected':'') . " value='11'>11 - Nov</option>";
  245. $output .= "<option " . ($sel==12?' selected':'') . " value='12'>12 - Dec</option>";
  246. return($output);
  247. }
  248. /**
  249. * Use this to process any fields you added. Use the $_POST global,
  250. * and be sure to save it to both the $_SESSION and usermeta if logged in.
  251. * DO NOT save credit card details to usermeta as it's not PCI compliant.
  252. * Call $mp->cart_checkout_error($msg, $context); to handle errors. If no errors
  253. * it will redirect to the next step.
  254. *
  255. * @param array $cart. Contains the cart contents for the current blog, global cart if $mp->global_cart is true
  256. * @param array $shipping_info. Contains shipping info and email in case you need it
  257. */
  258. function process_payment_form($cart, $shipping_info) {
  259. global $mp;
  260. $settings = get_option('mp_settings');
  261. if (!is_email($_POST['email']))
  262. $mp->cart_checkout_error('Please enter a valid Email Address.', 'email');
  263. if (empty($_POST['name']))
  264. $mp->cart_checkout_error('Please enter your Full Name.', 'name');
  265. if (empty($_POST['address1']))
  266. $mp->cart_checkout_error('Please enter your Street Address.', 'address1');
  267. if (empty($_POST['city']))
  268. $mp->cart_checkout_error('Please enter your City.', 'city');
  269. if (($_POST['country'] == 'US' || $_POST['country'] == 'CA') && empty($_POST['state']))
  270. $mp->cart_checkout_error('Please enter your State/Province/Region.', 'state');
  271. if (empty($_POST['zip']))
  272. $mp->cart_checkout_error('Please enter your Zip/Postal Code.', 'zip');
  273. if (empty($_POST['country']) || strlen($_POST['country']) != 2)
  274. $mp->cart_checkout_error('Please enter your Country.', 'country');
  275. //for checkout plugins
  276. do_action( 'mp_billing_process' );
  277. //save to session
  278. global $current_user;
  279. $meta = get_user_meta($current_user->ID, 'mp_billing_info', true);
  280. $_SESSION['mp_billing_info']['email'] = ($_POST['email']) ? trim(stripslashes($_POST['email'])) : $current_user->user_email;
  281. $_SESSION['mp_billing_info']['name'] = ($_POST['name']) ? trim(stripslashes($_POST['name'])) : $current_user->user_firstname . ' ' . $current_user->user_lastname;
  282. $_SESSION['mp_billing_info']['address1'] = ($_POST['address1']) ? trim(stripslashes($_POST['address1'])) : $meta['address1'];
  283. $_SESSION['mp_billing_info']['address2'] = ($_POST['address2']) ? trim(stripslashes($_POST['address2'])) : $meta['address2'];
  284. $_SESSION['mp_billing_info']['city'] = ($_POST['city']) ? trim(stripslashes($_POST['city'])) : $meta['city'];
  285. $_SESSION['mp_billing_info']['state'] = ($_POST['state']) ? trim(stripslashes($_POST['state'])) : $meta['state'];
  286. $_SESSION['mp_billing_info']['zip'] = ($_POST['zip']) ? trim(stripslashes($_POST['zip'])) : $meta['zip'];
  287. $_SESSION['mp_billing_info']['country'] = ($_POST['country']) ? trim($_POST['country']) : $meta['country'];
  288. $_SESSION['mp_billing_info']['phone'] = ($_POST['phone']) ? preg_replace('/[^0-9-\(\) ]/', '', trim($_POST['phone'])) : $meta['phone'];
  289. //save to user meta
  290. if ($current_user->ID)
  291. update_user_meta($current_user->ID, 'mp_billing_info', $_SESSION['mp_billing_info']);
  292. if (!isset($_POST['exp_month']) || !isset($_POST['exp_year']) || empty($_POST['exp_month']) || empty($_POST['exp_year'])) {
  293. $mp->cart_checkout_error( __('Please select your credit card expiration date.', 'mp'), 'exp');
  294. }
  295. if (!isset($_POST['card_code']) || empty($_POST['card_code'])) {
  296. $mp->cart_checkout_error( __('Please enter your credit card security code', 'mp'), 'card_code');
  297. }
  298. if (!isset($_POST['card_num']) || empty($_POST['card_num'])) {
  299. $mp->cart_checkout_error( __('Please enter your credit card number', 'mp'), 'card_num');
  300. } else {
  301. if ($this->_get_card_type($_POST['card_num']) == "") {
  302. $mp->cart_checkout_error( __('Please enter a valid credit card number', 'mp'), 'card_num');
  303. }
  304. }
  305. if (!$mp->checkout_error) {
  306. if (
  307. ($this->_get_card_type($_POST['card_num']) == "American Express" && strlen($_POST['card_code']) != 4) ||
  308. ($this->_get_card_type($_POST['card_num']) != "American Express" && strlen($_POST['card_code']) != 3)
  309. ) {
  310. $mp->cart_checkout_error(__('Please enter a valid credit card security code', 'mp'), 'card_code');
  311. }
  312. }
  313. if (!$mp->checkout_error) {
  314. $_SESSION['card_num'] = $_POST['card_num'];
  315. $_SESSION['card_code'] = $_POST['card_code'];
  316. $_SESSION['exp_month'] = $_POST['exp_month'];
  317. $_SESSION['exp_year'] = $_POST['exp_year'];
  318. $mp->generate_order_id();
  319. }
  320. }
  321. function _get_card_type($number) {
  322. $num_length = strlen($number);
  323. if ($num_length > 10 && preg_match('/[0-9]+/', $number) >= 1) {
  324. if((substr($number, 0, 1) == '4') && (($num_length == 13)||($num_length == 16))) {
  325. return "Visa";
  326. } else if((substr($number, 0, 1) == '5' && ((substr($number, 1, 1) >= '1') && (substr($number, 1, 1) <= '5'))) && ($num_length == 16)) {
  327. return "Mastercard";
  328. } else if(substr($number, 0, 4) == "6011" && ($num_length == 16)) {
  329. return "Discover Card";
  330. } else if((substr($number, 0, 1) == '3' && ((substr($number, 1, 1) == '4') || (substr($number, 1, 1) == '7'))) && ($num_length == 15)) {
  331. return "American Express";
  332. }
  333. }
  334. return "";
  335. }
  336. /**
  337. * Return the chosen payment details here for final confirmation. You probably don't need
  338. * to post anything in the form as it should be in your $_SESSION var already.
  339. *
  340. * @param array $cart. Contains the cart contents for the current blog, global cart if $mp->global_cart is true
  341. * @param array $shipping_info. Contains shipping info and email in case you need it
  342. */
  343. function confirm_payment_form($cart, $shipping_info) {
  344. global $mp;
  345. $settings = get_option('mp_settings');
  346. $meta = get_user_meta($current_user->ID, 'mp_billing_info', true);
  347. $email = (!empty($_SESSION['mp_billing_info']['email'])) ? $_SESSION['mp_billing_info']['email'] : (!empty($meta['email'])?$meta['email']:$_SESSION['mp_shipping_info']['email']);
  348. $name = (!empty($_SESSION['mp_billing_info']['name'])) ? $_SESSION['mp_billing_info']['name'] : (!empty($meta['name'])?$meta['name']:$_SESSION['mp_shipping_info']['name']);
  349. $address1 = (!empty($_SESSION['mp_billing_info']['address1'])) ? $_SESSION['mp_billing_info']['address1'] : (!empty($meta['address1'])?$meta['address1']:$_SESSION['mp_shipping_info']['address1']);
  350. $address2 = (!empty($_SESSION['mp_billing_info']['address2'])) ? $_SESSION['mp_billing_info']['address2'] : (!empty($meta['address2'])?$meta['address2']:$_SESSION['mp_shipping_info']['address2']);
  351. $city = (!empty($_SESSION['mp_billing_info']['city'])) ? $_SESSION['mp_billing_info']['city'] : (!empty($meta['city'])?$meta['city']:$_SESSION['mp_shipping_info']['city']);
  352. $state = (!empty($_SESSION['mp_billing_info']['state'])) ? $_SESSION['mp_billing_info']['state'] : (!empty($meta['state'])?$meta['state']:$_SESSION['mp_shipping_info']['state']);
  353. $zip = (!empty($_SESSION['mp_billing_info']['zip'])) ? $_SESSION['mp_billing_info']['zip'] : (!empty($meta['zip'])?$meta['zip']:$_SESSION['mp_shipping_info']['zip']);
  354. $country = (!empty($_SESSION['mp_billing_info']['country'])) ? $_SESSION['mp_billing_info']['country'] : (!empty($meta['country'])?$meta['country']:$_SESSION['mp_shipping_info']['country']);
  355. if (!$country)
  356. $country = $settings['base_country'];
  357. $phone = (!empty($_SESSION['mp_billing_info']['phone'])) ? $_SESSION['mp_billing_info']['phone'] : (!empty($meta['phone'])?$meta['phone']:$_SESSION['mp_shipping_info']['phone']);
  358. $content = '';
  359. $content .= '<table class="mp_cart_billing">';
  360. $content .= '<thead><tr>';
  361. $content .= '<th>'.__('Billing Information:', 'mp').'</th>';
  362. $content .= '<th align="right"><a href="'. mp_checkout_step_url('checkout').'">'.__('&laquo; Edit', 'mp').'</a></th>';
  363. $content .= '</tr></thead>';
  364. $content .= '<tbody>';
  365. $content .= '<tr>';
  366. $content .= '<td align="right">'.__('Email:', 'mp').'</td><td>';
  367. $content .= esc_attr($email).'</td>';
  368. $content .= '</tr>';
  369. $content .= '<tr>';
  370. $content .= '<td align="right">'.__('Full Name:', 'mp').'</td><td>';
  371. $content .= esc_attr($name).'</td>';
  372. $content .= '</tr>';
  373. $content .= '<tr>';
  374. $content .= '<td align="right">'.__('Address:', 'mp').'</td>';
  375. $content .= '<td>'.esc_attr($address1).'</td>';
  376. $content .= '</tr>';
  377. if ($address2) {
  378. $content .= '<tr>';
  379. $content .= '<td align="right">'.__('Address 2:', 'mp').'</td>';
  380. $content .= '<td>'.esc_attr($address2).'</td>';
  381. $content .= '</tr>';
  382. }
  383. $content .= '<tr>';
  384. $content .= '<td align="right">'.__('City:', 'mp').'</td>';
  385. $content .= '<td>'.esc_attr($city).'</td>';
  386. $content .= '</tr>';
  387. if ($state) {
  388. $content .= '<tr>';
  389. $content .= '<td align="right">'.__('State/Province/Region:', 'mp').'</td>';
  390. $content .= '<td>'.esc_attr($state).'</td>';
  391. $content .= '</tr>';
  392. }
  393. $content .= '<tr>';
  394. $content .= '<td align="right">'.__('Postal/Zip Code:', 'mp').'</td>';
  395. $content .= '<td>'.esc_attr($zip).'</td>';
  396. $content .= '</tr>';
  397. $content .= '<tr>';
  398. $content .= '<td align="right">'.__('Country:', 'mp').'</td>';
  399. $content .= '<td>'.$mp->countries[$country].'</td>';
  400. $content .= '</tr>';
  401. if ($phone) {
  402. $content .= '<tr>';
  403. $content .= '<td align="right">'.__('Phone Number:', 'mp').'</td>';
  404. $content .= '<td>'.esc_attr($phone).'</td>';
  405. $content .= '</tr>';
  406. }
  407. $content .= '<tr>';
  408. $content .= '<td align="right">'.__('Payment method:', 'mp').'</td>';
  409. $content .= '<td>'.$this->_get_card_type($_SESSION['card_num']).' ending in '. substr($_SESSION['card_num'], strlen($_SESSION['card_num'])-4, 4).'</td>';
  410. $content .= '</tr>';
  411. $content .= '</tbody>';
  412. $content .= '</table>';
  413. return $content;
  414. }
  415. /**
  416. * Use this to do the final payment. Create the order then process the payment. If
  417. * you know the payment is successful right away go ahead and change the order status
  418. * as well.
  419. * Call $mp->cart_checkout_error($msg, $context); to handle errors. If no errors
  420. * it will redirect to the next step.
  421. *
  422. * @param array $cart. Contains the cart contents for the current blog, global cart if $mp->global_cart is true
  423. * @param array $shipping_info. Contains shipping info and email in case you need it
  424. */
  425. function process_payment($cart, $shipping_info) {
  426. global $mp;
  427. $timestamp = time();
  428. $settings = get_option('mp_settings');
  429. $billing_info = $_SESSION['mp_billing_info'];
  430. $payment = new MP_Gateway_Worker_AuthorizeNet_AIM($this->API_Endpoint,
  431. $settings['gateways']['authorizenet-aim']['delim_data'],
  432. $settings['gateways']['authorizenet-aim']['delim_char'],
  433. $settings['gateways']['authorizenet-aim']['encap_char'],
  434. $settings['gateways']['authorizenet-aim']['api_user'],
  435. $settings['gateways']['authorizenet-aim']['api_key'],
  436. ($settings['gateways']['authorizenet-aim']['mode'] == 'sandbox'));
  437. $payment->transaction($_SESSION['card_num']);
  438. $totals = array();
  439. foreach ($cart as $product_id => $variations) {
  440. foreach ($variations as $variation => $data) {
  441. $sku = empty($data['SKU']) ? "{$product_id}_{$variation}" : $data['SKU'];
  442. $totals[] = $mp->before_tax_price($data['price'], $product_id) * $data['quantity'];
  443. $payment->addLineItem($sku, substr($data['name'], 0, 31),
  444. substr($data['name'].' - '.$data['url'], 0, 254), $data['quantity'], $mp->before_tax_price($data['price'], $product_id), 1);
  445. $i++;
  446. }
  447. }
  448. $total = array_sum($totals);
  449. //coupon line
  450. if ( $coupon = $mp->coupon_value($mp->get_coupon_code(), $total) ) {
  451. $total = $coupon['new_total'];
  452. }
  453. //shipping line
  454. if ( ($shipping_price = $mp->shipping_price()) !== false ) {
  455. $total = $total + $shipping_price;
  456. }
  457. //tax line
  458. if ( ($tax_price = $mp->tax_price()) !== false ) {
  459. $total = $total + $tax_price;
  460. }
  461. // Billing Info
  462. $payment->setParameter("x_card_code", $_SESSION['card_code']);
  463. $payment->setParameter("x_exp_date ", $_SESSION['exp_month'] . $_SESSION['exp_year']);
  464. $payment->setParameter("x_amount", $total);
  465. // Order Info
  466. $payment->setParameter("x_description", "Order ID: ".$_SESSION['mp_order']);
  467. $payment->setParameter("x_invoice_num", $_SESSION['mp_order']);
  468. if ($settings['gateways']['authorizenet-aim']['mode'] == 'sandbox') {
  469. $payment->setParameter("x_test_request", true);
  470. } else {
  471. $payment->setParameter("x_test_request", false);
  472. }
  473. $payment->setParameter("x_duplicate_window", 30);
  474. // E-mail
  475. $payment->setParameter("x_header_email_receipt", $settings['gateways']['authorizenet-aim']['header_email_receipt']);
  476. $payment->setParameter("x_footer_email_receipt", $settings['gateways']['authorizenet-aim']['footer_email_receipt']);
  477. $payment->setParameter("x_email_customer", strtoupper($settings['gateways']['authorizenet-aim']['email_customer']));
  478. $_names = split(" ", $billing_info['name']);
  479. if (isset($_names[0])) {
  480. $first_name = array_shift($_names);
  481. } else {
  482. $first_name = "";
  483. }
  484. if (isset($_names[0])) {
  485. $last_name = join(" ", $_names);
  486. } else {
  487. $last_name = "";
  488. }
  489. $address = $billing_info['address1'];
  490. if (!empty($billing_info['address2'])) {
  491. $address .= "\n".$billing_info['address2'];
  492. }
  493. //Customer Info
  494. $payment->setParameter("x_first_name", $first_name);
  495. $payment->setParameter("x_last_name", $last_name);
  496. $payment->setParameter("x_address", $address);
  497. $payment->setParameter("x_city", $billing_info['city']);
  498. $payment->setParameter("x_state", $billing_info['state']);
  499. $payment->setParameter("x_country", $billing_info['country']);
  500. $payment->setParameter("x_zip", $billing_info['zip']);
  501. $payment->setParameter("x_phone", $billing_info['phone']);
  502. $payment->setParameter("x_email", $billing_info['email']);
  503. //only add shipping info if set
  504. if (!$mp->download_only_cart($cart) && isset($shipping_info['name'])) {
  505. $_names = split(" ", $shipping_info['name']);
  506. if (isset($_names[0])) {
  507. $shipping_first_name = array_shift($_names);
  508. } else {
  509. $shipping_first_name = "";
  510. }
  511. if (isset($_names[0])) {
  512. $shipping_last_name = join(" ", $_names);
  513. } else {
  514. $shipping_last_name = "";
  515. }
  516. $shipping_address = $shipping_info['address1'];
  517. if (!empty($billing_info['address2'])) {
  518. $shipping_address .= "\n".$shipping_info['address2'];
  519. }
  520. $payment->setParameter("x_ship_to_first_name", $shipping_first_name);
  521. $payment->setParameter("x_ship_to_last_name", $shipping_last_name);
  522. $payment->setParameter("x_ship_to_address", $shipping_address);
  523. $payment->setParameter("x_ship_to_city", $shipping_info['city']);
  524. $payment->setParameter("x_ship_to_state", $shipping_info['state']);
  525. $payment->setParameter("x_ship_to_country", $shipping_info['country']);
  526. $payment->setParameter("x_ship_to_zip", $shipping_info['zip']);
  527. }
  528. $payment->setParameter("x_customer_ip", $_SERVER['REMOTE_ADDR']);
  529. $payment->process();
  530. if ($payment->isApproved()) {
  531. $status = __('The payment has been completed, and the funds have been added successfully to your account balance.', 'mp');
  532. $paid = true;
  533. $payment_info['gateway_public_name'] = $this->public_name;
  534. $payment_info['gateway_private_name'] = $this->admin_name;
  535. $payment_info['method'] = $payment->getMethod();
  536. $payment_info['status'][$timestamp] = "paid";
  537. $payment_info['total'] = $total;
  538. $payment_info['currency'] = "USD"; // Authorize.net only supports USD transactions
  539. $payment_info['transaction_id'] = $payment->getTransactionID();
  540. //succesful payment, create our order now
  541. $result = $mp->create_order($_SESSION['mp_order'], $cart, $shipping_info, $payment_info, $paid);
  542. } else {
  543. $error = $payment->getResponseText();
  544. $mp->cart_checkout_error( sprintf(__('There was a problem finalizing your purchase. %s Please <a href="%s">go back and try again</a>.', 'mp') , $error, mp_checkout_step_url('checkout')) );
  545. }
  546. }
  547. /**
  548. * Filters the order confirmation email message body. You may want to append something to
  549. * the message. Optional
  550. *
  551. * Don't forget to return!
  552. */
  553. function order_confirmation_email($msg) {
  554. return $msg;
  555. }
  556. /**
  557. * Return any html you want to show on the confirmation screen after checkout. This
  558. * should be a payment details box and message.
  559. */
  560. function order_confirmation_msg($content, $order) {
  561. global $mp;
  562. if ($order->post_status == 'order_received') {
  563. $content .= '<p>' . sprintf(__('Your credit card payment for this order totaling %s is not yet complete. Here is the latest status:', 'mp'), $mp->format_currency($order->mp_payment_info['currency'], $order->mp_payment_info['total'])) . '</p>';
  564. $statuses = $order->mp_payment_info['status'];
  565. krsort($statuses); //sort with latest status at the top
  566. $status = reset($statuses);
  567. $timestamp = key($statuses);
  568. $content .= '<p><strong>' . date(get_option('date_format') . ' - ' . get_option('time_format'), $timestamp) . ':</strong> ' . htmlentities($status) . '</p>';
  569. } else {
  570. $content .= '<p>' . sprintf(__('Your credit card payment for this order totaling %s is complete. The credit card transaction number is <strong>%s</strong>.', 'mp'), $mp->format_currency($order->mp_payment_info['currency'], $order->mp_payment_info['total']), $order->mp_payment_info['transaction_id']) . '</p>';
  571. }
  572. return $content;
  573. }
  574. /**
  575. * Runs before page load incase you need to run any scripts before loading the success message page
  576. */
  577. function order_confirmation($order) {
  578. }
  579. /**
  580. * Echo a settings meta box with whatever settings you need for you gateway.
  581. * Form field names should be prefixed with mp[gateways][plugin_name], like "mp[gateways][plugin_name][mysetting]".
  582. * You can access saved settings via $settings array.
  583. */
  584. function gateway_settings_box($settings) {
  585. global $mp;
  586. ?>
  587. <div id="mp_authorizenet-aim_express" class="postbox">
  588. <h3 class='hndle'><span><?php _e('Authorize.net AIM Settings', 'mp'); ?></span></h3>
  589. <div class="inside">
  590. <span class="description"><?php _e('Authorize.net AIM is a customizable payment processing solution that gives the merchant control over all the steps in processing a transaction. An SSL certificate is required to use this gateway. USD is the only currency supported by this gateway.', 'mp') ?></span>
  591. <table class="form-table">
  592. <tr>
  593. <th scope="row"><?php _e('Mode', 'mp') ?></th>
  594. <td>
  595. <p>
  596. <select name="mp[gateways][authorizenet-aim][mode]">
  597. <option value="sandbox" <?php selected($settings['gateways']['authorizenet-aim']['mode'], 'sandbox') ?>><?php _e('Sandbox', 'mp') ?></option>
  598. <option value="live" <?php selected($settings['gateways']['authorizenet-aim']['mode'], 'live') ?>><?php _e('Live', 'mp') ?></option>
  599. </select>
  600. </p>
  601. </td>
  602. </tr>
  603. <tr>
  604. <th scope="row"><?php _e('Gateway Credentials', 'mp') ?></th>
  605. <td>
  606. <span class="description"><?php print sprintf(__('You must login to Authorize.net merchant dashboard to obtain the API login ID and API transaction key. <a target="_blank" href="%s">Instructions &raquo;</a>', 'mp'), "http://www.authorize.net/support/merchant/Integration_Settings/Access_Settings.htm"); ?></span>
  607. <p>
  608. <label><?php _e('Login ID', 'mp') ?><br />
  609. <input value="<?php echo esc_attr($settings['gateways']['authorizenet-aim']['api_user']); ?>" size="30" name="mp[gateways][authorizenet-aim][api_user]" type="text" />
  610. </label>
  611. </p>
  612. <p>
  613. <label><?php _e('Transaction Key', 'mp') ?><br />
  614. <input value="<?php echo esc_attr($settings['gateways']['authorizenet-aim']['api_key']); ?>" size="30" name="mp[gateways][authorizenet-aim][api_key]" type="text" />
  615. </label>
  616. </p>
  617. </td>
  618. </tr>
  619. <tr>
  620. <th scope="row"><?php _e('Advanced Settings', 'mp') ?></th>
  621. <td>
  622. <span class="description"><?php _e('Optional settings to control advanced options', 'mp') ?></span>
  623. <p>
  624. <label><a title="<?php _e('Authorize.net default is \',\'. Otherwise, get this from your credit card processor. If the transactions are not going through, this character is most likely wrong.', 'mp'); ?>"><?php _e('Delimiter Character', 'mp'); ?></a><br />
  625. <input value="<?php echo (empty($settings['gateways']['authorizenet-aim']['delim_char']))?",":esc_attr($settings['gateways']['authorizenet-aim']['delim_char']); ?>" size="2" name="mp[gateways][authorizenet-aim][delim_char]" type="text" />
  626. </label>
  627. </p>
  628. <p>
  629. <label><a title="<?php _e('Authorize.net default is blank. Otherwise, get this from your credit card processor. If the transactions are going through, but getting strange responses, this character is most likely wrong.', 'mp'); ?>"><?php _e('Encapsulation Character', 'mp'); ?></a><br />
  630. <input value="<?php echo esc_attr($settings['gateways']['authorizenet-aim']['encap_char']); ?>" size="2" name="mp[gateways][authorizenet-aim][encap_char]" type="text" />
  631. </label>
  632. </p>
  633. <p>
  634. <label><?php _e('Email Customer (on success):', 'mp'); ?><br />
  635. <select name="mp[gateways][authorizenet-aim][email_customer]">
  636. <option value="yes" <?php selected($settings['gateways']['authorizenet-aim']['email_customer'], 'yes') ?>><?php _e('Yes', 'mp') ?></option>
  637. <option value="no" <?php selected($settings['gateways']['authorizenet-aim']['email_customer'], 'no') ?>><?php _e('No', 'mp') ?></option>
  638. </select>
  639. </label>
  640. </p>
  641. <p>
  642. <label><a title="<?php _e('This text will appear as the header of the email receipt sent to the customer.', 'mp'); ?>"><?php _e('Customer Receipt Email Header', 'mp'); ?></a><br/>
  643. <input value="<?php echo empty($settings['gateways']['authorizenet-aim']['header_email_receipt'])?__('Thanks for your payment!', 'mp'):esc_attr($settings['gateways']['authorizenet-aim']['header_email_receipt']); ?>" size="40" name="mp[gateways][authorizenet-aim][header_email_receipt]" type="text" />
  644. </label>
  645. </p>
  646. <p>
  647. <label><a title="<?php _e('This text will appear as the footer on the email receipt sent to the customer.', 'mp'); ?>"><?php _e('Customer Receipt Email Footer', 'mp'); ?></a><br/>
  648. <input value="<?php echo empty($settings['gateways']['authorizenet-aim']['footer_email_receipt']) ? '' : esc_attr($settings['gateways']['authorizenet-aim']['footer_email_receipt']); ?>" size="40" name="mp[gateways][authorizenet-aim][footer_email_receipt]" type="text" />
  649. </label>
  650. </p>
  651. <p>
  652. <label><a title="<?php _e('The payment gateway generated MD5 hash value that can be used to authenticate the transaction response. Not needed because responses are returned using an SSL connection.', 'mp'); ?>"><?php _e('Security: MD5 Hash', 'mp'); ?></a><br/>
  653. <input value="<?php echo esc_attr($settings['gateways']['authorizenet-aim']['md5_hash']); ?>" size="32" name="mp[gateways][authorizenet-aim][md5_hash]" type="text" />
  654. </label>
  655. </p>
  656. <p>
  657. <label><a title="<?php _e('Request a delimited response from the payment gateway.', 'mp'); ?>"><?php _e('Delim Data:', 'mp'); ?></a><br/>
  658. <select name="mp[gateways][authorizenet-aim][delim_data]">
  659. <option value="yes" <?php selected($settings['gateways']['authorizenet-aim']['delim_data'], 'yes') ?>><?php _e('Yes', 'mp') ?></option>
  660. <option value="no" <?php selected($settings['gateways']['authorizenet-aim']['delim_data'], 'no') ?>><?php _e('No', 'mp') ?></option>
  661. </select>
  662. </label>
  663. </p>
  664. <p>
  665. <label><a title="<?php _e('Many other gateways have Authorize.net API emulators. To use one of these gateways input their API post url here.', 'mp'); ?>"><?php _e('Custom API URL', 'mp') ?></a><br />
  666. <input value="<?php echo esc_attr($settings['gateways']['authorizenet-aim']['custom_api']); ?>" size="50" name="mp[gateways][authorizenet-aim][custom_api]" type="text" />
  667. </label>
  668. </p>
  669. </td>
  670. </tr>
  671. </table>
  672. </div>
  673. </div>
  674. <?php
  675. }
  676. /**
  677. * Filters posted data from your settings form. Do anything you need to the $settings['gateways']['plugin_name']
  678. * array. Don't forget to return!
  679. */
  680. function process_gateway_settings($settings) {
  681. return $settings;
  682. }
  683. }
  684. if(!class_exists('MP_Gateway_Worker_AuthorizeNet_AIM')) {
  685. class MP_Gateway_Worker_AuthorizeNet_AIM
  686. {
  687. var $login;
  688. var $transkey;
  689. var $params = array();
  690. var $results = array();
  691. var $line_items = array();
  692. var $approved = false;
  693. var $declined = false;
  694. var $error = true;
  695. var $method = "";
  696. var $fields;
  697. var $response;
  698. var $instances = 0;
  699. function __construct($url, $delim_data, $delim_char, $encap_char, $gw_username, $gw_tran_key, $gw_test_mode)
  700. {
  701. if ($this->instances == 0)
  702. {
  703. $this->url = $url;
  704. $this->params['x_delim_data'] = ($delim_data == 'yes') ? 'TRUE' : 'FALSE';
  705. $this->params['x_delim_char'] = $delim_char;
  706. $this->params['x_encap_char'] = $encap_char;
  707. $this->params['x_relay_response'] = "FALSE";
  708. $this->params['x_url'] = "FALSE";
  709. $this->params['x_version'] = "3.1";
  710. $this->params['x_method'] = "CC";
  711. $this->params['x_type'] = "AUTH_CAPTURE";
  712. $this->params['x_login'] = $gw_username;
  713. $this->params['x_tran_key'] = $gw_tran_key;
  714. $this->params['x_test_request'] = $gw_test_mode;
  715. $this->instances++;
  716. } else {
  717. return false;
  718. }
  719. }
  720. function transaction($cardnum)
  721. {
  722. $this->params['x_card_num'] = trim($cardnum);
  723. }
  724. function addLineItem($id, $name, $description, $quantity, $price, $taxable = 0)
  725. {
  726. $this->line_items[] = "{$id}<|>{$name}<|>{$description}<|>{$quantity}<|>{$price}<|>{$taxable}";
  727. }
  728. function process($retries = 1)
  729. {
  730. global $mp;
  731. $this->_prepareParameters();
  732. $query_string = rtrim($this->fields, "&");
  733. $count = 0;
  734. while ($count < $retries)
  735. {
  736. $args['user-agent'] = "MarketPress/{$mp->version}: http://premium.wpmudev.org/project/e-commerce | Authorize.net AIM Plugin/{$mp->version}";
  737. $args['body'] = $query_string;
  738. $args['sslverify'] = false;
  739. $args['timeout'] = 30;
  740. //use built in WP http class to work with most server setups
  741. $response = wp_remote_post($this->url, $args);
  742. if (is_array($response) && isset($response['body'])) {
  743. $this->response = $response['body'];
  744. } else {
  745. $this->response = "";
  746. $this->error = true;
  747. return;
  748. }
  749. $this->parseResults();
  750. if ($this->getResultResponseFull() == "Approved")
  751. {
  752. $this->approved = true;
  753. $this->declined = false;
  754. $this->error = false;
  755. $this->method = $this->getMethod();
  756. break;
  757. } else if ($this->getResultResponseFull() == "Declined")
  758. {
  759. $this->approved = false;
  760. $this->declined = true;
  761. $this->error = false;
  762. break;
  763. }
  764. $count++;
  765. }
  766. }
  767. function parseResults()
  768. {
  769. $this->results = explode($this->params['x_delim_char'], $this->response);
  770. }
  771. function setParameter($param, $value)
  772. {
  773. $param = trim($param);
  774. $value = trim($value);
  775. $this->params[$param] = $value;
  776. }
  777. function setTransactionType($type)
  778. {
  779. $this->params['x_type'] = strtoupper(trim($type));
  780. }
  781. function _prepareParameters()
  782. {
  783. foreach($this->params as $key => $value)
  784. {
  785. $this->fields .= "$key=" . urlencode($value) . "&";
  786. }
  787. for($i=0; $i<count($this->line_items); $i++) {
  788. $this->fields .= "x_line_item={$this->line_items[$i]}&";
  789. }
  790. }
  791. function getMethod()
  792. {
  793. if (isset($this->results[51]))
  794. {
  795. return str_replace($this->params['x_encap_char'],'',$this->results[51]);
  796. }
  797. return "";
  798. }
  799. function getGatewayResponse()
  800. {
  801. return str_replace($this->params['x_encap_char'],'',$this->results[0]);
  802. }
  803. function getResultResponseFull()
  804. {
  805. $response = array("", "Approved", "Declined", "Error");
  806. return $response[str_replace($this->params['x_encap_char'],'',$this->results[0])];
  807. }
  808. function isApproved()
  809. {
  810. return $this->approved;
  811. }
  812. function isDeclined()
  813. {
  814. return $this->declined;
  815. }
  816. function isError()
  817. {
  818. return $this->error;
  819. }
  820. function getResponseText()
  821. {
  822. return $this->results[3];
  823. $strip = array($this->params['x_delim_char'],$this->params['x_encap_char'],'|',',');
  824. return str_replace($strip,'',$this->results[3]);
  825. }
  826. function getAuthCode()
  827. {
  828. return str_replace($this->params['x_encap_char'],'',$this->results[4]);
  829. }
  830. function getAVSResponse()
  831. {
  832. return str_replace($this->params['x_encap_char'],'',$this->results[5]);
  833. }
  834. function getTransactionID()
  835. {
  836. return str_replace($this->params['x_encap_char'],'',$this->results[6]);
  837. }
  838. }
  839. }
  840. //register payment gateway plugin
  841. mp_register_gateway_plugin( 'MP_Gateway_AuthorizeNet_AIM', 'authorizenet-aim', __('Authorize.net AIM Checkout', 'mp') );
  842. ?>