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

  1. <?php 
  2.  
  3. class MS_Gateway_Stripe_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. $member = MS_Model_Member::get_current_member(); 
  10. $gateway = $this->data['gateway']; 
  11.  
  12. // Stripe is using Ajax, so the URL is empty. 
  13. $action_url = apply_filters( 
  14. 'ms_gateway_stripe_view_button_form_action_url',  
  15. '' 
  16. ); 
  17.  
  18. $row_class = 'gateway_' . $gateway->id; 
  19. if ( ! $gateway->is_live_mode() ) { 
  20. $row_class .= ' sandbox-mode'; 
  21.  
  22. $stripe_data = array( 
  23. 'name' => get_bloginfo( 'name' ),  
  24. 'description' => strip_tags( $invoice->short_description ),  
  25. 'label' => $gateway->pay_button_url,  
  26. ); 
  27.  
  28. /** 
  29. * Users can change details (like the title or description) of the 
  30. * stripe checkout popup. 
  31. * 
  32. * @since 1.0.2.4 
  33. * @var array 
  34. */ 
  35. $stripe_data = apply_filters( 
  36. 'ms_gateway_stripe_form_details',  
  37. $stripe_data,  
  38. $invoice 
  39. ); 
  40.  
  41. $stripe_data['email'] = $member->email; 
  42. $stripe_data['key'] = $gateway->get_publishable_key(); 
  43. $stripe_data['currency'] = $invoice->currency; 
  44. $stripe_data['amount'] = ceil(abs( $invoice->total * 100 )); // Amount in cents. 
  45. $stripe_data['image'] = $gateway->get_vendor_logo(); 
  46. $stripe_data['locale'] = 'auto'; 
  47. $stripe_data['zip-code'] = 'true'; 
  48.  
  49. $stripe_data = apply_filters( 
  50. 'ms_gateway_stripe_form_details_after',  
  51. $stripe_data,  
  52. $invoice 
  53. ); 
  54.  
  55. ob_start(); 
  56. ?> 
  57. <form action="<?php echo esc_url( $action_url ); ?>" method="post"> 
  58. <?php 
  59. foreach ( $fields as $field ) { 
  60. MS_Helper_Html::html_element( $field ); 
  61. ?> 
  62. <script 
  63. src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
  64. <?php 
  65. foreach ( $stripe_data as $key => $value ) { 
  66. printf( 
  67. 'data-%s="%s" ',  
  68. esc_attr( $key ),  
  69. esc_attr( $value ) 
  70. ); 
  71. ?> 
  72. ></script> 
  73. </form> 
  74. <?php 
  75. $payment_form = apply_filters( 
  76. 'ms_gateway_form',  
  77. ob_get_clean(),  
  78. $gateway,  
  79. $invoice,  
  80. $this 
  81. ); 
  82.  
  83. ob_start(); 
  84. ?> 
  85. <tr class="<?php echo esc_attr( $row_class ); ?>"> 
  86. <td class="ms-buy-now-column" colspan="2"> 
  87. <?php echo $payment_form; ?> 
  88. </td> 
  89. </tr> 
  90. <?php 
  91. $html = ob_get_clean(); 
  92.  
  93. $html = apply_filters( 
  94. 'ms_gateway_button-' . $gateway->id,  
  95. $html,  
  96. $this 
  97. ); 
  98.  
  99. return $html; 
  100.  
  101. private function prepare_fields() { 
  102. $gateway = $this->data['gateway']; 
  103. $subscription = $this->data['ms_relationship']; 
  104.  
  105. $fields = array( 
  106. '_wpnonce' => array( 
  107. 'id' => '_wpnonce',  
  108. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  109. 'value' => wp_create_nonce( 
  110. $gateway->id . '_' . $subscription->id 
  111. ),  
  112. ),  
  113. 'gateway' => array( 
  114. 'id' => 'gateway',  
  115. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  116. 'value' => $gateway->id,  
  117. ),  
  118. 'ms_relationship_id' => array( 
  119. 'id' => 'ms_relationship_id',  
  120. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  121. 'value' => $subscription->id,  
  122. ),  
  123. 'step' => array( 
  124. 'id' => 'step',  
  125. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  126. 'value' => $this->data['step'],  
  127. ),  
  128. ); 
  129.  
  130. return $fields; 
.