/app/view/frontend/class-ms-view-frontend-payment.php

  1. <?php 
  2.  
  3. class MS_View_Frontend_Payment extends MS_View { 
  4.  
  5. /** 
  6. * Returns the HTML code for the Purchase-Membership form. 
  7. * 
  8. * @since 1.0.0 
  9. * @return string 
  10. */ 
  11. public function to_html() { 
  12. $membership = $this->data['membership']; 
  13. $invoice = $this->data['invoice']; 
  14. $subscription = $this->data['ms_relationship']; 
  15.  
  16. $class = 'ms-alert-success'; 
  17. $msg = __( 
  18. 'Please check the details of the membership below and click ' . 
  19. 'on the relevant button to complete the signup.', 'membership2' 
  20. ); 
  21.  
  22. if ( ! empty( $this->data['error'] ) ) { 
  23. $class = 'ms-alert-error'; 
  24. $msg = $this->data['error']; 
  25.  
  26. /** 
  27. * Log the users IP and current timestamp inside the invoice. 
  28. * 
  29. * @since 1.0.2.0 
  30. */ 
  31. $invoice->checkout_ip = lib3()->net->current_ip()->ip; 
  32. $invoice->checkout_date = date( 'Y-m-d H:i:s' ); 
  33. $invoice->save(); 
  34.  
  35. $cancel_warning = false; 
  36. if ( ! MS_Model_Member::is_admin_user() ) { 
  37. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MULTI_MEMBERSHIPS ) ) { 
  38. // Member can only sign up to one membership. 
  39. $valid_status = array( 
  40. MS_Model_Relationship::STATUS_TRIAL,  
  41. MS_Model_Relationship::STATUS_ACTIVE,  
  42. MS_Model_Relationship::STATUS_PENDING,  
  43. ); 
  44.  
  45. foreach ( $this->data['member']->subscriptions as $tmp_subscription ) { 
  46. if ( $tmp_subscription->is_system() ) { continue; } 
  47. if ( in_array( $tmp_subscription->status, $valid_status ) ) { 
  48. $cancel_warning = __( 
  49. 'Your other Memberships will be cancelled when you complete this payment.',  
  50. 'membership2' 
  51. ); 
  52. break; 
  53. } elseif ( $subscription->move_from_id ) { 
  54. $move_from_ids = explode( ', ', $subscription->move_from_id ); 
  55. $names = array(); 
  56. foreach ( $move_from_ids as $id ) { 
  57. $ms = MS_Factory::load( 'MS_Model_Membership', $id ); 
  58. if ( $ms->is_system() ) { continue; } 
  59. $names[] = $ms->name; 
  60.  
  61. if ( 1 == count( $names ) ) { 
  62. $cancel_warning = sprintf( 
  63. __( 'When you complete this payment your Membership "%s" will be cancelled.', 'membership2' ),  
  64. $names[0] 
  65. ); 
  66. } elseif ( 1 < count( $names ) ) { 
  67. $cancel_warning = sprintf( 
  68. __( 'When you complete this payment the following Memberships will be cancelled: %s.', 'membership2' ),  
  69. implode( ', ', $names ) 
  70. ); 
  71.  
  72. // Check if the user goes through a trial period before first payment. 
  73. $is_trial = $invoice->uses_trial; 
  74.  
  75. $skip_form = ! MS_Model_Member::is_admin_user() 
  76. && ! $cancel_warning 
  77. && $membership->is_free(); 
  78.  
  79. /** 
  80. * Filter the flag to allow Add-ons like "Invitation codes" to override 
  81. * the state and force the form to display. 
  82. * 
  83. * @var bool 
  84. */ 
  85. $skip_form = apply_filters( 
  86. 'ms_view_frontend_payment_skip_form',  
  87. $skip_form,  
  88. $invoice,  
  89. $this 
  90. ); 
  91.  
  92. if ( $skip_form ) { 
  93. // No confirmation required. Simply register for this membership! 
  94. $args = array(); 
  95. $args['ms_relationship_id'] = $subscription->id; 
  96. $args['gateway'] = MS_Gateway_Free::ID; 
  97. $args['step'] = MS_Controller_Frontend::STEP_PROCESS_PURCHASE; 
  98. $args['_wpnonce'] = wp_create_nonce( $args['gateway'] . '_' . $args['ms_relationship_id'] ); 
  99. $url = esc_url_raw( add_query_arg( $args ) ); 
  100.  
  101. /** 
  102. * Very likely the html output has already began. 
  103. * So we redirect by using javascript. 
  104. */ 
  105. ?> 
  106. <script>window.location.href = '<?php echo $url; ?>';</script> 
  107. <?php 
  108. exit; 
  109.  
  110. $show_tax = MS_Model_Addon::is_enabled( MS_Addon_Taxamo::ID ); 
  111.  
  112. /** 
  113. * Trigger an action before the payment form is displayed. This hook 
  114. * can be used by Add-ons or plugins to initialize payment settings or 
  115. * add custom code. 
  116. */ 
  117. do_action( 'ms_view_frontend_payment_form_start', $invoice, $this ); 
  118.  
  119. $classes = array( 
  120. 'ms-membership-form-wrapper',  
  121. 'ms-subscription-' . $subscription->id,  
  122. 'ms-invoice-' . $invoice->id,  
  123. ); 
  124.  
  125. ob_start(); 
  126. ?> 
  127. <div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>"> 
  128. <legend><?php _e( 'Join Membership', 'membership2' ) ?></legend> 
  129. <p class="ms-alert-box <?php echo esc_attr( $class ); ?>"> 
  130. <?php echo $msg; ?> 
  131. </p> 
  132. <table class="ms-purchase-table"> 
  133. <tr> 
  134. <td class="ms-title-column"> 
  135. <?php _e( 'Name', 'membership2' ); ?> 
  136. </td> 
  137. <td class="ms-details-column"> 
  138. <?php echo esc_html( $membership->name ); ?> 
  139. </td> 
  140. </tr> 
  141.  
  142. <?php if ( $membership->description ) : ?> 
  143. <tr> 
  144. <td class="ms-title-column"> 
  145. <?php _e( 'Description', 'membership2' ); ?> 
  146. </td> 
  147. <td class="ms-desc-column"> 
  148. <span class="ms-membership-description"><?php 
  149. echo $membership->get_description(); 
  150. ?></span> 
  151. </td> 
  152. </tr> 
  153. <?php endif; ?> 
  154.  
  155. <?php if ( ! $membership->is_free() ) : ?> 
  156. <?php if ( $invoice->discount || $invoice->pro_rate || $invoice->tax_rate ) : ?> 
  157. <tr> 
  158. <td class="ms-title-column"> 
  159. <?php _e( 'Price', 'membership2' ); ?> 
  160. </td> 
  161. <td class="ms-details-column"> 
  162. <?php 
  163. if ( $membership->price > 0 ) { 
  164. printf( 
  165. '<span class="price">%s %s</span>',  
  166. $invoice->currency,  
  167. MS_Helper_Billing::format_price( $membership->price ) 
  168. ); 
  169. } else { 
  170. _e( 'Free', 'membership2' ); 
  171. ?> 
  172. </td> 
  173. </tr> 
  174. <?php endif; ?> 
  175.  
  176. <?php if ( $invoice->discount ) : ?> 
  177. <tr> 
  178. <td class="ms-title-column"> 
  179. <?php _e( 'Coupon Discount', 'membership2' ); ?> 
  180. </td> 
  181. <td class="ms-price-column"> 
  182. <?php 
  183. printf( 
  184. '%s -%s',  
  185. $invoice->currency,  
  186. MS_Helper_Billing::format_price( $invoice->discount ) 
  187. ); 
  188. ?> 
  189. </td> 
  190. </tr> 
  191. <?php endif; ?> 
  192.  
  193. <?php if ( $invoice->pro_rate ) : ?> 
  194. <tr> 
  195. <td class="ms-title-column"> 
  196. <?php _e( 'Pro-Rate Discount', 'membership2' ); ?> 
  197. </td> 
  198. <td class="ms-price-column"> 
  199. <?php 
  200. printf( 
  201. '%s -%s',  
  202. $invoice->currency,  
  203. MS_Helper_Billing::format_price( $invoice->pro_rate ) 
  204. ); 
  205. ?> 
  206. </td> 
  207. </tr> 
  208. <?php endif; ?> 
  209.  
  210. <?php if ( $show_tax ) : ?> 
  211. <tr> 
  212. <td class="ms-title-column"> 
  213. <?php 
  214. printf( 
  215. __( 'Taxes %s', 'membership2' ),  
  216. '<a href="#" class="ms-tax-editor"><small>(' . $invoice->tax_name . ')</small></a>' 
  217. ); 
  218. ?> 
  219. </td> 
  220. <td class="ms-price-column"> 
  221. <?php 
  222. printf( 
  223. '%s %s',  
  224. $invoice->currency,  
  225. MS_Helper_Billing::format_price( $invoice->tax ) 
  226. ); 
  227. ?> 
  228. </td> 
  229. </tr> 
  230. <?php endif; ?> 
  231.  
  232. <tr> 
  233. <td class="ms-title-column"> 
  234. <?php _e( 'Total', 'membership2' ); ?> 
  235. </td> 
  236. <td class="ms-price-column ms-total"> 
  237. <?php 
  238. if ( $invoice->total > 0 ) { 
  239. printf( 
  240. '<span class="price">%s %s</span>',  
  241. $invoice->currency,  
  242. MS_Helper_Billing::format_price( $invoice->total ) 
  243. ); 
  244. } else { 
  245. _e( 'Free', 'membership2' ); 
  246. ?> 
  247. </td> 
  248. </tr> 
  249.  
  250. <?php if ( $is_trial ) : ?> 
  251. <tr> 
  252. <td class="ms-title-column"> 
  253. <?php _e( 'Payment due', 'membership2' ); ?> 
  254. </td> 
  255. <td class="ms-desc-column"><?php 
  256. echo MS_Helper_Period::format_date( $invoice->due_date ); 
  257. ?></td> 
  258. </tr> 
  259. <tr> 
  260. <td class="ms-title-column"> 
  261. <?php _e( 'Trial price', 'membership2' ); ?> 
  262. </td> 
  263. <td class="ms-desc-column"> 
  264. <?php 
  265. if ( $invoice->trial_price > 0 ) { 
  266. printf( 
  267. '<span class="price">%s %s</span>',  
  268. $invoice->currency,  
  269. MS_Helper_Billing::format_price( $invoice->trial_price ) 
  270. ); 
  271. } else { 
  272. _e( 'Free', 'membership2' ); 
  273. ?> 
  274. </td> 
  275. </tr> 
  276. <?php endif; ?> 
  277.  
  278. <?php 
  279. do_action( 
  280. 'ms_view_frontend_payment_after_total_row',  
  281. $subscription,  
  282. $invoice,  
  283. $this 
  284. ); 
  285. ?> 
  286.  
  287. <tr> 
  288. <td class="ms-desc-column" colspan="2"> 
  289. <span class="ms-membership-description"><?php 
  290. echo $subscription->get_payment_description( $invoice ); 
  291. ?></span> 
  292. </td> 
  293. </tr> 
  294. <?php endif; ?> 
  295.  
  296. <?php if ( $cancel_warning ) : ?> 
  297. <tr> 
  298. <td class="ms-desc-warning" colspan="2"> 
  299. <span class="ms-cancel-other-memberships"><?php 
  300. echo $cancel_warning; 
  301. ?></span> 
  302. </td> 
  303. </tr> 
  304. <?php endif; 
  305.  
  306. if ( MS_Model_Member::is_admin_user() ) : ?> 
  307. <tr> 
  308. <td class="ms-desc-adminnote" colspan="2"> 
  309. <em><?php 
  310. _e( 'As admin user you already have access to this membership', 'membership2' ); 
  311. ?></em> 
  312. </td> 
  313. </tr> 
  314. <?php else : 
  315. do_action( 
  316. 'ms_view_frontend_payment_purchase_button',  
  317. $subscription,  
  318. $invoice,  
  319. $this 
  320. ); 
  321. endif; 
  322. ?> 
  323. </table> 
  324. </div> 
  325. <?php 
  326. do_action( 'ms_view_frontend_payment_after', $this->data, $this ); 
  327. do_action( 'ms_show_prices' ); 
  328.  
  329. if ( $show_tax ) { 
  330. do_action( 'ms_tax_editor', $invoice ); 
  331. ?> 
  332. <div style="clear:both;"></div> 
  333. <?php 
  334.  
  335. $html = ob_get_clean(); 
  336. $html = apply_filters( 'ms_compact_code', $html ); 
  337.  
  338. $html = apply_filters( 
  339. 'ms_view_frontend_payment_form',  
  340. $html,  
  341. $this 
  342. ); 
  343. return $html; 
  344.  
.