woocommerce-paypal-payments/modules/ppcp-googlepay/resources/js/Helper/TransactionInfo.js

74 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-08-13 18:41:42 +02:00
export default class TransactionInfo {
#country = '';
#currency = '';
#amount = 0;
2024-08-13 19:31:51 +02:00
#shippingFee = 0;
2024-08-13 18:41:42 +02:00
constructor( total, shippingFee, currency, country ) {
2024-08-13 18:41:42 +02:00
this.#country = country;
this.#currency = currency;
2024-08-13 19:31:51 +02:00
this.shippingFee = shippingFee;
this.amount = total - shippingFee;
2024-08-13 18:41:42 +02:00
}
set amount( newAmount ) {
this.#amount = this.toAmount( newAmount );
2024-08-13 18:41:42 +02:00
}
get amount() {
return this.#amount;
2024-08-13 19:31:51 +02:00
}
set shippingFee( newCost ) {
this.#shippingFee = this.toAmount( newCost );
}
2024-08-13 19:31:51 +02:00
get shippingFee() {
return this.#shippingFee;
2024-08-13 19:31:51 +02:00
}
2024-08-13 18:41:42 +02:00
get currencyCode() {
return this.#currency;
}
get countryCode() {
return this.#country;
}
get totalPrice() {
2024-08-13 19:31:51 +02:00
const total = this.#amount + this.#shippingFee;
return total.toFixed( 2 );
2024-08-13 18:41:42 +02:00
}
get finalObject() {
2024-08-13 18:41:42 +02:00
return {
countryCode: this.countryCode,
currencyCode: this.currencyCode,
totalPriceStatus: 'FINAL',
2024-08-13 18:41:42 +02:00
totalPrice: this.totalPrice,
};
}
/**
* Converts the value to a number and rounds to a precision of 2 digits.
*
* @param {any} value - The value to sanitize.
* @return {number} Numeric value.
*/
toAmount( value ) {
value = Number( value ) || 0;
return Math.round( value * 100 ) / 100;
}
setTotal( totalPrice, shippingFee ) {
totalPrice = this.toAmount( totalPrice );
if ( totalPrice ) {
this.shippingFee = shippingFee;
this.amount = totalPrice - this.shippingFee;
}
}
2024-08-13 18:41:42 +02:00
}