/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.  
  46. ob_start(); 
  47. ?> 
  48. <form action="<?php echo esc_url( $action_url ); ?>" method="post"> 
  49. <?php 
  50. foreach ( $fields as $field ) { 
  51. MS_Helper_Html::html_element( $field ); 
  52. ?> 
  53. <script 
  54. src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
  55. <?php 
  56. foreach ( $stripe_data as $key => $value ) { 
  57. printf( 
  58. 'data-%s="%s" ',  
  59. esc_attr( $key ),  
  60. esc_attr( $value ) 
  61. ); 
  62. ?> 
  63. ></script> 
  64. </form> 
  65. <?php 
  66. $payment_form = apply_filters( 
  67. 'ms_gateway_form',  
  68. ob_get_clean(),  
  69. $gateway,  
  70. $invoice,  
  71. $this 
  72. ); 
  73.  
  74. ob_start(); 
  75. ?> 
  76. <tr class="<?php echo esc_attr( $row_class ); ?>"> 
  77. <td class="ms-buy-now-column" colspan="2"> 
  78. <?php echo $payment_form; ?> 
  79. </td> 
  80. </tr> 
  81. <?php 
  82. $html = ob_get_clean(); 
  83.  
  84. $html = apply_filters( 
  85. 'ms_gateway_button-' . $gateway->id,  
  86. $html,  
  87. $this 
  88. ); 
  89.  
  90. return $html; 
  91.  
  92. private function prepare_fields() { 
  93. $gateway = $this->data['gateway']; 
  94. $subscription = $this->data['ms_relationship']; 
  95.  
  96. $fields = array( 
  97. '_wpnonce' => array( 
  98. 'id' => '_wpnonce',  
  99. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  100. 'value' => wp_create_nonce( 
  101. $gateway->id . '_' . $subscription->id 
  102. ),  
  103. ),  
  104. 'gateway' => array( 
  105. 'id' => 'gateway',  
  106. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  107. 'value' => $gateway->id,  
  108. ),  
  109. 'ms_relationship_id' => array( 
  110. 'id' => 'ms_relationship_id',  
  111. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  112. 'value' => $subscription->id,  
  113. ),  
  114. 'step' => array( 
  115. 'id' => 'step',  
  116. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  117. 'value' => $this->data['step'],  
  118. ),  
  119. ); 
  120.  
  121. return $fields; 
.