1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
<?php
defined( 'ABSPATH' ) || exit();
if ( ! class_exists( 'WC_Stripe_Rest_Controller' ) ) {
return;
}
class WC_Stripe_Controller_Checkout extends WC_Stripe_Rest_Controller {
use WC_Stripe_Controller_Frontend_Trait;
protected $namespace = '';
private $order_review = false;
private $gateway = null;
public function register_routes() {
register_rest_route(
$this->rest_uri(),
'checkout',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'process_checkout' ),
'permission_callback' => '__return_true',
)
);
register_rest_route(
$this->rest_uri(),
'checkout/payment',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'process_payment' ),
'permission_callback' => '__return_true',
'args' => array(
'order_id' => array( 'required' => true ),
'order_key' => array( 'required' => true )
)
)
);
register_rest_route(
$this->rest_uri(),
'order-pay',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'process_order_pay' ),
'permission_callback' => '__return_true',
)
);
}
public function process_checkout( $request ) {
$this->actions();
$checkout = WC()->checkout();
$payment_method = $request->get_param( 'payment_method' );
$this->gateway = $gateway = WC()->payment_gateways()->payment_gateways()[ $payment_method ];
try {
do_action( 'wc_stripe_rest_process_checkout', $request, $gateway );
if ( ! is_user_logged_in() ) {
$this->create_customer( $request );
}
$_REQUEST['_wpnonce'] = $_POST['_wpnonce'] = wp_create_nonce( 'woocommerce-process_checkout' );
if ( 'product' == $request->get_param( 'page_id' ) ) {
wc_stripe_stash_cart( WC()->cart );
$gateway->set_post_payment_process( array( $this, 'post_payment_processes' ) );
add_filter( 'woocommerce_get_checkout_order_received_url', function ( $url, $order ) {
return add_query_arg( 'wc_stripe_product_checkout', $order->get_payment_method(), $url );
}, 10, 2 );
$option = new WC_Stripe_Product_Gateway_Option( current( WC()->cart->get_cart() )['data'], $gateway );
$gateway->settings['charge_type'] = $option->get_option( 'charge_type' );
}
$this->required_post_data();
$checkout->process_checkout();
} catch ( Exception $e ) {
wc_add_notice( $e->getMessage(), 'error' );
}
if ( wc_notice_count( 'error' ) > 0 ) {
return $this->send_response( false );
}
return $this->send_response( true );
}
public function process_payment( $request ) {
wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );
wc_maybe_define_constant( WC_Stripe_Constants::PROCESSING_PAYMENT, true );
try {
$order_id = absint( $request->get_param( 'order_id' ) );
$order = wc_get_order( $order_id );
if ( ! $order ) {
throw new Exception( __( 'Invalid order ID.', 'woo-stripe-payment' ) );
}
if ( ! $order->key_is_valid( $request->get_param( 'order_key' ) ) ) {
throw new Exception( __( 'Invalid order key.', 'woo-stripe-payment' ) );
}
$payment_method = WC()->payment_gateways()->payment_gateways()[ $order->get_payment_method() ];
$result = $payment_method->process_payment( $order_id );
if ( isset( $result['result'] ) && 'success' === $result['result'] ) {
return $this->send_response( true, $result );
}
return $this->send_response( false, $result );
} catch ( Exception $e ) {
wc_add_notice( $e->getMessage(), 'error' );
return $this->send_response( false );
}
}
public function process_order_pay( $request ) {
global $wp;
$this->frontend_includes();
$wp->set_query_var( 'order-pay', $request->get_param( 'order_id' ) );
wc_maybe_define_constant( WC_Stripe_Constants::WOOCOMMERCE_STRIPE_ORDER_PAY, true );
WC_Form_Handler::pay_action();
if ( wc_notice_count( 'error' ) > 0 ) {
return rest_ensure_response(
array(
'success' => false,
'message' => $this->get_messages( 'error' ),
)
);
}
}
private function create_customer( $request ) {
$create = WC()->checkout()->is_registration_required();
if ( wcs_stripe_active() && WC_Subscriptions_Cart::cart_contains_subscription() ) {
$create = true;
}
if ( $create ) {
$password = wp_generate_password();
$username = $email = $request->get_param( 'billing_email' );
$result = wc_create_new_customer( $email, $username, $password );
if ( $result instanceof WP_Error ) {
throw new Exception( $result->get_error_message() );
}
wp_set_current_user( $result );
wc_set_customer_auth_cookie( $result );
WC()->session->set( 'reload_checkout', true );
}
}
private function send_response( $success, $defaults = array() ) {
$reload = WC()->session->get( 'reload_checkout', false );
$data = wp_parse_args( $defaults, array(
'result' => $success ? 'success' : 'failure',
'messages' => $reload ? null : $this->get_error_messages(),
'reload' => $reload,
) );
unset( WC()->session->reload_checkout );
return rest_ensure_response( $data );
}
public function validate_payment_method( $payment_method ) {
$gateways = WC()->payment_gateways()->payment_gateways();
return isset( $gateways[ $payment_method ] ) ? true : new WP_Error( 'validation-error', 'Please choose a valid payment method.' );
}
private function actions() {
add_action( 'woocommerce_after_checkout_validation', array( $this, 'after_checkout_validation' ), 10, 2 );
add_action( 'woocommerce_checkout_posted_data', array( $this, 'filter_posted_data' ) );
}
public function set_stashed_cart( $order, $gateway ) {
wc_stripe_restore_cart( WC()->cart );
}
public function after_checkout_validation( $data, $errors ) {
if ( $errors->get_error_codes() ) {
wc_stripe_log_info( sprintf( __CLASS__ . '::checkout errors: %s', print_r( $errors->get_error_codes(), true ) ) );
wc_add_notice(
apply_filters(
'wc_stripe_after_checkout_validation_notice', __( 'Please review your order details then click Place Order.', 'woo-stripe-payment' ),
$data,
$errors
),
'notice'
);
wp_send_json(
array(
'result' => 'success',
'redirect' => $this->get_order_review_url(),
'reload' => false,
),
200
);
}
}
private function required_post_data() {
if ( WC()->cart->needs_shipping() ) {
$_POST['ship_to_different_address'] = true;
}
if ( wc_get_page_id( 'terms' ) > 0 ) {
$_POST['terms'] = 1;
}
}
private function get_order_review_url() {
return add_query_arg(
array(
'_stripe_order_review' => rawurlencode( base64_encode( wp_json_encode( array(
'payment_method' => $this->gateway->id,
'payment_nonce' => $this->gateway->get_payment_source(),
) ) ) ),
),
wc_get_checkout_url()
);
}
public function checkout_order_processed( $order_id, $posted_data, $order ) {
if ( $this->order_review ) {
wc_add_notice( __( 'Please review your order details then click Place Order.', 'woo-stripe-payment' ), 'notice' );
wp_send_json(
array(
'result' => 'success',
'redirect' => $this->get_order_review_url(),
),
200
);
}
}
public function post_payment_processes( $order, $gateway ) {
wc_stripe_restore_cart( WC()->cart );
$data = WC()->session->get( 'wc_stripe_cart', array() );
unset( $data['product_cart'] );
WC()->session->set( 'wc_stripe_cart', $data );
}
public function filter_posted_data( $data ) {
if ( isset( $data['shipping_method'], $data['shipping_country'], $data['shipping_state'] ) ) {
$data['shipping_state'] = wc_stripe_filter_address_state( $data['shipping_state'], $data['shipping_country'] );
}
return $data;
}
}