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
<?php
defined( 'ABSPATH' ) || exit();
trait WC_Stripe_Controller_Cart_Trait {
public function add_ready_to_calc_shipping() {
add_filter(
'woocommerce_cart_ready_to_calc_shipping',
function ( $show_shipping ) {
return true;
},
1000
);
}
private function get_shipping_method_from_request( $request ) {
if ( ( $method = $request->get_param( 'shipping_method' ) ) ) {
if ( ! preg_match( '/^(?P<index>[\w]+)\:(?P<id>.+)$/', $method, $shipping_method ) ) {
throw new Exception( __( 'Invalid shipping method format. Expected: index:id', 'woo-stripe-payment' ) );
}
return array( $shipping_method['index'] => $shipping_method['id'] );
}
return array();
}
public function validate_shipping_address( $address, $request ) {
if ( isset( $address['state'], $address['country'] ) ) {
$address['state'] = wc_stripe_filter_address_state( $address['state'], $address['country'] );
$request['address'] = $address;
}
return true;
}
}
trait WC_Stripe_Controller_Frontend_Trait {
private $request;
protected function cart_includes() {
include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
include_once WC_ABSPATH . 'includes/wc-notice-functions.php';
wc_load_cart();
WC()->cart->get_cart();
WC()->payment_gateways();
}
protected function frontend_includes() {
WC()->frontend_includes();
wc_load_cart();
WC()->cart->get_cart();
WC()->payment_gateways();
}
public function validate_rest_nonce( $request ) {
if ( ! isset( $request['wp_rest_nonce'] ) || ! wp_verify_nonce( $request['wp_rest_nonce'], 'wp_rest' ) ) {
return new WP_Error( 'rest_cookie_invalid_nonce', __( 'Cookie nonce is invalid' ), array( 'status' => 403 ) );
}
return true;
}
}