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