Skip to content

Commit

Permalink
fix: rework Stripe Google Pay using the payment request button
Browse files Browse the repository at this point in the history
  • Loading branch information
vstudenichnik-insoft committed Jan 18, 2024
1 parent d018da4 commit 0ddf9b4
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 225 deletions.
67 changes: 4 additions & 63 deletions src/payment/apple/stripe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Payment from '../payment';
import { stripeAmountByCurrency } from '../../utils/stripe';
import { getPaymentRequestData } from '../../utils/stripe';
import {
PaymentMethodDisabledError,
LibraryNotLoadedError,
Expand Down Expand Up @@ -101,7 +101,7 @@ export default class StripeApplePayment extends Payment {
requestPayerPhone: Boolean(phone),
requestShipping: Boolean(shipping),
disableWallets: ['googlePay', 'browserCard', 'link'],
...this._getPaymentRequestData(cart),
...getPaymentRequestData(cart),
});

paymentRequest.on(
Expand All @@ -117,65 +117,6 @@ export default class StripeApplePayment extends Payment {
return paymentRequest;
}

_getPaymentRequestData(cart) {
const {
currency,
shipping,
items,
capture_total,
shipment_rating,
shipment_total,
tax_included_total,
settings,
} = cart;

const stripeCurrency = currency.toLowerCase();
const displayItems = items.map((item) => ({
label: item.product.name,
amount: stripeAmountByCurrency(
currency,
item.price_total - item.discount_total,
),
}));

if (tax_included_total) {
displayItems.push({
label: 'Taxes',
amount: stripeAmountByCurrency(currency, tax_included_total),
});
}

if (shipping.price && shipment_total) {
displayItems.push({
label: shipping.service_name,
amount: stripeAmountByCurrency(currency, shipment_total),
});
}

const services = shipment_rating && shipment_rating.services;
let shippingOptions;
if (services) {
shippingOptions = services.map((service) => ({
id: service.id,
label: service.name,
detail: service.description,
amount: stripeAmountByCurrency(currency, service.price),
}));
}

return {
country: settings.country,
currency: stripeCurrency,
total: {
label: settings.name,
amount: stripeAmountByCurrency(currency, capture_total),
pending: true,
},
displayItems,
shippingOptions,
};
}

async _onShippingAddressChange(event) {
const { shippingAddress, updateWith } = event;
const shipping = this._mapShippingAddress(shippingAddress);
Expand All @@ -185,7 +126,7 @@ export default class StripeApplePayment extends Payment {
});

if (cart) {
updateWith({ status: 'success', ...this._getPaymentRequestData(cart) });
updateWith({ status: 'success', ...getPaymentRequestData(cart) });
} else {
updateWith({ status: 'invalid_shipping_address' });
}
Expand All @@ -198,7 +139,7 @@ export default class StripeApplePayment extends Payment {
});

if (cart) {
updateWith({ status: 'success', ...this._getPaymentRequestData(cart) });
updateWith({ status: 'success', ...getPaymentRequestData(cart) });
} else {
updateWith({ status: 'fail' });
}
Expand Down
Loading

0 comments on commit 0ddf9b4

Please sign in to comment.