PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce-gateway-stripe/includes/payment-methods/class-wc-gateway-stripe-bancontact.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 296 lines | 154 code | 47 blank | 95 comment | 18 complexity | 68502c4cbcb25bb14a094136bfcb970b MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * Class that handles Bancontact payment method.
  7. *
  8. * @extends WC_Gateway_Stripe
  9. *
  10. * @since 4.0.0
  11. */
  12. class WC_Gateway_Stripe_Bancontact extends WC_Stripe_Payment_Gateway {
  13. const ID = 'stripe_bancontact';
  14. /**
  15. * Notices (array)
  16. *
  17. * @var array
  18. */
  19. public $notices = [];
  20. /**
  21. * Is test mode active?
  22. *
  23. * @var bool
  24. */
  25. public $testmode;
  26. /**
  27. * Alternate credit card statement name
  28. *
  29. * @var bool
  30. */
  31. public $statement_descriptor;
  32. /**
  33. * API access secret key
  34. *
  35. * @var string
  36. */
  37. public $secret_key;
  38. /**
  39. * Api access publishable key
  40. *
  41. * @var string
  42. */
  43. public $publishable_key;
  44. /**
  45. * Should we store the users credit cards?
  46. *
  47. * @var bool
  48. */
  49. public $saved_cards;
  50. /**
  51. * Constructor
  52. */
  53. public function __construct() {
  54. $this->id = self::ID;
  55. $this->method_title = __( 'Stripe Bancontact', 'woocommerce-gateway-stripe' );
  56. $this->method_description = sprintf(
  57. /* translators: 1) HTML anchor open tag 2) HTML anchor closing tag */
  58. __( 'All other general Stripe settings can be adjusted %1$shere%2$s.', 'woocommerce-gateway-stripe' ),
  59. '<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout&section=stripe' ) ) . '">',
  60. '</a>'
  61. );
  62. $this->supports = [
  63. 'products',
  64. 'refunds',
  65. ];
  66. // Load the form fields.
  67. $this->init_form_fields();
  68. // Load the settings.
  69. $this->init_settings();
  70. $main_settings = get_option( 'woocommerce_stripe_settings' );
  71. $this->title = $this->get_option( 'title' );
  72. $this->description = $this->get_option( 'description' );
  73. $this->enabled = $this->get_option( 'enabled' );
  74. $this->testmode = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
  75. $this->saved_cards = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? true : false;
  76. $this->publishable_key = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : '';
  77. $this->secret_key = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
  78. $this->statement_descriptor = ! empty( $main_settings['statement_descriptor'] ) ? $main_settings['statement_descriptor'] : '';
  79. if ( $this->testmode ) {
  80. $this->publishable_key = ! empty( $main_settings['test_publishable_key'] ) ? $main_settings['test_publishable_key'] : '';
  81. $this->secret_key = ! empty( $main_settings['test_secret_key'] ) ? $main_settings['test_secret_key'] : '';
  82. }
  83. add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, [ $this, 'process_admin_options' ] );
  84. add_action( 'wp_enqueue_scripts', [ $this, 'payment_scripts' ] );
  85. }
  86. /**
  87. * Returns all supported currencies for this payment method.
  88. *
  89. * @since 4.0.0
  90. * @version 4.0.0
  91. * @return array
  92. */
  93. public function get_supported_currency() {
  94. return apply_filters(
  95. 'wc_stripe_bancontact_supported_currencies',
  96. [
  97. 'EUR',
  98. ]
  99. );
  100. }
  101. /**
  102. * Checks to see if all criteria is met before showing payment method.
  103. *
  104. * @since 4.0.0
  105. * @version 4.0.0
  106. * @return bool
  107. */
  108. public function is_available() {
  109. if ( ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
  110. return false;
  111. }
  112. return parent::is_available();
  113. }
  114. /**
  115. * Get_icon function.
  116. *
  117. * @since 1.0.0
  118. * @version 4.0.0
  119. * @return string
  120. */
  121. public function get_icon() {
  122. $icons = $this->payment_icons();
  123. $icons_str = '';
  124. $icons_str .= isset( $icons['bancontact'] ) ? $icons['bancontact'] : '';
  125. return apply_filters( 'woocommerce_gateway_icon', $icons_str, $this->id );
  126. }
  127. /**
  128. * Outputs scripts used for stripe payment
  129. */
  130. public function payment_scripts() {
  131. if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
  132. return;
  133. }
  134. wp_enqueue_style( 'stripe_styles' );
  135. wp_enqueue_script( 'woocommerce_stripe' );
  136. }
  137. /**
  138. * Initialize Gateway Settings Form Fields.
  139. */
  140. public function init_form_fields() {
  141. $this->form_fields = require WC_STRIPE_PLUGIN_PATH . '/includes/admin/stripe-bancontact-settings.php';
  142. }
  143. /**
  144. * Payment form on checkout page
  145. */
  146. public function payment_fields() {
  147. global $wp;
  148. $user = wp_get_current_user();
  149. $total = WC()->cart->total;
  150. $description = $this->get_description();
  151. // If paying from order, we need to get total from order not cart.
  152. if ( isset( $_GET['pay_for_order'] ) && ! empty( $_GET['key'] ) ) {
  153. $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) );
  154. $total = $order->get_total();
  155. }
  156. if ( is_add_payment_method_page() ) {
  157. $pay_button_text = __( 'Add Payment', 'woocommerce-gateway-stripe' );
  158. $total = '';
  159. } else {
  160. $pay_button_text = '';
  161. }
  162. echo '<div
  163. id="stripe-bancontact-payment-data"
  164. data-amount="' . esc_attr( WC_Stripe_Helper::get_stripe_amount( $total ) ) . '"
  165. data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '">';
  166. if ( $description ) {
  167. echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $description ) ), $this->id );
  168. }
  169. echo '</div>';
  170. }
  171. /**
  172. * Creates the source for charge.
  173. *
  174. * @since 4.0.0
  175. * @version 4.0.0
  176. * @param object $order
  177. * @return mixed
  178. */
  179. public function create_source( $order ) {
  180. $currency = $order->get_currency();
  181. $return_url = $this->get_stripe_return_url( $order );
  182. $post_data = [];
  183. $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency );
  184. $post_data['currency'] = strtolower( $currency );
  185. $post_data['type'] = 'bancontact';
  186. $post_data['owner'] = $this->get_owner_details( $order );
  187. $post_data['redirect'] = [ 'return_url' => $return_url ];
  188. $post_data['bancontact'] = [ 'preferred_language' => $this->get_locale() ];
  189. if ( ! empty( $this->statement_descriptor ) ) {
  190. $post_data['statement_descriptor'] = WC_Stripe_Helper::clean_statement_descriptor( $this->statement_descriptor );
  191. }
  192. WC_Stripe_Logger::log( 'Info: Begin creating Bancontact source' );
  193. return WC_Stripe_API::request( apply_filters( 'wc_stripe_bancontact_source', $post_data, $order ), 'sources' );
  194. }
  195. /**
  196. * Process the payment
  197. *
  198. * @param int $order_id Reference.
  199. * @param bool $retry Should we retry on fail.
  200. * @param bool $force_save_source Force payment source to be saved.
  201. *
  202. * @throws Exception If payment will not be accepted.
  203. *
  204. * @return array|void
  205. */
  206. public function process_payment( $order_id, $retry = true, $force_save_source = false ) {
  207. try {
  208. $order = wc_get_order( $order_id );
  209. // This will throw exception if not valid.
  210. $this->validate_minimum_order_amount( $order );
  211. // This comes from the create account checkbox in the checkout page.
  212. $create_account = ! empty( $_POST['createaccount'] ) ? true : false;
  213. if ( $create_account ) {
  214. $new_customer_id = $order->get_customer_id();
  215. $new_stripe_customer = new WC_Stripe_Customer( $new_customer_id );
  216. $new_stripe_customer->create_customer();
  217. }
  218. $response = $this->create_source( $order );
  219. if ( ! empty( $response->error ) ) {
  220. $order->add_order_note( $response->error->message );
  221. throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message );
  222. }
  223. $order->update_meta_data( '_stripe_source_id', $response->id );
  224. $order->save();
  225. WC_Stripe_Logger::log( 'Info: Redirecting to Bancontact...' );
  226. return [
  227. 'result' => 'success',
  228. 'redirect' => esc_url_raw( $response->redirect->url ),
  229. ];
  230. } catch ( WC_Stripe_Exception $e ) {
  231. wc_add_notice( $e->getLocalizedMessage(), 'error' );
  232. WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
  233. do_action( 'wc_gateway_stripe_process_payment_error', $e, $order );
  234. if ( $order->has_status(
  235. apply_filters(
  236. 'wc_stripe_allowed_payment_processing_statuses',
  237. [ 'pending', 'failed' ],
  238. $order
  239. )
  240. ) ) {
  241. $this->send_failed_order_email( $order_id );
  242. }
  243. return [
  244. 'result' => 'fail',
  245. 'redirect' => '',
  246. ];
  247. }
  248. }
  249. }