/app/gateway/paypalstandard/view/class-ms-gateway-paypalstandard-view-button.php

  1. <?php 
  2.  
  3. class MS_Gateway_Paypalstandard_View_Button extends MS_View { 
  4.  
  5. public function to_html() { 
  6. $fields = $this->prepare_fields(); 
  7. $subscription = $this->data['ms_relationship']; 
  8. $invoice = $subscription->get_current_invoice(); 
  9. $gateway = $this->data['gateway']; 
  10.  
  11. $action_url = apply_filters( 
  12. 'ms_gateway_paypalstandard_view_button_form_action_url',  
  13. $this->data['action_url'] 
  14. ); 
  15.  
  16. $row_class = 'gateway_' . $gateway->id; 
  17. if ( ! $gateway->is_live_mode() ) { 
  18. $row_class .= ' sandbox-mode'; 
  19.  
  20. ob_start(); 
  21. ?> 
  22. <form action="<?php echo esc_url( $action_url ); ?>" method="post" id="ms-paypal-form"> 
  23. <?php 
  24. foreach ( $fields as $field ) { 
  25. MS_Helper_Html::html_element( $field ); 
  26. ?> 
  27. <img alt="" border="0" width="1" height="1" src="https://www.paypal.com/en_US/i/scr/pixel.gif" > 
  28. </form> 
  29. <?php 
  30. $payment_form = apply_filters( 
  31. 'ms_gateway_form',  
  32. ob_get_clean(),  
  33. $gateway,  
  34. $invoice,  
  35. $this 
  36. ); 
  37.  
  38. ob_start(); 
  39. ?> 
  40. <tr class="<?php echo esc_attr( $row_class ); ?>"> 
  41. <td class="ms-buy-now-column" colspan="2"> 
  42. <?php echo $payment_form; ?> 
  43. </td> 
  44. </tr> 
  45. <?php 
  46. $html = ob_get_clean(); 
  47.  
  48. $html = apply_filters( 
  49. 'ms_gateway_button-' . $gateway->id,  
  50. $html,  
  51. $this 
  52. ); 
  53.  
  54. $html = apply_filters( 
  55. 'ms_gateway_button',  
  56. $html,  
  57. $gateway->id,  
  58. $this 
  59. ); 
  60.  
  61. return $html; 
  62.  
  63. /** 
  64. * Prepare the PayPal IPN fields 
  65. * 
  66. * Details here: 
  67. * https://developer.paypal.com/docs/classic/paypal-payments-standard/integration-guide/Appx_websitestandard_htmlvariables/ 
  68. * 
  69. * @since 1.0.0 
  70. * @return array 
  71. */ 
  72. private function prepare_fields() { 
  73. $subscription = $this->data['ms_relationship']; 
  74. $membership = $subscription->get_membership(); 
  75.  
  76. if ( 0 === $membership->price ) { 
  77. return; 
  78.  
  79. $gateway = $this->data['gateway']; 
  80.  
  81. $invoice = $subscription->get_current_invoice(); 
  82. $regular_invoice = null; 
  83. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  84.  
  85. $nonce = wp_create_nonce( 
  86. $gateway->id. '_' . $subscription->id 
  87. ); 
  88. $cancel_url = MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_REGISTER ); 
  89. $return_url = esc_url_raw( 
  90. add_query_arg( 
  91. array( 'ms_relationship_id' => $subscription->id ),  
  92. MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_REG_COMPLETE, false ) 
  93. ); 
  94.  
  95. $fields = array( 
  96. '_wpnonce' => array( 
  97. 'id' => '_wpnonce',  
  98. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  99. 'value' => $nonce,  
  100. ),  
  101. 'charset' => array( 
  102. 'id' => 'charset',  
  103. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  104. 'value' => 'utf-8',  
  105. ),  
  106. 'business' => array( 
  107. 'id' => 'business',  
  108. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  109. 'value' => $gateway->merchant_id,  
  110. ),  
  111. 'cmd' => array( 
  112. 'id' => 'cmd',  
  113. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  114. 'value' => '_xclick-subscriptions',  
  115. ),  
  116. 'bn' => array( 
  117. 'id' => 'bn',  
  118. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  119. 'value' => 'incsub_SP',  
  120. ),  
  121. 'item_name' => array( 
  122. 'id' => 'item_name',  
  123. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  124. 'value' => $membership->name,  
  125. ),  
  126. 'no_shipping' => array( 
  127. 'id' => 'no_shipping',  
  128. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  129. 'value' => 1,  
  130. ),  
  131. 'item_number' => array( 
  132. 'id' => 'item_number',  
  133. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  134. 'value' => $membership->id,  
  135. ),  
  136. 'currency_code' => array( 
  137. 'id' => 'currency_code',  
  138. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  139. 'value' => MS_Plugin::instance()->settings->currency,  
  140. ),  
  141. 'return' => array( 
  142. 'id' => 'return',  
  143. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  144. 'value' => $return_url,  
  145. ),  
  146. 'cancel_return' => array( 
  147. 'id' => 'cancel_return',  
  148. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  149. 'value' => $cancel_url,  
  150. ),  
  151. 'notify_url' => array( 
  152. 'id' => 'notify_url',  
  153. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  154. 'value' => $gateway->get_return_url(),  
  155. ),  
  156. 'country' => array( 
  157. 'id' => 'country',  
  158. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  159. 'value' => $gateway->paypal_site,  
  160. ),  
  161. 'no_note' => array( 
  162. 'id' => 'no_note',  
  163. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  164. 'value' => 1,  
  165. ),  
  166. 'no_shipping' => array( 
  167. 'id' => 'no_shipping',  
  168. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  169. 'value' => 1,  
  170. ),  
  171. 'invoice' => array( 
  172. 'id' => 'invoice',  
  173. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  174. 'value' => $invoice->id,  
  175. ),  
  176. ); 
  177.  
  178. $fields['submit'] = array( 
  179. 'id' => 'submit',  
  180. 'type' => MS_Helper_Html::INPUT_TYPE_IMAGE,  
  181. 'value' => 'https://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif',  
  182. 'alt' => __( 'PayPal - The safer, easier way to pay online', 'membership2' ),  
  183. ); 
  184.  
  185. // custom pay button defined in gateway settings 
  186. $custom_label = $gateway->pay_button_url; 
  187. if ( ! empty( $custom_label ) ) { 
  188. if ( false !== strpos( $custom_label, '://' ) ) { 
  189. $fields['submit']['value'] = $custom_label; 
  190. } else { 
  191. $fields['submit'] = array( 
  192. 'id' => 'submit',  
  193. 'type' => MS_Helper_Html::INPUT_TYPE_SUBMIT,  
  194. 'value' => $custom_label,  
  195. ); 
  196.  
  197. // Trial period 
  198. if ( $subscription->is_trial_eligible() ) { 
  199. $fields['a1'] = array( 
  200. 'id' => 'a1',  
  201. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  202. 'value' => $invoice->trial_price,  
  203. ); 
  204.  
  205. $trial_type = MS_Helper_Period::get_period_value( 
  206. $membership->trial_period,  
  207. 'period_type' 
  208. ); 
  209. $trial_type = strtoupper( $trial_type[0] ); 
  210. $trial_value = MS_Helper_Period::get_period_value( 
  211. $membership->trial_period,  
  212. 'period_unit' 
  213. ); 
  214. $trial_value = MS_Helper_Period::validate_range( 
  215. $trial_value,  
  216. $trial_type 
  217. ); 
  218.  
  219. $fields['p1'] = array( 
  220. 'id' => 'p1',  
  221. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  222. 'value' => $trial_value,  
  223. ); 
  224.  
  225. $fields['t1'] = array( 
  226. 'id' => 't1',  
  227. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  228. 'value' => $trial_type,  
  229. ); 
  230.  
  231. // Membership price 
  232. $membership_price = $invoice->total; 
  233. $membership_price = MS_Helper_Billing::format_price( $membership_price ); 
  234.  
  235. $fields['a3'] = array( 
  236. 'id' => 'a3',  
  237. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  238. 'value' => $membership_price,  
  239. ); 
  240. $fields['amount'] = array( 
  241. 'id' => 'amount',  
  242. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  243. 'value' => $membership_price,  
  244. ); 
  245.  
  246. $recurring = 0; 
  247. switch ( $membership->payment_type ) { 
  248. // == RECURRING PAYMENTS 
  249. case MS_Model_Membership::PAYMENT_TYPE_RECURRING: 
  250. $period_type = MS_Helper_Period::get_period_value( 
  251. $membership->pay_cycle_period,  
  252. 'period_type' 
  253. ); 
  254. $period_type = strtoupper( $period_type[0] ); 
  255. $period_value = MS_Helper_Period::get_period_value( 
  256. $membership->pay_cycle_period,  
  257. 'period_unit' 
  258. ); 
  259. $period_value = MS_Helper_Period::validate_range( 
  260. $period_value,  
  261. $period_type 
  262. ); 
  263.  
  264. $fields['p3'] = array( 
  265. 'id' => 'p3',  
  266. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  267. 'value' => $period_value,  
  268. ); 
  269. $fields['t3'] = array( 
  270. 'id' => 't3',  
  271. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  272. 'value' => $period_type,  
  273. ); 
  274.  
  275. $custom_period_type = isset( $period_type ) ? $period_type : ''; 
  276. $custom_period_value = isset( $period_value ) ? $period_value : ''; 
  277.  
  278. // This makes the payments recurring! 
  279. $recurring = 1; 
  280. break; 
  281.  
  282. // == FINITE END DATE 
  283. case MS_Model_Membership::PAYMENT_TYPE_FINITE: 
  284. $period_type = MS_Helper_Period::get_period_value( 
  285. $membership->period,  
  286. 'period_type' 
  287. ); 
  288. $period_type = strtoupper( $period_type[0] ); 
  289. $period_value = MS_Helper_Period::get_period_value( 
  290. $membership->period,  
  291. 'period_unit' 
  292. ); 
  293. $period_value = MS_Helper_Period::validate_range( 
  294. $period_value,  
  295. $period_type 
  296. ); 
  297.  
  298. $fields['p3'] = array( 
  299. 'id' => 'p3',  
  300. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  301. 'value' => $period_value,  
  302. ); 
  303. $fields['t3'] = array( 
  304. 'id' => 't3',  
  305. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  306. 'value' => $period_type,  
  307. ); 
  308.  
  309. $custom_period_type = isset( $period_type ) ? $period_type : ''; 
  310. $custom_period_value = isset( $period_value ) ? $period_value : ''; 
  311. break; 
  312.  
  313. // == DATE RANGE 
  314. case MS_Model_Membership::PAYMENT_TYPE_DATE_RANGE: 
  315. $period_value = MS_Helper_Period::subtract_dates( 
  316. $membership->period_date_end,  
  317. $membership->period_date_start 
  318. ); 
  319. $period_value = MS_Helper_Period::validate_range( 
  320. $period_value,  
  321. 'D' 
  322. ); 
  323.  
  324. $fields['p3'] = array( 
  325. 'id' => 'p3',  
  326. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  327. 'value' => $period_value,  
  328. ); 
  329. $fields['t3'] = array( 
  330. 'id' => 't3',  
  331. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  332. 'value' => 'D',  
  333. ); 
  334.  
  335. $custom_period_type = isset( $period_type ) ? $period_type : ''; 
  336. $custom_period_value = isset( $period_value ) ? $period_value : ''; 
  337. break; 
  338.  
  339. // == PERMANENT 
  340. case MS_Model_Membership::PAYMENT_TYPE_PERMANENT: 
  341. /** 
  342. * Permanent membership: Set the subscription range to 5 years! 
  343. * PayPal requires us to provide the subscription range and the 
  344. * maximum possible value is 5 years. 
  345. */ 
  346. $period_value = MS_Helper_Period::validate_range( 
  347. 5,  
  348. 'Y' 
  349. ); 
  350.  
  351. $fields['p3'] = array( 
  352. 'id' => 'p3',  
  353. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  354. 'value' => $period_value,  
  355. ); 
  356. $fields['t3'] = array( 
  357. 'id' => 't3',  
  358. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  359. 'value' => 'Y',  
  360. ); 
  361.  
  362. $custom_period_type = isset( $period_type ) ? $period_type : ''; 
  363. $custom_period_value = isset( $period_value ) ? $period_value : ''; 
  364. break; 
  365.  
  366. if ( 1 == $recurring ) { 
  367. if ( 1 == $membership->pay_cycle_repetitions ) { 
  368. $recurring = 0; 
  369. } elseif ( $membership->pay_cycle_repetitions > 1 ) { 
  370. /** 
  371. * Recurring times. 
  372. * The number of times that a recurring payment is made. 
  373. */ 
  374. $fields['srt'] = array( 
  375. 'id' => 'srt',  
  376. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  377. 'value' => $membership->pay_cycle_repetitions,  
  378. ); 
  379.  
  380. /** 
  381. * Recurring field. 
  382. * 0 - one time payment 
  383. * 1 - recurring payments 
  384. */ 
  385. $fields['src'] = array( 
  386. 'id' => 'src',  
  387. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  388. 'value' => $recurring,  
  389. ); 
  390.  
  391. /** 
  392. * Modify current subscription field. 
  393. * value != 0 does not allow trial period. 
  394. * 0 - allows subscribers only to sign up for new subscriptions 
  395. * 1 - allows subscribers to sign up for new subscriptions and modify their current subscriptions 
  396. * 2 - allows subscribers to modify only their current subscriptions 
  397. */ 
  398. $modify = ! empty( $move_from_id ); 
  399. $fields['modify'] = array( 
  400. 'id' => 'modify',  
  401. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  402. 'value' => $modify ? 2 : 0,  
  403. ); 
  404.  
  405. $custom_interval = $fields['p3']['value'] . $fields['t3']['value']; 
  406. $custom_invoice_date = date( 'Y-m-d H:i:s' ); 
  407.  
  408. $fields['custom'] = array( 
  409. 'id' => 'custom',  
  410. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  411. 'value' => sprintf( 
  412. '%1$s | %2$s | %3$s (%4$s) | ms_id %5$s ',  
  413. $custom_invoice_date,  
  414. $membership_price,  
  415. $membership->payment_type,  
  416. $custom_interval,  
  417. $membership->id 
  418. ); 
  419.  
  420. // Added to return in correct URL 
  421. $fields['rm'] = array( 
  422. 'id' => 'rm',  
  423. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  424. 'value' => 2,  
  425. ); 
  426.  
  427. if ( $gateway->is_live_mode() ) { 
  428. $this->data['action_url'] = 'https://www.paypal.com/cgi-bin/webscr'; 
  429. } else { 
  430. $this->data['action_url'] = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; 
  431.  
  432. return apply_filters( 
  433. 'ms_gateway_paypalstandard_view_prepare_fields',  
  434. $fields,  
  435. $invoice 
  436. ); 
.