PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/3.7.6 Beta 2/merchants/paypal_pro.php

https://github.com/evadne/wp-e-commerce
PHP | 348 lines | 301 code | 25 blank | 22 comment | 89 complexity | 5d6a44e0cf2b87ccd4934aa7909fefda MD5 | raw file
  1. <?php
  2. /*
  3. * Some parts of this code were inspired by the shopp plugin and their paypal pro module.
  4. * and copyright Ingenesis Limited, 19 August, 2008.
  5. */
  6. $nzshpcrt_gateways[$num]['name'] = 'Paypal Payments Pro';
  7. $nzshpcrt_gateways[$num]['internalname'] = 'paypal_pro';
  8. $nzshpcrt_gateways[$num]['function'] = 'gateway_paypal_pro';
  9. $nzshpcrt_gateways[$num]['form'] = "form_paypal_pro";
  10. $nzshpcrt_gateways[$num]['submit_function'] = "submit_paypal_pro";
  11. $nzshpcrt_gateways[$num]['payment_type'] = "credit_card";
  12. if(in_array('paypal_pro',(array)get_option('custom_gateway_options'))) {
  13. $gateway_checkout_form_fields[$nzshpcrt_gateways[$num]['internalname']] = "
  14. <tr %s>
  15. <td>Credit Card Number *</td>
  16. <td>
  17. <input type='text' value='' name='card_number' />
  18. <p class='validation-error'>%s</p>
  19. </td>
  20. </tr>
  21. <tr %s>
  22. <td>Credit Card Expiry *</td>
  23. <td>
  24. <input type='text' size='2' value='' maxlength='2' name='expiry[month]' />/<input type='text' size='4' maxlength='4' value='' name='expiry[year]' />
  25. <p class='validation-error'>%s</p>
  26. </td>
  27. </tr>
  28. <tr %s>
  29. <td>CVV *</td>
  30. <td><input type='text' size='4' value='' maxlength='4' name='card_code' />
  31. <p class='validation-error'>%s</p>
  32. </td>
  33. </tr>
  34. <tr %s>
  35. <td>Card Type *</td>
  36. <td>
  37. <select name='cctype'>
  38. <option value='Visa'>Visa</option>
  39. <option value='Mastercard'>MasterCard</option>
  40. <option value='Discover'>Discover</option>
  41. <option value='Amex'>Amex</option>
  42. </select>
  43. <p class='validation-error'>%s</p>
  44. </td>
  45. </tr>
  46. ";
  47. }
  48. function gateway_paypal_pro($seperator, $sessionid){
  49. global $wpdb, $wpsc_cart;
  50. $purchase_log = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `sessionid`= ".$sessionid." LIMIT 1",ARRAY_A) ;
  51. $usersql = "SELECT `".WPSC_TABLE_SUBMITED_FORM_DATA."`.value, `".WPSC_TABLE_CHECKOUT_FORMS."`.`name`, `".WPSC_TABLE_CHECKOUT_FORMS."`.`unique_name` FROM `".WPSC_TABLE_CHECKOUT_FORMS."` LEFT JOIN `".WPSC_TABLE_SUBMITED_FORM_DATA."` ON `".WPSC_TABLE_CHECKOUT_FORMS."`.id = `".WPSC_TABLE_SUBMITED_FORM_DATA."`.`form_id` WHERE `".WPSC_TABLE_SUBMITED_FORM_DATA."`.`log_id`=".$purchase_log['id']." ORDER BY `".WPSC_TABLE_CHECKOUT_FORMS."`.`order`";
  52. //exit($usersql);
  53. $userinfo = $wpdb->get_results($usersql, ARRAY_A);
  54. //exit('<pre>'.print_r($userinfo, true).'</pre>');
  55. //BUILD DATA TO SEND TO PayPal
  56. $data = array();
  57. $data['USER'] = get_option('paypal_pro_username');
  58. $data['PWD'] = get_option('paypal_pro_password');
  59. $data['SIGNATURE'] = get_option('paypal_pro_signature');
  60. $data['VERSION'] = "52.0";
  61. $data['METHOD'] = "DoDirectPayment";
  62. $data['PAYMENTACTION'] = "Sale";
  63. $data['IPADDRESS'] = $_SERVER["REMOTE_ADDR"];
  64. $data['RETURNFMFDETAILS'] = "1"; // optional - return fraud management filter data
  65. foreach((array)$userinfo as $key => $value){
  66. if(($value['unique_name']=='billingfirstname') && $value['value'] != ''){
  67. $data['FIRSTNAME'] = $value['value'];
  68. }
  69. if(($value['unique_name']=='billinglastname') && $value['value'] != ''){
  70. $data['LASTNAME'] = $value['value'];
  71. }
  72. if(($value['unique_name']=='billingemail') && $value['value'] != ''){
  73. $data['EMAIL'] = $value['value'];
  74. }
  75. if(($value['unique_name']=='billingphone') && $value['value'] != ''){
  76. $data['PHONENUM'] = $value['value'];
  77. }
  78. if(($value['unique_name']=='billingaddress') && $value['value'] != ''){
  79. $data['STREET'] = $value['value'];
  80. }
  81. if(($value['unique_name']=='billingcity') && $value['value'] != ''){
  82. $data['CITY'] = $value['value'];
  83. }
  84. if(($value['unique_name']=='billingstate') && $value['value'] != ''){
  85. $sql = "SELECT `code` FROM `".WPSC_TABLE_REGION_TAX."` WHERE `name` ='".$value['value']."' LIMIT 1";
  86. $data['STATE'] = $wpdb->get_var($sql);
  87. }else{
  88. $data['STATE']='CA';
  89. }
  90. if(($value['unique_name']=='billingcountry') && $value['value'] != ''){
  91. $data['COUNTRYCODE'] = $value['value'];
  92. }
  93. if(($value['unique_name']=='billingpostcode') && $value['value'] != ''){
  94. $data['ZIP'] = $value['value'];
  95. }
  96. //
  97. if((($value['unique_name']=='shippingfirstname') && $value['value'] != '')){
  98. $data1['SHIPTONAME1'] = $value['value'];
  99. }
  100. if((($value['unique_name']=='shippinglastname') && $value['value'] != '')){
  101. $data1['SHIPTONAME2'] = $value['value'];
  102. }
  103. if(($value['unique_name']=='shippingaddress') && $value['value'] != ''){
  104. $data['SHIPTOSTREET'] = $value['value'];
  105. }
  106. if(($value['unique_name']=='shippingcity') && $value['value'] != ''){
  107. $data['SHIPTOCITY'] = $value['value'];
  108. }
  109. //$data['SHIPTOCITY'] = 'CA';
  110. if(($value['unique_name']=='shippingstate') && $value['value'] != ''){
  111. // $data['SHIPTOSTATE'] = $value['value'];
  112. $sql = "SELECT `code` FROM `".WPSC_TABLE_REGION_TAX."` WHERE `name` ='".$value['value']."' LIMIT 1";
  113. $data['SHIPTOSTATE'] = $wpdb->get_var($sql);
  114. }else{
  115. }
  116. if(($value['unique_name']=='shippingcountry') && $value['value'] != ''){
  117. $data['SHIPTOCOUNTRY'] = $value['value'];
  118. }
  119. if(($value['unique_name']=='shippingpostcode') && $value['value'] != ''){
  120. $data['SHIPTOZIP'] = $value['value'];
  121. }
  122. //exit($key.' > '.print_r($value,true));
  123. }
  124. $data['SHIPTONAME'] = $data1['SHIPTONAME1'].' '.$data1['SHIPTONAME2'];
  125. if( ($data['SHIPTONAME'] == null) || ($data['SHIPTOSTREET'] == null) || ($data['SHIPTOCITY'] == null) ||
  126. ($data['SHIPTOSTATE'] == null) || ($data['SHIPTOCOUNTRY'] == null) || ($data['SHIPTOZIP'] == null)) {
  127. // if any shipping details are empty, the order will simply fail, this deletes them all if one is empty
  128. unset($data['SHIPTONAME']);
  129. unset($data['SHIPTOSTREET']);
  130. unset($data['SHIPTOCITY']);
  131. unset($data['SHIPTOSTATE']);
  132. unset($data['SHIPTOCOUNTRY']);
  133. unset($data['SHIPTOZIP']);
  134. }
  135. $data['CREDITCARDTYPE'] = $_POST['cctype'];
  136. $data['ACCT'] = $_POST['card_number'];
  137. $data['EXPDATE'] = $_POST['expiry']['month'].$_POST['expiry']['year'];
  138. $data['CVV2'] = $_POST['card_code'];
  139. $data['AMT'] = number_format($wpsc_cart->total_price,2);
  140. $data['ITEMAMT'] = number_format($wpsc_cart->subtotal,2);
  141. $data['SHIPPINGAMT'] = number_format($wpsc_cart->base_shipping,2);
  142. $data['TAXAMT'] = number_format($wpsc_cart->total_tax, 2);
  143. // Ordered Items
  144. $discount = $wpsc_cart->coupons_amount;
  145. //exit($discount);
  146. if(($discount > 0)) {
  147. $i = 1;
  148. $data['AMT'] = number_format(sprintf("%01.2f", $wpsc_cart->calculate_total_price()),2,'.','');
  149. $data['ITEMAMT'] = number_format(sprintf("%01.2f", $wpsc_cart->calculate_total_price()),2,'.','');
  150. $data['SHIPPINGAMT'] = 0;
  151. $data['TAXAMT'] = 0;
  152. $data['L_NAME'.$i] = "Your Shopping Cart";
  153. $data['L_AMT'.$i] = number_format(sprintf("%01.2f", $wpsc_cart->calculate_total_price()),2,'.','');
  154. $data['L_QTY'.$i] = 1;
  155. // $data['item_number_'.$i] = 0;
  156. $data['L_TAXAMT'.$i] = 0;
  157. } else {
  158. foreach($wpsc_cart->cart_items as $i => $Item) {
  159. $data['L_NAME'.$i] = $Item->product_name;
  160. $data['L_AMT'.$i] = number_format($Item->unit_price,2);
  161. $data['L_NUMBER'.$i] = $i;
  162. $data['L_QTY'.$i] = $Item->quantity;
  163. $data['L_TAXAMT'.$i] = number_format($Item->tax,2);
  164. }
  165. }
  166. $transaction = "";
  167. foreach($data as $key => $value) {
  168. if (is_array($value)) {
  169. foreach($value as $item) {
  170. if (strlen($transaction) > 0) $transaction .= "&";
  171. $transaction .= "$key=".urlencode($item);
  172. }
  173. } else {
  174. if (strlen($transaction) > 0) $transaction .= "&";
  175. $transaction .= "$key=".urlencode($value);
  176. }
  177. }
  178. //exit($transaction);
  179. $response = send($transaction);
  180. //exit('<pre>'.print_r($response, true).'</pre><pre>'.print_r($data, true).'</pre>');
  181. if($response->ack == 'Success' || $response->ack == 'SuccessWithWarning'){
  182. //redirect to transaction page and store in DB as a order with accepted payment
  183. $sql = "UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` SET `processed`= '2' WHERE `sessionid`=".$sessionid;
  184. $wpdb->query($sql);
  185. $transact_url = get_option('transact_url');
  186. unset($_SESSION['WpscGatewayErrorMessage']);
  187. $_SESSION['paypalpro'] = 'success';
  188. header("Location: ".get_option('transact_url').$seperator."sessionid=".$sessionid);
  189. exit(); // on some servers, a header that is not followed up with an exit does nothing.
  190. }else{
  191. //redirect back to checkout page with errors
  192. $sql = "UPDATE `".WPSC_TABLE_PURCHASE_LOGS."` SET `processed`= '5' WHERE `sessionid`=".$sessionid;
  193. $wpdb->query($sql);
  194. $transact_url = get_option('checkout_url');
  195. $paypal_account_error = false;
  196. $paypal_error_codes = array('10500','10501','10507','10548','10549','10550','10552','10758','10760','15003');
  197. foreach($paypal_error_codes as $error_code) {
  198. if(in_array($error_code, $response->errorcodes)) {
  199. $paypal_account_error = true;
  200. break;
  201. }
  202. }
  203. if($paypal_account_error == true) {
  204. $_SESSION['wpsc_checkout_misc_error_messages'][] = __('There is a problem with your PayPal account configuration, please contact PayPal for further information.');
  205. foreach($response->longerror as $paypal_error) {
  206. $_SESSION['wpsc_checkout_misc_error_messages'][] = $paypal_error;
  207. }
  208. } else {
  209. $_SESSION['wpsc_checkout_misc_error_messages'][] = __('Sorry your transaction did not go through to Paypal successfully, please try again.');
  210. }
  211. $_SESSION['paypalpro'] = 'fail';
  212. }
  213. //exit('<pre>'.print_r($response, true).'</pre>');
  214. }
  215. function send ($transaction) {
  216. $connection = curl_init();
  217. if (get_option('paypal_pro_testmode') == "on"){
  218. curl_setopt($connection,CURLOPT_URL,"https://api-3t.sandbox.paypal.com/nvp"); // Sandbox testing
  219. }else{
  220. curl_setopt($connection,CURLOPT_URL,"https://api-3t.paypal.com/nvp"); // Live
  221. }
  222. $useragent = 'WP e-Commerce plugin';
  223. curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
  224. curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
  225. curl_setopt($connection, CURLOPT_NOPROGRESS, 1);
  226. curl_setopt($connection, CURLOPT_VERBOSE, 1);
  227. curl_setopt($connection, CURLOPT_FOLLOWLOCATION,0);
  228. curl_setopt($connection, CURLOPT_POST, 1);
  229. curl_setopt($connection, CURLOPT_POSTFIELDS, $transaction);
  230. curl_setopt($connection, CURLOPT_TIMEOUT, 30);
  231. curl_setopt($connection, CURLOPT_USERAGENT, $useragent);
  232. curl_setopt($connection, CURLOPT_REFERER, "https://".$_SERVER['SERVER_NAME']);
  233. curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
  234. $buffer = curl_exec($connection);
  235. curl_close($connection);
  236. //echo $buffer;
  237. $Response = response($buffer);
  238. return $Response;
  239. }
  240. function response ($buffer) {
  241. $_ = new stdClass();
  242. $r = array();
  243. $pairs = split("&",$buffer);
  244. foreach($pairs as $pair) {
  245. list($key,$value) = split("=",$pair);
  246. if (preg_match("/(\w*?)(\d+)/",$key,$matches)) {
  247. if (!isset($r[$matches[1]])) $r[$matches[1]] = array();
  248. $r[$matches[1]][$matches[2]] = urldecode($value);
  249. } else $r[$key] = urldecode($value);
  250. }
  251. $_->ack = $r['ACK'];
  252. $_->errorcodes = $r['L_ERRORCODE'];
  253. $_->shorterror = $r['L_SHORTMESSAGE'];
  254. $_->longerror = $r['L_LONGMESSAGE'];
  255. $_->severity = $r['L_SEVERITYCODE'];
  256. $_->timestamp = $r['TIMESTAMP'];
  257. $_->correlationid = $r['CORRELATIONID'];
  258. $_->version = $r['VERSION'];
  259. $_->build = $r['BUILD'];
  260. $_->transactionid = $r['TRANSACTIONID'];
  261. $_->amt = $r['AMT'];
  262. $_->avscode = $r['AVSCODE'];
  263. $_->cvv2match = $r['CVV2MATCH'];
  264. return $_;
  265. }
  266. function submit_paypal_pro(){
  267. //exit('<pre>'.print_r($_POST, true).'</pre>');
  268. if($_POST['PayPalPro']['username'] != null) {
  269. update_option('paypal_pro_username', $_POST['PayPalPro']['username']);
  270. }
  271. if($_POST['PayPalPro']['password'] != null) {
  272. update_option('paypal_pro_password', $_POST['PayPalPro']['password']);
  273. }
  274. if($_POST['PayPalPro']['signature'] != null) {
  275. update_option('paypal_pro_signature', $_POST['PayPalPro']['signature']);
  276. }
  277. if($_POST['PayPalPro']['testmode'] != null) {
  278. update_option('paypal_pro_testmode', $_POST['PayPalPro']['testmode']);
  279. }
  280. return true;
  281. }
  282. function form_paypal_pro(){
  283. if(get_option('paypal_pro_testmode') == "on"){
  284. $selected = 'checked="checked"';
  285. }else{
  286. $selected = '';
  287. }
  288. $output = '
  289. <tr>
  290. <td>
  291. <label for="paypal_pro_username">'.__('API Username:').'</label>
  292. </td>
  293. <td>
  294. <input type="text" name="PayPalPro[username]" id="paypal_pro_username" value="'.get_option("paypal_pro_username").'" size="30" />
  295. </td>
  296. </tr>
  297. <tr>
  298. <td>
  299. <label for="paypal_pro_password">'.__('API Password:').'</label>
  300. </td>
  301. <td>
  302. <input type="password" name="PayPalPro[password]" id="paypal_pro_password" value="'.get_option('paypal_pro_password').'" size="16" />
  303. </td>
  304. </tr>
  305. <tr>
  306. <td>
  307. <label for="paypal_pro_signature">'.__('API Signature:').'</label>
  308. </td>
  309. <td>
  310. <input type="text" name="PayPalPro[signature]" id="paypal_pro_signature" value="'.get_option('paypal_pro_signature').'" size="48" />
  311. </td>
  312. </tr>
  313. <tr>
  314. <td>
  315. <label for="paypal_pro_testmode">'.__('Test Mode Enabled:').'</label>
  316. </td>
  317. <td>
  318. <input type="hidden" name="PayPalPro[testmode]" value="off" /><input type="checkbox" name="PayPalPro[testmode]" id="paypal_pro_testmode" value="on" '.$selected.' />
  319. </td>
  320. </tr>';
  321. return $output;
  322. }
  323. ?>