MS_View_Frontend_Payment

The Membership 2 MS View Frontend Payment class.

Defined (1)

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

/app/view/frontend/class-ms-view-frontend-payment.php  
  1. class MS_View_Frontend_Payment extends MS_View { 
  2.  
  3. /** 
  4. * Returns the HTML code for the Purchase-Membership form. 
  5. * @since 1.0.0 
  6. * @return string 
  7. */ 
  8. public function to_html() { 
  9. $membership = $this->data['membership']; 
  10. $invoice = $this->data['invoice']; 
  11. $subscription = $this->data['ms_relationship']; 
  12.  
  13. $class = 'ms-alert-success'; 
  14. $msg = __( 
  15. 'Please check the details of the membership below and click ' . 
  16. 'on the relevant button to complete the signup.', 'membership2' 
  17. ); 
  18.  
  19. if ( ! empty( $this->data['error'] ) ) { 
  20. $class = 'ms-alert-error'; 
  21. $msg = $this->data['error']; 
  22.  
  23. /** 
  24. * Log the users IP and current timestamp inside the invoice. 
  25. * @since 1.0.2.0 
  26. */ 
  27. $invoice->checkout_ip = lib3()->net->current_ip()->ip; 
  28. $invoice->checkout_date = date( 'Y-m-d H:i:s' ); 
  29. $invoice->save(); 
  30.  
  31. $cancel_warning = false; 
  32.  
  33. if ( ! MS_Model_Member::is_admin_user() ) { 
  34.  
  35. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MULTI_MEMBERSHIPS ) ) { 
  36. // Member can only sign up to one membership. 
  37. $valid_status = array( 
  38. MS_Model_Relationship::STATUS_TRIAL,  
  39. MS_Model_Relationship::STATUS_ACTIVE,  
  40. MS_Model_Relationship::STATUS_PENDING,  
  41. ); 
  42.  
  43. foreach ( $this->data['member']->subscriptions as $tmp_subscription ) { 
  44. if ( $tmp_subscription->is_system() ) { continue; } 
  45. if ( in_array( $tmp_subscription->status, $valid_status ) ) { 
  46. $cancel_warning = __( 
  47. 'Your other Memberships will be cancelled when you complete this payment.',  
  48. 'membership2' 
  49. ); 
  50. break; 
  51. } elseif ( $subscription->move_from_id ) { 
  52. $move_from_ids = explode( ', ', $subscription->move_from_id ); 
  53. $names = array(); 
  54. foreach ( $move_from_ids as $id ) { 
  55. $ms = MS_Factory::load( 'MS_Model_Membership', $id ); 
  56. if ( $ms->is_system() ) { continue; } 
  57. $names[] = $ms->name; 
  58.  
  59. if ( 1 == count( $names ) ) { 
  60. $cancel_warning = sprintf( 
  61. __( 'When you complete this payment your Membership "%s" will be cancelled.', 'membership2' ),  
  62. $names[0] 
  63. ); 
  64. } elseif ( 1 < count( $names ) ) { 
  65. $cancel_warning = sprintf( 
  66. __( 'When you complete this payment the following Memberships will be cancelled: %s.', 'membership2' ),  
  67. implode( ', ', $names ) 
  68. ); 
  69.  
  70. // Check if the user goes through a trial period before first payment. 
  71. $is_trial = $invoice->uses_trial; 
  72.  
  73. $skip_form = ! MS_Model_Member::is_admin_user() 
  74. && ! $cancel_warning 
  75. && $membership->is_free(); 
  76.  
  77. /** 
  78. * Filter the flag to allow Add-ons like "Invitation codes" to override 
  79. * the state and force the form to display. 
  80. * @var bool 
  81. */ 
  82. $skip_form = apply_filters( 
  83. 'ms_view_frontend_payment_skip_form',  
  84. $skip_form,  
  85. $invoice,  
  86. $this 
  87. ); 
  88.  
  89. if ( $skip_form ) { 
  90. // No confirmation required. Simply register for this membership! 
  91. $args = array(); 
  92. $args['ms_relationship_id'] = $subscription->id; 
  93. $args['gateway'] = MS_Gateway_Free::ID; 
  94. $args['step'] = MS_Controller_Frontend::STEP_PROCESS_PURCHASE; 
  95. $args['_wpnonce'] = wp_create_nonce( $args['gateway'] . '_' . $args['ms_relationship_id'] ); 
  96. $url = esc_url_raw( add_query_arg( $args ) ); 
  97.  
  98. /** 
  99. * Very likely the html output has already began. 
  100. * So we redirect by using javascript. 
  101. */ 
  102. ?> 
  103. <script>window.location.href = '<?php echo $url; ?>';</script> 
  104. <?php 
  105. exit; 
  106.  
  107. $show_tax = MS_Model_Addon::is_enabled( MS_Addon_Taxamo::ID ); 
  108.  
  109. /** 
  110. * Trigger an action before the payment form is displayed. This hook 
  111. * can be used by Add-ons or plugins to initialize payment settings or 
  112. * add custom code. 
  113. */ 
  114. do_action( 'ms_view_frontend_payment_form_start', $invoice, $this ); 
  115.  
  116. $classes = array( 
  117. 'ms-membership-form-wrapper',  
  118. 'ms-subscription-' . $subscription->id,  
  119. 'ms-invoice-' . $invoice->id,  
  120. ); 
  121.  
  122. ob_start(); 
  123.  
  124. $membership_wrapper_class = esc_attr( implode( ' ', $classes ) ); 
  125. $alert_box_class = esc_attr( $class ); 
  126. $membership_name = esc_html( $membership->name ); 
  127. $is_membership_description = $membership->description; 
  128. $membership_description = $membership->get_description(); 
  129. $is_membership_free = $membership->is_free(); 
  130. $invoice_discount = $invoice->discount; 
  131. $invoice_pro_rate = $invoice->pro_rate; 
  132. $invoice_tax_rate = $invoice->tax_rate; 
  133. $invoice_discount = $invoice->discount; 
  134. $invoice_formatted_discount = sprintf( 
  135. '%s -%s',  
  136. $invoice->currency,  
  137. MS_Helper_Billing::format_price( $invoice->discount ) 
  138. ); 
  139. $invoice_formatted_pro_rate = sprintf( 
  140. '%s -%s',  
  141. $invoice->currency,  
  142. MS_Helper_Billing::format_price( $invoice->pro_rate ) 
  143. ); 
  144. $invoice_tax_name = sprintf( 
  145. __( 'Taxes %s', 'membership2' ),  
  146. '<a href="#" class="ms-tax-editor"><small>(' . $invoice->tax_name . ')</small></a>' 
  147. ); 
  148. $invoice_formatted_tax = sprintf( 
  149. '%s %s',  
  150. $invoice->currency,  
  151. MS_Helper_Billing::format_price( $invoice->tax ) 
  152. ); 
  153. $invoice_total = $invoice->total; 
  154. $is_ms_admin_user = MS_Model_Member::is_admin_user(); 
  155. $invoice_formatted_total_for_admin = sprintf( 
  156. '<span class="price">%s %s</span>',  
  157. $invoice->currency,  
  158. MS_Helper_Billing::format_price( $membership->price - $invoice->discount + $invoice->pro_rate + $invoice->tax ) 
  159. ); 
  160. $invoice_formatted_total = sprintf( 
  161. '<span class="price">%s %s</span>',  
  162. $invoice->currency,  
  163. MS_Helper_Billing::format_price( $invoice->total ) 
  164. ); 
  165. $membership_price = $membership->price; 
  166. $membership_formatted_price = sprintf( 
  167. '<span class="price">%s %s</span>',  
  168. $invoice->currency,  
  169. MS_Helper_Billing::format_price( $membership->price ) 
  170. ); 
  171.  
  172. $invoice_formatted_due_date = MS_Helper_Period::format_date( $invoice->due_date ); 
  173. $invoice_trial_price = $invoice->trial_price; 
  174. $invoice_formatted_trial_price = sprintf( 
  175. '<span class="price">%s %s</span>',  
  176. $invoice->currency,  
  177. MS_Helper_Billing::format_price( $invoice->trial_price ) 
  178. ); 
  179. $invoice_payment_description = $subscription->get_payment_description( $invoice ); 
  180.  
  181. $template_data = array( 
  182. 'membership_wrapper_class' => $membership_wrapper_class,  
  183. 'alert_box_class' => $alert_box_class,  
  184. 'msg' => $msg,  
  185. 'membership_name' => $membership_name,  
  186. 'is_membership_description' => $is_membership_description,  
  187. 'membership_description' => $membership_description,  
  188. 'is_membership_free' => $is_membership_free,  
  189. 'invoice_discount' => $invoice_discount,  
  190. 'invoice_pro_rate' => $invoice_pro_rate,  
  191. 'invoice_tax_rate' => $invoice_tax_rate,  
  192. 'membership_price' => $membership_price,  
  193. 'membership_formatted_price' => $membership_formatted_price,  
  194. 'invoice_formatted_discount' => $invoice_formatted_discount,  
  195. 'invoice_formatted_pro_rate' => $invoice_formatted_pro_rate,  
  196. 'show_tax' => $show_tax,  
  197. 'invoice_tax_name' => $invoice_tax_name,  
  198. 'invoice_formatted_tax' => $invoice_formatted_tax,  
  199. 'invoice_total' => $invoice_total,  
  200. 'is_ms_admin_user' => $is_ms_admin_user,  
  201. 'invoice_formatted_total_for_admin' => $invoice_formatted_total_for_admin,  
  202. 'invoice_formatted_total' => $invoice_formatted_total,  
  203. 'is_trial' => $is_trial,  
  204. 'invoice_formatted_due_date' => $invoice_formatted_due_date,  
  205. 'invoice_trial_price' => $invoice_trial_price,  
  206. 'invoice_formatted_trial_price' => $invoice_formatted_trial_price,  
  207. 'invoice_payment_description' => $invoice_payment_description,  
  208. 'cancel_warning' => $cancel_warning,  
  209. 'm2_payment_obj' => $this,  
  210. 'subscription' => $subscription,  
  211. 'invoice' => $invoice,  
  212. ); 
  213.  
  214. MS_Helper_Template::$ms_front_payment = $template_data; 
  215. if ( $path = MS_Helper_Template::template_exists( 'membership_frontend_payment.php' ) ) { 
  216. require $path; 
  217.  
  218. $html = ob_get_clean(); 
  219. $html = apply_filters( 'ms_compact_code', $html ); 
  220.  
  221. $html = apply_filters( 
  222. 'ms_view_frontend_payment_form',  
  223. $html,  
  224. $this 
  225. ); 
  226. return $html;