MS_Gateway_Paypalstandard_View_Button

The Membership 2 MS Gateway Paypalstandard View Button class.

Defined (1)

The class is defined in the following location(s).

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