PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-e-commerce/merchants/paypal-standard.merchant.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 335 lines | 231 code | 52 blank | 52 comment | 29 complexity | 88099ff441bdc8ee04600cff7fb733a9 MD5 | raw file
  1. <?php
  2. /**
  3. */
  4. $nzshpcrt_gateways[$num] = array(
  5. 'name' => 'Paypal Payments Standard 2.0',
  6. 'api_version' => 2.0,
  7. 'class_name' => 'wpsc_merchant_paypal_standard',
  8. 'has_recurring_billing' => true,
  9. 'wp_admin_cannot_cancel' => true,
  10. 'requirements' => array(
  11. /// so that you can restrict merchant modules to PHP 5, if you use PHP 5 features
  12. 'php_version' => 4.3,
  13. /// for modules that may not be present, like curl
  14. 'extra_modules' => array()
  15. ),
  16. // this may be legacy, not yet decided
  17. 'internalname' => 'wpsc_merchant_paypal_standard',
  18. // All array members below here are legacy, and use the code in paypal_multiple.php
  19. 'form' => 'form_paypal_multiple',
  20. 'submit_function' => 'submit_paypal_multiple',
  21. 'payment_type' => 'paypal',
  22. 'supported_currencies' => array(
  23. 'currency_list' => array('AUD', 'BRL', 'CAD', 'CHF', 'CZK', 'DKK', 'EUR', 'GBP', 'HKD', 'HUF', 'ILS', 'JPY', 'MXN', 'MYR', 'NOK', 'NZD', 'PHP', 'PLN', 'SEK', 'SGD', 'THB', 'TWD', 'USD'),
  24. 'option_name' => 'paypal_curcode'
  25. )
  26. );
  27. /**
  28. * WP eCommerce Paypal Standard Merchant Class
  29. *
  30. * This is the paypal standard merchant class, it extends the base merchant class
  31. *
  32. * @package wp-e-commerce
  33. * @since 3.7.6
  34. * @subpackage wpsc-merchants
  35. */
  36. class wpsc_merchant_paypal_standard extends wpsc_merchant {
  37. var $name = 'Paypal Payments Standard';
  38. var $paypal_ipn_values = array();
  39. /**
  40. * construct value array method, converts the data gathered by the base class code to something acceptable to the gateway
  41. * @access public
  42. */
  43. function construct_value_array() {
  44. //$collected_gateway_data
  45. $paypal_vars = array();
  46. // Store settings to be sent to paypal
  47. $paypal_vars += array(
  48. 'business' => get_option('paypal_multiple_business'),
  49. 'return' => add_query_arg('sessionid', $this->cart_data['session_id'], $this->cart_data['transaction_results_url']),
  50. 'cancel_return' => $this->cart_data['transaction_results_url'],
  51. 'notify_url' => add_query_arg('gateway', 'wpsc_merchant_paypal_standard', $this->cart_data['notification_url']),
  52. 'rm' => '2',
  53. 'currency_code' => $this->cart_data['store_currency'],
  54. 'lc' => $this->cart_data['store_currency'],
  55. 'bn' => $this->cart_data['software_name'],
  56. 'no_note' => '1',
  57. 'charset' => 'utf-8'
  58. );
  59. //used to send shipping
  60. if((int)(bool)get_option('paypal_ship') == 1) {
  61. $paypal_vars += array(
  62. 'address_override' => '1',
  63. 'no_shipping' => '0'
  64. );
  65. }
  66. // User settings to be sent to paypal
  67. $paypal_vars += array(
  68. 'email' => $this->cart_data['email_address'],
  69. 'first_name' => $this->cart_data['shipping_address']['first_name'],
  70. 'last_name' => $this->cart_data['shipping_address']['last_name'],
  71. 'address1' => $this->cart_data['shipping_address']['address'],
  72. 'city' => $this->cart_data['shipping_address']['city'],
  73. 'country' => $this->cart_data['shipping_address']['country'],
  74. 'zip' => $this->cart_data['shipping_address']['post_code']
  75. );
  76. if($this->cart_data['shipping_address']['state'] != '') {
  77. $paypal_vars += array(
  78. 'state' => $this->cart_data['shipping_address']['state']
  79. );
  80. }
  81. // Order settings to be sent to paypal
  82. $paypal_vars += array(
  83. //'tax' => '',
  84. //'custom' => '',
  85. 'invoice' => $this->cart_data['session_id']
  86. );
  87. if($this->cart_data['is_subscription'] == true) {
  88. $reprocessed_cart_data['shopping_cart'] = array(
  89. 'is_used' => false,
  90. 'price' => 0,
  91. 'length' => 1,
  92. 'unit' => 'd',
  93. 'times_to_rebill' => 1
  94. );
  95. $reprocessed_cart_data['subscription'] = array(
  96. 'is_used' => false,
  97. 'price' => 0,
  98. 'length' => 1,
  99. 'unit' => 'D',
  100. 'times_to_rebill' => 1
  101. );
  102. foreach($this->cart_items as $cart_row) {
  103. if($cart_row['is_recurring'] == true) {
  104. $reprocessed_cart_data['subscription']['is_used'] = true;
  105. $reprocessed_cart_data['subscription']['price'] = $cart_row['price'];
  106. $reprocessed_cart_data['subscription']['length'] = $cart_row['recurring_data']['rebill_interval']['length'];
  107. $reprocessed_cart_data['subscription']['unit'] = strtoupper($cart_row['recurring_data']['rebill_interval']['unit']);
  108. $reprocessed_cart_data['subscription']['times_to_rebill'] =$cart_row['recurring_data']['times_to_rebill'];
  109. } else {
  110. $item_cost = ($cart_row['price'] + $cart_row['shipping'] + $cart_row['tax']) + $cart_row['quantity'];
  111. if($item_cost > 0) {
  112. $reprocessed_cart_data['shopping_cart']['price'] += $item_cost;
  113. $reprocessed_cart_data['shopping_cart']['is_used'] = true;
  114. }
  115. }
  116. $paypal_vars += array(
  117. "item_name" => __('Your Subscription', 'wpsc'),
  118. "src" => "1" // I fail to see the point of sending a subscription to paypal as a subscription if it does not recur, if (src == 0) then (this == underfeatured waste of time)
  119. );
  120. // this can be false, we don't need to have additional items in the cart
  121. if($reprocessed_cart_data['shopping_cart']['is_used'] == true) {
  122. $paypal_vars += array(
  123. "a1" => $this->format_price($reprocessed_cart_data['shopping_cart']['price']),
  124. "p1" => $reprocessed_cart_data['shopping_cart']['length'],
  125. "t1" => $reprocessed_cart_data['shopping_cart']['unit'],
  126. );
  127. }
  128. //we need at least one subscription product, if we are in thise piece of code and this is not true, something is rather wrong
  129. if($reprocessed_cart_data['subscription']['is_used'] == true) {
  130. $paypal_vars += array(
  131. "a3" => $this->format_price($reprocessed_cart_data['subscription']['price']),
  132. "p3" => $reprocessed_cart_data['subscription']['length'],
  133. "t3" => $reprocessed_cart_data['subscription']['unit'],
  134. );
  135. // If the srt value for the number of times to rebill is not greater than 1, paypal won't accept the transaction.
  136. if($reprocessed_cart_data['subscription']['times_to_rebill'] > 1) {
  137. $paypal_vars += array("srt" => $reprocessed_cart_data['subscription']['times_to_rebill']);
  138. }
  139. }
  140. }
  141. } else {
  142. // Stick the cart item values together here
  143. $i = 1;
  144. foreach($this->cart_items as $cart_row) {
  145. $paypal_vars += array(
  146. "item_name_$i" => $cart_row['name'],
  147. "amount_$i" => $this->format_price($cart_row['price']),
  148. "tax_$i" => $this->format_price($cart_row['tax']),
  149. "quantity_$i" => $cart_row['quantity'],
  150. "item_number_$i" => $cart_row['product_id'],
  151. "shipping_$i" => $this->format_price($cart_row['shipping']), // additional shipping for the the (first item / total of the items)
  152. "shipping2_$i" => $this->format_price($cart_row['shipping']), // additional shipping beyond the first item
  153. "handling_$i" => '',
  154. );
  155. ++$i;
  156. }
  157. }
  158. // Payment Type settings to be sent to paypal
  159. if($this->cart_data['is_subscription'] == true) {
  160. $paypal_vars += array(
  161. 'cmd'=> '_xclick-subscriptions'
  162. );
  163. } else {
  164. $paypal_vars += array(
  165. 'upload' => '1',
  166. 'cmd' => '_ext-enter',
  167. 'redirect_cmd' => '_cart'
  168. );
  169. }
  170. $this->collected_gateway_data = $paypal_vars;
  171. }
  172. /**
  173. * submit method, sends the received data to the payment gateway
  174. * @access public
  175. */
  176. function submit() {
  177. $name_value_pairs = array();
  178. foreach($this->collected_gateway_data as $key=>$value) {
  179. //$output .= $key.'='.urlencode($value).$amp;
  180. $name_value_pairs[]= $key.'='.urlencode($value);
  181. }
  182. $gateway_values = implode('&', $name_value_pairs);
  183. //if(defined('WPSC_ADD_DEBUG_PAGE') and (WPSC_ADD_DEBUG_PAGE == true) ) {
  184. echo "<a href='".get_option('paypal_multiple_url')."?".$gateway_values."'>Test the URL here</a>";
  185. //echo "<pre>".print_r($this->cart_items,true)."</pre>";
  186. echo "<pre>".print_r($this->collected_gateway_data,true)."</pre>";
  187. exit();
  188. //}
  189. //exit('<pre>'.print_r($gateway_values, true).'</pre>');
  190. header("Location: ".get_option('paypal_multiple_url')."?".$gateway_values);
  191. exit();
  192. }
  193. /**
  194. * parse_gateway_notification method, receives data from the payment gateway
  195. * @access private
  196. */
  197. function parse_gateway_notification() {
  198. /// PayPal first expects the IPN variables to be returned to it within 30 seconds, so we do this first.
  199. $paypal_url = get_option('paypal_multiple_url');
  200. $received_values = array();
  201. $received_values['cmd'] = '_notify-validate';
  202. $received_values += $_POST;
  203. $options = array(
  204. 'timeout' => 5,
  205. 'body' => $received_values,
  206. 'user-agent' => ('WP e-Commerce/'.WPSC_PRESENTABLE_VERSION)
  207. );
  208. $response = wp_remote_post($paypal_url, $options);
  209. if(strpos($response['body'], 'VERIFIED') !== false) {
  210. $this->paypal_ipn_values = $received_values;
  211. $this->session_id = $received_values['invoice'];
  212. } else {
  213. exit("IPN Request Failure");
  214. }
  215. }
  216. /**
  217. * process_gateway_notification method, receives data from the payment gateway
  218. * @access public
  219. */
  220. function process_gateway_notification() {
  221. // Compare the received store owner email address to the set one
  222. if(strtolower($this->paypal_ipn_values['receiver_email']) == strtolower(get_option('paypal_multiple_business'))) {
  223. switch($this->paypal_ipn_values['txn_type']) {
  224. case 'cart':
  225. case 'express_checkout':
  226. if((float)$this->paypal_ipn_values['mc_gross'] == (float)$this->cart_data['total_price']) {
  227. $this->set_transaction_details($this->paypal_ipn_values['txn_id'], 2);
  228. transaction_results($this->cart_data['session_id'],false);
  229. }
  230. break;
  231. case 'subscr_signup':
  232. case 'subscr_payment':
  233. $this->set_transaction_details($this->paypal_ipn_values['subscr_id'], 2);
  234. foreach($this->cart_items as $cart_row) {
  235. if($cart_row['is_recurring'] == true) {
  236. do_action('wpsc_activate_subscription', $cart_row['cart_item_id'], $this->paypal_ipn_values['subscr_id']);
  237. }
  238. }
  239. transaction_results($this->cart_data['session_id'],false);
  240. break;
  241. case 'subscr_cancel':
  242. case 'subscr_eot':
  243. case 'subscr_failed':
  244. foreach($this->cart_items as $cart_row) {
  245. $altered_count = 0;
  246. if((bool)$cart_row['is_recurring'] == true) {
  247. $altered_count++;
  248. wpsc_update_cartmeta($cart_row['cart_item_id'], 'is_subscribed', 0);
  249. }
  250. }
  251. break;
  252. default:
  253. break;
  254. }
  255. }
  256. $message = "
  257. {$this->paypal_ipn_values['receiver_email']} => ".get_option('paypal_multiple_business')."
  258. {$this->paypal_ipn_values['txn_type']}
  259. {$this->paypal_ipn_values['mc_gross']} => {$this->cart_data['total_price']}
  260. {$this->paypal_ipn_values['txn_id']}
  261. ".print_r($this->cart_items, true)."
  262. {$altered_count}
  263. ";
  264. //mail('thomas.howard@gmail.com', "IPN Debugging", $message);
  265. }
  266. function format_price($price) {
  267. $paypal_currency_code = get_option('paypal_curcode');
  268. switch($paypal_currency_code) {
  269. case "JPY":
  270. $decimal_places = 0;
  271. break;
  272. case "HUF":
  273. $decimal_places = 0;
  274. default:
  275. $decimal_places = 2;
  276. break;
  277. }
  278. $price = number_format(sprintf("%01.2f",$price),$decimal_places,'.','');
  279. return $price;
  280. }
  281. }
  282. ?>