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

  1. <?php 
  2.  
  3. class MS_Gateway_Paypalsingle_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_paypalsingle_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"> 
  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. $invoice = $subscription->get_current_invoice(); 
  81.  
  82. $fields = array( 
  83. 'business' => array( 
  84. 'id' => 'business',  
  85. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  86. 'value' => $gateway->paypal_email,  
  87. ),  
  88. 'cmd' => array( 
  89. 'id' => 'cmd',  
  90. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  91. 'value' => '_xclick',  
  92. ),  
  93. 'bn' => array( 
  94. 'id' => 'bn',  
  95. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  96. 'value' => 'incsub_SP',  
  97. ),  
  98. 'item_number' => array( 
  99. 'id' => 'item_number',  
  100. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  101. 'value' => $subscription->membership_id,  
  102. ),  
  103. 'item_name' => array( 
  104. 'id' => 'item_name',  
  105. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  106. 'value' => $membership->name,  
  107. ),  
  108. 'amount' => array( 
  109. 'id' => 'amount',  
  110. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  111. 'value' => MS_Helper_Billing::format_price( $invoice->total ),  
  112. ),  
  113. 'currency_code' => array( 
  114. 'id' => 'currency_code',  
  115. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  116. 'value' => $invoice->currency,  
  117. ),  
  118. 'return' => array( 
  119. 'id' => 'return',  
  120. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  121. 'value' => esc_url_raw( 
  122. add_query_arg( 
  123. array( 'ms_relationship_id' => $subscription->id ),  
  124. MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_REG_COMPLETE, false ) 
  125. ),  
  126. ),  
  127. 'cancel_return' => array( 
  128. 'id' => 'cancel_return',  
  129. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  130. 'value' => MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_REGISTER ),  
  131. ),  
  132. 'notify_url' => array( 
  133. 'id' => 'notify_url',  
  134. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  135. 'value' => $gateway->get_return_url(),  
  136. ),  
  137. 'lc' => array( 
  138. 'id' => 'lc',  
  139. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  140. 'value' => $gateway->paypal_site,  
  141. ),  
  142. 'invoice' => array( 
  143. 'id' => 'invoice',  
  144. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  145. 'value' => $invoice->id,  
  146. ),  
  147. ); 
  148.  
  149. // Don't send to paypal if free 
  150. if ( 0 === $invoice->total ) { 
  151. $fields = array( 
  152. 'gateway' => array( 
  153. 'id' => 'gateway',  
  154. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  155. 'value' => $gateway->id,  
  156. ),  
  157. 'ms_relationship_id' => array( 
  158. 'id' => 'ms_relationship_id',  
  159. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  160. 'value' => $subscription->id,  
  161. ),  
  162. 'step' => array( 
  163. 'id' => 'step',  
  164. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  165. 'value' => MS_Controller_Frontend::STEP_PROCESS_PURCHASE,  
  166. ),  
  167. '_wpnonce' => array( 
  168. 'id' => '_wpnonce',  
  169. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  170. 'value' => wp_create_nonce( 
  171. $gateway->id . '_' .$subscription->id 
  172. ),  
  173. ),  
  174. ); 
  175. $this->data['action_url'] = null; 
  176. } else { 
  177. if ( $gateway->is_live_mode() ) { 
  178. $this->data['action_url'] = 'https://www.paypal.com/cgi-bin/webscr'; 
  179. } else { 
  180. $this->data['action_url'] = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; 
  181.  
  182. $fields['submit'] = array( 
  183. 'id' => 'submit',  
  184. 'type' => MS_Helper_Html::INPUT_TYPE_IMAGE,  
  185. 'value' => 'https://www.paypalobjects.com/en_US/i/btn/x-click-but06.gif',  
  186. 'alt' => __( 'PayPal - The safer, easier way to pay online', 'membership2' ),  
  187. ); 
  188.  
  189. // custom pay button defined in gateway settings 
  190. $custom_label = $gateway->pay_button_url; 
  191. if ( ! empty( $custom_label ) ) { 
  192. if ( false !== strpos( $custom_label, '://' ) ) { 
  193. $fields['submit']['value'] = $custom_label; 
  194. } else { 
  195. $fields['submit'] = array( 
  196. 'id' => 'submit',  
  197. 'type' => MS_Helper_Html::INPUT_TYPE_SUBMIT,  
  198. 'value' => $custom_label,  
  199. ); 
  200.  
  201. return apply_filters( 
  202. 'ms_gateway_paypalsingle_view_prepare_fields',  
  203. $fields 
  204. ); 
.