From 7946150f5b975ed228cb7ff8fc5b28f8fe5b5921 Mon Sep 17 00:00:00 2001 From: Philipp Stracker Date: Tue, 13 Aug 2024 18:41:42 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20New=20TransactionInfo=20DTO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/js/Context/BaseHandler.js | 15 ++++--- .../resources/js/Context/PayNowHandler.js | 15 ++++--- .../js/Context/SingleProductHandler.js | 15 ++++--- .../resources/js/GooglepayButton.js | 4 +- .../resources/js/Helper/TransactionInfo.js | 43 +++++++++++++++++++ 5 files changed, 72 insertions(+), 20 deletions(-) create mode 100644 modules/ppcp-googlepay/resources/js/Helper/TransactionInfo.js diff --git a/modules/ppcp-googlepay/resources/js/Context/BaseHandler.js b/modules/ppcp-googlepay/resources/js/Context/BaseHandler.js index d61b674a7..014a6f2ce 100644 --- a/modules/ppcp-googlepay/resources/js/Context/BaseHandler.js +++ b/modules/ppcp-googlepay/resources/js/Context/BaseHandler.js @@ -1,5 +1,6 @@ import ErrorHandler from '../../../../ppcp-button/resources/js/modules/ErrorHandler'; import CartActionHandler from '../../../../ppcp-button/resources/js/modules/ActionHandler/CartActionHandler'; +import TransactionInfo from '../Helper/TransactionInfo'; class BaseHandler { constructor( buttonConfig, ppcpConfig, externalHandler ) { @@ -35,12 +36,14 @@ class BaseHandler { // handle script reload const data = result.data; - resolve( { - countryCode: data.country_code, - currencyCode: data.currency_code, - totalPriceStatus: 'FINAL', - totalPrice: data.total_str, - } ); + resolve( + new TransactionInfo( + data.total, + data.currency_code, + data.country_code, + true + ) + ); } ); } ); } diff --git a/modules/ppcp-googlepay/resources/js/Context/PayNowHandler.js b/modules/ppcp-googlepay/resources/js/Context/PayNowHandler.js index 79de6b39d..29e91d39b 100644 --- a/modules/ppcp-googlepay/resources/js/Context/PayNowHandler.js +++ b/modules/ppcp-googlepay/resources/js/Context/PayNowHandler.js @@ -1,6 +1,7 @@ import Spinner from '../../../../ppcp-button/resources/js/modules/Helper/Spinner'; import BaseHandler from './BaseHandler'; import CheckoutActionHandler from '../../../../ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler'; +import TransactionInfo from '../Helper/TransactionInfo'; class PayNowHandler extends BaseHandler { validateContext() { @@ -14,12 +15,14 @@ class PayNowHandler extends BaseHandler { return new Promise( async ( resolve, reject ) => { const data = this.ppcpConfig.pay_now; - resolve( { - countryCode: data.country_code, - currencyCode: data.currency_code, - totalPriceStatus: 'FINAL', - totalPrice: data.total_str, - } ); + resolve( + new TransactionInfo( + data.total, + data.currency_code, + data.country_code, + true + ) + ); } ); } diff --git a/modules/ppcp-googlepay/resources/js/Context/SingleProductHandler.js b/modules/ppcp-googlepay/resources/js/Context/SingleProductHandler.js index a8aa6e8bd..6066b9eca 100644 --- a/modules/ppcp-googlepay/resources/js/Context/SingleProductHandler.js +++ b/modules/ppcp-googlepay/resources/js/Context/SingleProductHandler.js @@ -3,6 +3,7 @@ import SimulateCart from '../../../../ppcp-button/resources/js/modules/Helper/Si import ErrorHandler from '../../../../ppcp-button/resources/js/modules/ErrorHandler'; import UpdateCart from '../../../../ppcp-button/resources/js/modules/Helper/UpdateCart'; import BaseHandler from './BaseHandler'; +import TransactionInfo from '../Helper/TransactionInfo'; class SingleProductHandler extends BaseHandler { validateContext() { @@ -42,12 +43,14 @@ class SingleProductHandler extends BaseHandler { this.ppcpConfig.ajax.simulate_cart.endpoint, this.ppcpConfig.ajax.simulate_cart.nonce ).simulate( ( data ) => { - resolve( { - countryCode: data.country_code, - currencyCode: data.currency_code, - totalPriceStatus: 'FINAL', - totalPrice: data.total_str, - } ); + resolve( + new TransactionInfo( + data.total, + data.currency_code, + data.country_code, + true + ) + ); }, products ); } ); } diff --git a/modules/ppcp-googlepay/resources/js/GooglepayButton.js b/modules/ppcp-googlepay/resources/js/GooglepayButton.js index 77c94b3fe..cd468b93e 100644 --- a/modules/ppcp-googlepay/resources/js/GooglepayButton.js +++ b/modules/ppcp-googlepay/resources/js/GooglepayButton.js @@ -331,7 +331,7 @@ class GooglepayButton { const paymentDataRequest = Object.assign( {}, baseRequest ); paymentDataRequest.allowedPaymentMethods = googlePayConfig.allowedPaymentMethods; - paymentDataRequest.transactionInfo = this.transactionInfo; + paymentDataRequest.transactionInfo = this.transactionInfo.dataObject; paymentDataRequest.merchantInfo = googlePayConfig.merchantInfo; if ( @@ -376,7 +376,7 @@ class GooglepayButton { const updatedData = await new UpdatePaymentData( this.buttonConfig.ajax.update_payment_data ).update( paymentData ); - const transactionInfo = this.transactionInfo; + const transactionInfo = this.transactionInfo.dataObject; this.log( 'onPaymentDataChanged:updatedData', updatedData ); this.log( diff --git a/modules/ppcp-googlepay/resources/js/Helper/TransactionInfo.js b/modules/ppcp-googlepay/resources/js/Helper/TransactionInfo.js new file mode 100644 index 000000000..66e198374 --- /dev/null +++ b/modules/ppcp-googlepay/resources/js/Helper/TransactionInfo.js @@ -0,0 +1,43 @@ +export default class TransactionInfo { + #country = ''; + #currency = ''; + #isFinal = false; + #amount = 0; + + constructor( amount, currency, country, isFinal ) { + this.#country = country; + this.#currency = currency; + this.#isFinal = isFinal; + + this.amount = amount; + } + + set amount( newAmount ) { + this.#amount = Number( newAmount ) || 0; + } + + get currencyCode() { + return this.#currency; + } + + get countryCode() { + return this.#country; + } + + get totalPriceStatus() { + return this.#isFinal ? 'FINAL' : 'DRAFT'; + } + + get totalPrice() { + return this.#amount.toFixed( 2 ); + } + + get dataObject() { + return { + countryCode: this.countryCode, + currencyCode: this.currencyCode, + totalPriceStatus: this.totalPriceStatus, + totalPrice: this.totalPrice, + }; + } +}