mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 13:44:42 +08:00
✨ Store shipping cost in the DTO
This commit is contained in:
parent
611ab4c5a7
commit
3b92eaac81
6 changed files with 30 additions and 7 deletions
|
@ -85,7 +85,8 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
|
||||||
$this->add_products( $products );
|
$this->add_products( $products );
|
||||||
|
|
||||||
$this->cart->calculate_totals();
|
$this->cart->calculate_totals();
|
||||||
$total = (float) $this->cart->get_total( 'numeric' );
|
$total = (float) $this->cart->get_total( 'numeric' );
|
||||||
|
$shipping_fee = (float) $this->cart->get_shipping_total();
|
||||||
|
|
||||||
$this->restore_real_cart();
|
$this->restore_real_cart();
|
||||||
|
|
||||||
|
@ -113,7 +114,7 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
|
||||||
wp_send_json_success(
|
wp_send_json_success(
|
||||||
array(
|
array(
|
||||||
'total' => $total,
|
'total' => $total,
|
||||||
'total_str' => ( new Money( $total, $currency_code ) )->value_str(),
|
'shipping_fee' => $shipping_fee,
|
||||||
'currency_code' => $currency_code,
|
'currency_code' => $currency_code,
|
||||||
'country_code' => $shop_country_code,
|
'country_code' => $shop_country_code,
|
||||||
'funding' => array(
|
'funding' => array(
|
||||||
|
|
|
@ -37,6 +37,7 @@ class BaseHandler {
|
||||||
const data = result.data;
|
const data = result.data;
|
||||||
const transaction = new TransactionInfo(
|
const transaction = new TransactionInfo(
|
||||||
data.total,
|
data.total,
|
||||||
|
data.shipping_fee,
|
||||||
data.currency_code,
|
data.currency_code,
|
||||||
data.country_code,
|
data.country_code,
|
||||||
true
|
true
|
||||||
|
|
|
@ -17,6 +17,7 @@ class PayNowHandler extends BaseHandler {
|
||||||
|
|
||||||
const transaction = new TransactionInfo(
|
const transaction = new TransactionInfo(
|
||||||
data.total,
|
data.total,
|
||||||
|
data.shipping_fee,
|
||||||
data.currency_code,
|
data.currency_code,
|
||||||
data.country_code,
|
data.country_code,
|
||||||
true
|
true
|
||||||
|
|
|
@ -45,6 +45,7 @@ class SingleProductHandler extends BaseHandler {
|
||||||
).simulate( ( data ) => {
|
).simulate( ( data ) => {
|
||||||
const transaction = new TransactionInfo(
|
const transaction = new TransactionInfo(
|
||||||
data.total,
|
data.total,
|
||||||
|
data.shipping_fee,
|
||||||
data.currency_code,
|
data.currency_code,
|
||||||
data.country_code,
|
data.country_code,
|
||||||
true
|
true
|
||||||
|
|
|
@ -3,19 +3,35 @@ export default class TransactionInfo {
|
||||||
#currency = '';
|
#currency = '';
|
||||||
#isFinal = false;
|
#isFinal = false;
|
||||||
#amount = 0;
|
#amount = 0;
|
||||||
|
#shippingFee = 0;
|
||||||
|
|
||||||
constructor( amount, currency, country, isFinal ) {
|
constructor( total, shippingFee, currency, country, isFinal ) {
|
||||||
this.#country = country;
|
this.#country = country;
|
||||||
this.#currency = currency;
|
this.#currency = currency;
|
||||||
this.#isFinal = isFinal;
|
this.#isFinal = isFinal;
|
||||||
|
|
||||||
this.amount = amount;
|
this.shippingFee = shippingFee;
|
||||||
|
this.amount = total - shippingFee;
|
||||||
}
|
}
|
||||||
|
|
||||||
set amount( newAmount ) {
|
set amount( newAmount ) {
|
||||||
this.#amount = Number( newAmount ) || 0;
|
this.#amount = Number( newAmount ) || 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set shippingFee( newCost ) {
|
||||||
|
this.#shippingFee = Number( newCost ) || 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
set total( newTotal ) {
|
||||||
|
newTotal = Number( newTotal ) || 0;
|
||||||
|
|
||||||
|
if ( ! newTotal ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.#amount = newTotal - this.#shippingFee;
|
||||||
|
}
|
||||||
|
|
||||||
get currencyCode() {
|
get currencyCode() {
|
||||||
return this.#currency;
|
return this.#currency;
|
||||||
}
|
}
|
||||||
|
@ -29,7 +45,9 @@ export default class TransactionInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
get totalPrice() {
|
get totalPrice() {
|
||||||
return this.#amount.toFixed( 2 );
|
const total = this.#amount + this.#shippingFee;
|
||||||
|
|
||||||
|
return total.toFixed( 2 );
|
||||||
}
|
}
|
||||||
|
|
||||||
get dataObject() {
|
get dataObject() {
|
||||||
|
|
|
@ -90,7 +90,8 @@ class UpdatePaymentDataEndpoint {
|
||||||
WC()->cart->calculate_fees();
|
WC()->cart->calculate_fees();
|
||||||
WC()->cart->calculate_totals();
|
WC()->cart->calculate_totals();
|
||||||
|
|
||||||
$total = (float) WC()->cart->get_total( 'numeric' );
|
$total = (float) WC()->cart->get_total( 'numeric' );
|
||||||
|
$shipping_fee = (float) WC()->cart->get_shipping_total();
|
||||||
|
|
||||||
// Shop settings.
|
// Shop settings.
|
||||||
$base_location = wc_get_base_location();
|
$base_location = wc_get_base_location();
|
||||||
|
@ -100,7 +101,7 @@ class UpdatePaymentDataEndpoint {
|
||||||
wp_send_json_success(
|
wp_send_json_success(
|
||||||
array(
|
array(
|
||||||
'total' => $total,
|
'total' => $total,
|
||||||
'total_str' => ( new Money( $total, $currency_code ) )->value_str(),
|
'shipping_fee' => $shipping_fee,
|
||||||
'currency_code' => $currency_code,
|
'currency_code' => $currency_code,
|
||||||
'country_code' => $shop_country_code,
|
'country_code' => $shop_country_code,
|
||||||
'shipping_options' => $this->get_shipping_options(),
|
'shipping_options' => $this->get_shipping_options(),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue