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

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

https://gitlab.com/campus-academy/krowkaramel
PHP | 308 lines | 162 code | 48 blank | 98 comment | 18 complexity | 360ce0748958eb61bbb29db18f959849 MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * Class that handles Alipay payment method.
  7. *
  8. * @extends WC_Gateway_Stripe
  9. *
  10. * @since 4.0.0
  11. */
  12. class WC_Gateway_Stripe_Alipay extends WC_Stripe_Payment_Gateway {
  13. const ID = 'stripe_alipay';
  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 Alipay', '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 5.8.0
  91. * @return array
  92. */
  93. public function get_supported_currency() {
  94. return apply_filters(
  95. 'wc_stripe_alipay_supported_currencies',
  96. [
  97. 'EUR',
  98. 'AUD',
  99. 'CAD',
  100. 'CNY',
  101. 'GBP',
  102. 'HKD',
  103. 'JPY',
  104. 'NZD',
  105. 'SGD',
  106. 'USD',
  107. 'MYR',
  108. ]
  109. );
  110. }
  111. /**
  112. * Checks to see if all criteria is met before showing payment method.
  113. *
  114. * @since 4.0.0
  115. * @version 4.0.0
  116. * @return bool
  117. */
  118. public function is_available() {
  119. if ( ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
  120. return false;
  121. }
  122. return parent::is_available();
  123. }
  124. /**
  125. * Get_icon function.
  126. *
  127. * @since 1.0.0
  128. * @version 4.0.0
  129. * @return string
  130. */
  131. public function get_icon() {
  132. $icons = $this->payment_icons();
  133. $icons_str = '';
  134. $icons_str .= isset( $icons['alipay'] ) ? $icons['alipay'] : '';
  135. return apply_filters( 'woocommerce_gateway_icon', $icons_str, $this->id );
  136. }
  137. /**
  138. * Payment_scripts function.
  139. *
  140. * @since 4.0.0
  141. * @version 4.0.0
  142. */
  143. public function payment_scripts() {
  144. if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
  145. return;
  146. }
  147. wp_enqueue_style( 'stripe_styles' );
  148. wp_enqueue_script( 'woocommerce_stripe' );
  149. }
  150. /**
  151. * Initialize Gateway Settings Form Fields.
  152. */
  153. public function init_form_fields() {
  154. $this->form_fields = require WC_STRIPE_PLUGIN_PATH . '/includes/admin/stripe-alipay-settings.php';
  155. }
  156. /**
  157. * Payment form on checkout page
  158. */
  159. public function payment_fields() {
  160. global $wp;
  161. $user = wp_get_current_user();
  162. $total = WC()->cart->total;
  163. $description = $this->get_description();
  164. // If paying from order, we need to get total from order not cart.
  165. if ( isset( $_GET['pay_for_order'] ) && ! empty( $_GET['key'] ) ) {
  166. $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) );
  167. $total = $order->get_total();
  168. }
  169. if ( is_add_payment_method_page() ) {
  170. $pay_button_text = __( 'Add Payment', 'woocommerce-gateway-stripe' );
  171. $total = '';
  172. } else {
  173. $pay_button_text = '';
  174. }
  175. echo '<div
  176. id="stripe-alipay-payment-data"
  177. data-amount="' . esc_attr( WC_Stripe_Helper::get_stripe_amount( $total ) ) . '"
  178. data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '">';
  179. if ( $description ) {
  180. echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $description ) ), $this->id );
  181. }
  182. echo '</div>';
  183. }
  184. /**
  185. * Creates the source for charge.
  186. *
  187. * @since 4.0.0
  188. * @version 4.0.0
  189. * @param object $order
  190. * @return mixed
  191. */
  192. public function create_source( $order ) {
  193. $currency = $order->get_currency();
  194. $return_url = $this->get_stripe_return_url( $order );
  195. $post_data = [];
  196. $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency );
  197. $post_data['currency'] = strtolower( $currency );
  198. $post_data['type'] = 'alipay';
  199. $post_data['owner'] = $this->get_owner_details( $order );
  200. $post_data['redirect'] = [ 'return_url' => $return_url ];
  201. if ( ! empty( $this->statement_descriptor ) ) {
  202. $post_data['statement_descriptor'] = WC_Stripe_Helper::clean_statement_descriptor( $this->statement_descriptor );
  203. }
  204. WC_Stripe_Logger::log( 'Info: Begin creating Alipay source' );
  205. return WC_Stripe_API::request( apply_filters( 'wc_stripe_alipay_source', $post_data, $order ), 'sources' );
  206. }
  207. /**
  208. * Process the payment
  209. *
  210. * @param int $order_id Reference.
  211. * @param bool $retry Should we retry on fail.
  212. * @param bool $force_save_source Force payment source to be saved.
  213. *
  214. * @throws Exception If payment will not be accepted.
  215. *
  216. * @return array|void
  217. */
  218. public function process_payment( $order_id, $retry = true, $force_save_save = false ) {
  219. try {
  220. $order = wc_get_order( $order_id );
  221. // This will throw exception if not valid.
  222. $this->validate_minimum_order_amount( $order );
  223. // This comes from the create account checkbox in the checkout page.
  224. $create_account = ! empty( $_POST['createaccount'] ) ? true : false;
  225. if ( $create_account ) {
  226. $new_customer_id = $order->get_customer_id();
  227. $new_stripe_customer = new WC_Stripe_Customer( $new_customer_id );
  228. $new_stripe_customer->create_customer();
  229. }
  230. $response = $this->create_source( $order );
  231. if ( ! empty( $response->error ) ) {
  232. $order->add_order_note( $response->error->message );
  233. throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message );
  234. }
  235. $order->update_meta_data( '_stripe_source_id', $response->id );
  236. $order->save();
  237. WC_Stripe_Logger::log( 'Info: Redirecting to Alipay...' );
  238. return [
  239. 'result' => 'success',
  240. 'redirect' => esc_url_raw( $response->redirect->url ),
  241. ];
  242. } catch ( WC_Stripe_Exception $e ) {
  243. wc_add_notice( $e->getLocalizedMessage(), 'error' );
  244. WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
  245. do_action( 'wc_gateway_stripe_process_payment_error', $e, $order );
  246. $statuses = apply_filters(
  247. 'wc_stripe_allowed_payment_processing_statuses',
  248. [ 'pending', 'failed' ],
  249. $order
  250. );
  251. if ( $order->has_status( $statuses ) ) {
  252. $this->send_failed_order_email( $order_id );
  253. }
  254. return [
  255. 'result' => 'fail',
  256. 'redirect' => '',
  257. ];
  258. }
  259. }
  260. }