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
<?php
defined( 'ABSPATH' ) || exit();
class WC_Braintree_Controller_GooglePay extends WC_Braintree_Rest_Controller {
use WC_Braintree_Controller_Cart_Trait;
protected $namespace = 'googlepay/';
private $shipping_method_id;
public function register_routes() {
register_rest_route(
$this->rest_uri(),
'shipping-address',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'update_shipping_data' ),
'permission_callback' => '__return_true',
)
);
}
public function update_shipping_data( $request ) {
wc_maybe_define_constant( 'WOOCOMMERCE_CART', true );
$customer = WC()->customer;
$gateway = WC()->payment_gateways()->payment_gateways()['braintree_googlepay'];
$address = array(
'country' => $request->get_param( 'address' )['countryCode'],
'state' => $request->get_param( 'address' )['administrativeArea'],
'city' => $request->get_param( 'address' )['locality'],
'postcode' => $request->get_param( 'address' )['postalCode'],
);
$shipping_options = $request->get_param( 'shippingOptions' );
try {
wc_braintree_update_customer_location( $address );
$chosen_shipping_methods = null;
if ( $shipping_options['id'] !== 'shipping_option_unselected' ) {
$this->shipping_method_id = $gateway->shipping_method_id = $shipping_options['id'];
preg_match( '/([\w]+)\:(.+)/', $this->shipping_method_id, $shipping_methods );
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods', array() );
if ( is_array( $shipping_methods ) ) {
$chosen_shipping_methods[ $shipping_methods[1] ] = $shipping_methods[2];
if ( $gateway->cart_contains_trial_period_subscription() ) {
foreach ( $chosen_shipping_methods as $i => $method_id ) {
if ( strlen( $i ) > 1 ) {
$chosen_shipping_methods[ $i ] = $shipping_methods[2];
}
}
}
}
WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
}
$this->add_ready_to_calc_shipping();
WC()->cart->calculate_totals();
$shipping_data = $gateway->get_shipping_options();
if ( empty( $shipping_data['shippingOptions'] ) ) {
throw new Exception( __( 'No shipping methods available for your shipping address.', 'woo-payment-gateway' ) );
}
$response = array(
'data' => array(
'chosen_shipping_methods' => $chosen_shipping_methods,
'requestUpdate' => array(
'newTransactionInfo' => array(
'currencyCode' => get_woocommerce_currency(),
'totalPriceStatus' => 'FINAL',
'totalPrice' => strval( WC()->cart->total ),
'totalPriceLabel' => __( 'Total', 'woo-payment-gateway' ),
'displayItems' => $gateway->get_display_items(),
),
'newShippingOptionParameters' => $shipping_data,
),
),
);
return rest_ensure_response( $response );
} catch ( Exception $e ) {
return new WP_Error(
'shipping-error',
$e->getMessage(),
array(
'status' => 200,
'data' => array(
'error' => array(
'reason' => $e->getMessage(),
'message' => $e->getMessage(),
'intent' => 'SHIPPING_ADDRESS',
),
),
)
);
}
}
}