/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. 'item_number' => array( 
  127. 'id' => 'item_number',  
  128. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  129. 'value' => $membership->id,  
  130. ),  
  131. 'currency_code' => array( 
  132. 'id' => 'currency_code',  
  133. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  134. 'value' => MS_Plugin::instance()->settings->currency,  
  135. ),  
  136. 'return' => array( 
  137. 'id' => 'return',  
  138. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  139. 'value' => $return_url,  
  140. ),  
  141. 'cancel_return' => array( 
  142. 'id' => 'cancel_return',  
  143. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  144. 'value' => $cancel_url,  
  145. ),  
  146. 'notify_url' => array( 
  147. 'id' => 'notify_url',  
  148. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  149. 'value' => $gateway->get_return_url(),  
  150. ),  
  151. 'country' => array( 
  152. 'id' => 'country',  
  153. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  154. 'value' => $gateway->paypal_site,  
  155. ),  
  156. 'no_note' => array( 
  157. 'id' => 'no_note',  
  158. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  159. 'value' => 1,  
  160. ),  
  161. 'no_shipping' => array( 
  162. 'id' => 'no_shipping',  
  163. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  164. 'value' => 1,  
  165. ),  
  166. 'invoice' => array( 
  167. 'id' => 'invoice',  
  168. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  169. 'value' => $invoice->id,  
  170. ),  
  171. ); 
  172.  
  173. $fields['submit'] = array( 
  174. 'id' => 'submit',  
  175. 'type' => MS_Helper_Html::INPUT_TYPE_IMAGE,  
  176. 'value' => 'https://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif',  
  177. 'alt' => __( 'PayPal - The safer, easier way to pay online', 'membership2' ),  
  178. ); 
  179.  
  180. // custom pay button defined in gateway settings 
  181. $custom_label = $gateway->pay_button_url; 
  182. if ( ! empty( $custom_label ) ) { 
  183. if ( false !== strpos( $custom_label, '://' ) ) { 
  184. $fields['submit']['value'] = $custom_label; 
  185. } else { 
  186. $fields['submit'] = array( 
  187. 'id' => 'submit',  
  188. 'type' => MS_Helper_Html::INPUT_TYPE_SUBMIT,  
  189. 'value' => $custom_label,  
  190. ); 
  191.  
  192. // Trial period 
  193. if ( $subscription->is_trial_eligible() ) { 
  194. $fields['a1'] = array( 
  195. 'id' => 'a1',  
  196. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  197. 'value' => $invoice->trial_price,  
  198. ); 
  199.  
  200. $trial_type = MS_Helper_Period::get_period_value( 
  201. $membership->trial_period,  
  202. 'period_type' 
  203. ); 
  204. $trial_type = strtoupper( $trial_type[0] ); 
  205. $trial_value = MS_Helper_Period::get_period_value( 
  206. $membership->trial_period,  
  207. 'period_unit' 
  208. ); 
  209. $trial_value = MS_Helper_Period::validate_range( 
  210. $trial_value,  
  211. $trial_type 
  212. ); 
  213.  
  214. $fields['p1'] = array( 
  215. 'id' => 'p1',  
  216. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  217. 'value' => $trial_value,  
  218. ); 
  219.  
  220. $fields['t1'] = array( 
  221. 'id' => 't1',  
  222. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  223. 'value' => $trial_type,  
  224. ); 
  225.  
  226. // Membership price 
  227. $membership_price = $invoice->total; 
  228. $membership_price = MS_Helper_Billing::format_price( $membership_price ); 
  229.  
  230. $fields['a3'] = array( 
  231. 'id' => 'a3',  
  232. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  233. 'value' => $membership_price,  
  234. ); 
  235. $fields['amount'] = array( 
  236. 'id' => 'amount',  
  237. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  238. 'value' => $membership_price,  
  239. ); 
  240.  
  241. $recurring = 0; 
  242. switch ( $membership->payment_type ) { 
  243. // == RECURRING PAYMENTS 
  244. case MS_Model_Membership::PAYMENT_TYPE_RECURRING: 
  245. $period_type = MS_Helper_Period::get_period_value( 
  246. $membership->pay_cycle_period,  
  247. 'period_type' 
  248. ); 
  249. $period_type = strtoupper( $period_type[0] ); 
  250. $period_value = MS_Helper_Period::get_period_value( 
  251. $membership->pay_cycle_period,  
  252. 'period_unit' 
  253. ); 
  254. $period_value = MS_Helper_Period::validate_range( 
  255. $period_value,  
  256. $period_type 
  257. ); 
  258.  
  259. $fields['p3'] = array( 
  260. 'id' => 'p3',  
  261. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  262. 'value' => $period_value,  
  263. ); 
  264. $fields['t3'] = array( 
  265. 'id' => 't3',  
  266. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  267. 'value' => $period_type,  
  268. ); 
  269.  
  270. // This makes the payments recurring! 
  271. $recurring = 1; 
  272. break; 
  273.  
  274. // == FINITE END DATE 
  275. case MS_Model_Membership::PAYMENT_TYPE_FINITE: 
  276. $period_type = MS_Helper_Period::get_period_value( 
  277. $membership->period,  
  278. 'period_type' 
  279. ); 
  280. $period_type = strtoupper( $period_type[0] ); 
  281. $period_value = MS_Helper_Period::get_period_value( 
  282. $membership->period,  
  283. 'period_unit' 
  284. ); 
  285. $period_value = MS_Helper_Period::validate_range( 
  286. $period_value,  
  287. $period_type 
  288. ); 
  289.  
  290. $fields['p3'] = array( 
  291. 'id' => 'p3',  
  292. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  293. 'value' => $period_value,  
  294. ); 
  295. $fields['t3'] = array( 
  296. 'id' => 't3',  
  297. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  298. 'value' => $period_type,  
  299. ); 
  300. break; 
  301.  
  302. // == DATE RANGE 
  303. case MS_Model_Membership::PAYMENT_TYPE_DATE_RANGE: 
  304. $period_value = MS_Helper_Period::subtract_dates( 
  305. $membership->period_date_end,  
  306. $membership->period_date_start 
  307. ); 
  308. $period_value = MS_Helper_Period::validate_range( 
  309. $period_value,  
  310. 'D' 
  311. ); 
  312.  
  313. $fields['p3'] = array( 
  314. 'id' => 'p3',  
  315. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  316. 'value' => $period_value,  
  317. ); 
  318. $fields['t3'] = array( 
  319. 'id' => 't3',  
  320. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  321. 'value' => 'D',  
  322. ); 
  323. break; 
  324.  
  325. // == PERMANENT 
  326. case MS_Model_Membership::PAYMENT_TYPE_PERMANENT: 
  327. /** 
  328. * Permanent membership: Set the subscription range to 5 years! 
  329. * PayPal requires us to provide the subscription range and the 
  330. * maximum possible value is 5 years. 
  331. */ 
  332. $period_value = MS_Helper_Period::validate_range( 
  333. 5,  
  334. 'Y' 
  335. ); 
  336.  
  337. $fields['p3'] = array( 
  338. 'id' => 'p3',  
  339. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  340. 'value' => $period_value,  
  341. ); 
  342. $fields['t3'] = array( 
  343. 'id' => 't3',  
  344. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  345. 'value' => 'Y',  
  346. ); 
  347. break; 
  348.  
  349. if ( 1 == $recurring ) { 
  350. if ( 1 == $membership->pay_cycle_repetitions ) { 
  351. $recurring = 0; 
  352. } elseif ( $membership->pay_cycle_repetitions > 1 ) { 
  353. /** 
  354. * Recurring times. 
  355. * The number of times that a recurring payment is made. 
  356. */ 
  357. $fields['srt'] = array( 
  358. 'id' => 'srt',  
  359. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  360. 'value' => $membership->pay_cycle_repetitions,  
  361. ); 
  362.  
  363. /** 
  364. * Recurring field. 
  365. * 0 - one time payment 
  366. * 1 - recurring payments 
  367. */ 
  368. $fields['src'] = array( 
  369. 'id' => 'src',  
  370. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  371. 'value' => $recurring,  
  372. ); 
  373.  
  374. /** 
  375. * Modify current subscription field. 
  376. * value != 0 does not allow trial period. 
  377. * 0 - allows subscribers only to sign up for new subscriptions 
  378. * 1 - allows subscribers to sign up for new subscriptions and modify their current subscriptions 
  379. * 2 - allows subscribers to modify only their current subscriptions 
  380. */ 
  381. $modify = ! empty( $move_from_id ); 
  382. $fields['modify'] = array( 
  383. 'id' => 'modify',  
  384. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  385. 'value' => $modify ? 2 : 0,  
  386. ); 
  387.  
  388. if ( $gateway->is_live_mode() ) { 
  389. $this->data['action_url'] = 'https://www.paypal.com/cgi-bin/webscr'; 
  390. } else { 
  391. $this->data['action_url'] = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; 
  392.  
  393. return apply_filters( 
  394. 'ms_gateway_paypalstandard_view_prepare_fields',  
  395. $fields 
  396. ); 
.