Skip to content

Commit

Permalink
Merge pull request #106 from bold-commerce/CHK-4371
Browse files Browse the repository at this point in the history
ApplePay for PPCP
  • Loading branch information
davemacaulay authored Oct 31, 2024
2 parents 1cc6ae8 + 28f2aad commit 91c264f
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 28 deletions.
8 changes: 5 additions & 3 deletions Service/ExpressPay/Order/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ public function execute($quoteMaskId, $gatewayId, $shippingStrategy): array
throw new LocalizedException(__('Could not create Express Pay order. Invalid quote ID "%1".', $quoteId));
}

if (!empty($quote->getShippingAddress()->getShippingMethod())) {
$quote->getShippingAddress()->setShippingMethod('');
}
$hasBillingData = $quote->getBillingAddress()->getFirstname() && $quote->getBillingAddress()->getStreet();

if (!$hasBillingData && !empty($quote->getShippingAddress()->getShippingMethod())) {
$quote->getShippingAddress()->setShippingMethod('');
}

$websiteId = (int)$quote->getStore()->getWebsiteId();
$uri = 'checkout/orders/{{shopId}}/wallet_pay';
Expand Down
8 changes: 7 additions & 1 deletion Service/ExpressPay/QuoteConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function convertLocale(Quote $quote): array
public function convertCustomer(Quote $quote): array
{
$billingAddress = $quote->getBillingAddress();
$shippingAddress = $quote->getShippingAddress();

if ($billingAddress->getId() === null) {
return [];
Expand All @@ -108,7 +109,7 @@ public function convertCustomer(Quote $quote): array
'customer' => [
'first_name' => $billingAddress->getFirstname() ?? '',
'last_name' => $billingAddress->getLastname() ?? '',
'email' => $billingAddress->getEmail() ?? ''
'email' => $billingAddress->getEmail() ?? $shippingAddress->getEmail() ?? ''
]
]
];
Expand Down Expand Up @@ -177,6 +178,8 @@ static function (Rate $rate) use ($currencyCode): array {

if ($includeAddress && $hasRequiredAddressData) {
$convertedQuote['order_data']['shipping_address'] = [
'first_name' => $shippingAddress->getFirstname() ?? '',
'last_name' => $shippingAddress->getLastname() ?? '',
'address_line_1' => $shippingAddress->getStreet()[0] ?? '',
'address_line_2' => $shippingAddress->getStreet()[1] ?? '',
'city' => $shippingAddress->getCity() ?? '',
Expand All @@ -186,6 +189,9 @@ static function (Rate $rate) use ($currencyCode): array {
];
}

if ($shippingAddress->getTelephone()) {
$convertedQuote['order_data']['shipping_address']['phone_number'] = $shippingAddress->getTelephone();
}
if ($hasRequiredAddressData && $shippingAddress->hasShippingMethod()) { // @phpstan-ignore method.notFound
$convertedQuote['order_data']['selected_shipping_option'] = [
'id' => $shippingAddress->getShippingMethod(),
Expand Down
3 changes: 3 additions & 0 deletions etc/csp_whitelist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<value id="bold_static_eps" type="host">static-eps.secure.boldcommerce.com</value>
<value id="bold_static_eps_staging" type="host">static-eps.secure.staging.boldcommerce.com</value>
<value id="bold_local" type="host">*.bold.ninja</value>
<value id="bold_applepay" type="host">*.cdn-apple.com</value>
</values>
</policy>
<policy id="script-src">
Expand All @@ -33,6 +34,8 @@
<value id="bold_static_eps_staging" type="host">static-eps.secure.staging.boldcommerce.com</value>
<value id="bold_paypal_script" type="hash" algorithm="sha256">1ozuCt5fPv779wJQEWXLF2gXag+V1bnu3hmAhDbY0Cg=</value>
<value id="bold_local" type="host">*.bold.ninja</value>
<value id="bold_applepay_script" type="host">*.cdn-apple.com</value>
<value id="bold_stripe" type="host">*.stripe.com</value>
</values>
</policy>
<policy id="frame-src">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ define(
shipping_total: parseFloat(totals()['shipping_amount'] || 0) * 100,
discounts_total: parseFloat(totals()['discount_amount'] || 0) * 100,
fees_total: parseFloat(totals()['fee_amount'] || 0) * 100,
taxes_total: parseFloat(totals()['tax'] || 0) * 100,
taxes_total: parseFloat(totals()['tax_amount'] || 0) * 100,
};
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ define(
* @param {Boolean} saveBillingAddress - Save billing address with shipping information.
* @return {Deferred}
*/
return function (saveBillingAddress = false) {
return async function (saveBillingAddress = false) {
let payload;
payload = {
addressInformation: {
Expand All @@ -37,6 +37,10 @@ define(
return storage.post(
resourceUrlManager.getUrlForSetShippingInformation(quote),
JSON.stringify(payload)
).done(
function (response) {
quote.setTotals(response.totals);
}
).fail((response) => {
errorProcessor.process(response);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ define(
region_code: state,
region_id: regionId
} : regionName;
const email = addressData['email'] || quote.shippingAddress.email || quote.billingAddress.email;
const phone = addressData['phone'] || quote.shippingAddress.telephone || quote.billingAddress.telephone;
const quoteAddress = magentoAddressConverter.formAddressDataToQuoteAddress(
{
address_type: addressType,
Expand All @@ -70,10 +72,10 @@ define(
city: addressData['city'] || addressData['locality'],
region: region,
region_id: regionId,
telephone: addressData['phoneNumber'] ?? null,
telephone: phone ?? null,
postcode: addressData['postal_code'] || addressData['postalCode'],
country_id: countryCode,
email: addressData['email'] ?? null
email: email ?? null
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ define(
* @return {Promise}
*/
return async function (paymentApprovalData) {
const paymentData = paymentApprovalData['payment_data'];
const availableWalletTypes = ['apple', 'google'];
const isWalletPayment = availableWalletTypes.includes(paymentData.payment_type);

let order;
try {
order = await getExpressPayOrderAction(
Expand All @@ -41,17 +45,22 @@ define(
address.last_name = order.last_name;
address.state = address.province;
address.country_code = address.country;
address.email = order.email;

if (!address.email && order.email) {
address.email = order.email;
}

delete address.province;
delete address.country;

return address;
}

quote.guestEmail = order.email;
updateQuoteAddressAction('shipping', _convertAddress(order.shipping_address, order));
updateQuoteAddressAction('billing', _convertAddress(order.billing_address, order));
if (!isWalletPayment) {
quote.guestEmail = order.email;
updateQuoteAddressAction('shipping', _convertAddress(order.shipping_address, order));
updateQuoteAddressAction('billing', _convertAddress(order.billing_address, order));
}
};
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ define(
[
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/model/shipping-service',
'Bold_CheckoutPaymentBooster/js/action/express-pay/save-shipping-information-action'
'Bold_CheckoutPaymentBooster/js/action/express-pay/save-shipping-information-action',
'Magento_Checkout/js/action/select-shipping-method'
],
function (
quote,
shippingService,
saveShippingInformation
saveShippingInformation,
selectShippingMethodAction
) {
'use strict';

Expand All @@ -17,13 +19,13 @@ define(
* @param {Object} shippingMethod
* @return void
*/
return function (shippingMethod = null) {
return async function (shippingMethod = null) {
let newMethod = null;
if (shippingMethod !== null) {
let availableMethods = shippingService.getShippingRates().filter((method) => {
let methodId = `${method.carrier_code}_${method.method_code}`;
methodId = methodId.replace(/\s/g, '');
return methodId === shippingMethod['id'];
return methodId === shippingMethod['id'] || methodId === shippingMethod['identifier'];
});
if (availableMethods.length > 0) {
newMethod = availableMethods[0];
Expand All @@ -32,12 +34,13 @@ define(
newMethod = shippingService.getShippingRates().first();
}
if (newMethod !== null) {
quote.shippingMethod(newMethod);
selectShippingMethodAction(newMethod);
}
if (quote.guestEmail === null) {
quote.guestEmail = '[email protected]';
}
saveShippingInformation();

await saveShippingInformation();
}
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,37 @@ define(
* @return {Promise}
*/
return async function (paymentType, paymentInformation, paymentApprovalData) {
const paymentData = paymentApprovalData['payment_data'];
const availableWalletTypes = ['apple', 'google'];
const isWalletPayment = availableWalletTypes.includes(paymentData.payment_type);

if (paymentApprovalData === null) {
console.error('Express Pay payment data is not set.');
return;
}
const paymentMethodData = {
method: 'bold',
method: window.checkoutConfig?.bold?.paymentBooster?.payment?.method ?? 'bold',
};
if (paymentType === 'ppcp') {

if (paymentType === 'ppcp' || isWalletPayment) {
paymentMethodData['additional_data'] = {
order_id: paymentApprovalData?.payment_data.order_id
};
}

if (paymentType === 'ppcp') {
await updateQuotePPCPAction(paymentApprovalData);
} else {
await updateQuoteBraintreeAction(paymentInformation, paymentApprovalData);
}
try {
await saveShippingInformationAction(true);
} catch (error) {
console.error('Could not save shipping information for Express Pay order.', error);
return;

if (!isWalletPayment) {
try {
await saveShippingInformationAction(true);
} catch (error) {
console.error('Could not save shipping information for Express Pay order.', error);
return;
}
}
const messageContainer = registry.get('checkout.errors').messageContainer;
$.when(placeOrderAction(paymentMethodData, messageContainer))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
define(
[
'Bold_CheckoutPaymentBooster/js/action/express-pay/update-quote-address-action',
'Bold_CheckoutPaymentBooster/js/action/express-pay/update-quote-shipping-method-action',
'Bold_CheckoutPaymentBooster/js/action/express-pay/save-shipping-information-action',
'Bold_CheckoutPaymentBooster/js/action/express-pay/create-wallet-pay-order-action'
],
function (
updateQuoteAddressAction,
updateQuoteShippingMethodAction,
saveShippingInformationAction,
createWalletPayOrderAction
) {
'use strict';
Expand All @@ -14,9 +20,27 @@ define(
* @param {Object} paymentOrder
*/
return async function (paymentType, paymentPayload) {
const paymentData = paymentPayload['payment_data'];
const availableWalletTypes = ['apple', 'google'];
const isWalletPayment = availableWalletTypes.includes(paymentData.payment_type);

if (paymentType !== 'ppcp') {
return;
}

if (isWalletPayment) {
if (paymentData['shipping_address']) {
updateQuoteAddressAction('shipping', paymentData['shipping_address']);
}
if (paymentData['billing_address']) {
updateQuoteAddressAction('billing', paymentData['billing_address']);
}
} else {
await updateQuoteShippingMethodAction(paymentData['shipping_options']);
}

await saveShippingInformationAction(true);

const walletPayResult = await createWalletPayOrderAction(paymentPayload);
return {
payment_data: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
define(
[
'jquery',
'Bold_CheckoutPaymentBooster/js/action/express-pay/update-quote-address-action',
'Bold_CheckoutPaymentBooster/js/action/express-pay/update-quote-shipping-method-action',
'Bold_CheckoutPaymentBooster/js/action/express-pay/update-wallet-pay-order-action',
'Bold_CheckoutPaymentBooster/js/action/express-pay/get-required-order-data-action',
'Magento_Checkout/js/action/get-totals'
],
function (
$,
updateQuoteAddressAction,
updateQuoteShippingMethodAction,
updateWalletPayOrderAction,
getRequiredOrderDataAction
getRequiredOrderDataAction,
getTotalsAction
) {
'use strict';

Expand All @@ -21,13 +25,18 @@ define(
*/
return async function (paymentType, paymentPayload) {
const paymentData = paymentPayload['payment_data'];
const availableWalletTypes = ['apple', 'google'];
const isWalletPayment = availableWalletTypes.includes(paymentData.payment_type);

if (paymentData['shipping_address']) {
updateQuoteAddressAction('shipping', paymentData['shipping_address']);
}
updateQuoteShippingMethodAction(paymentData['shipping_options']);
if (paymentType === 'ppcp') {
await updateQuoteShippingMethodAction(paymentData['shipping_options']);

if (paymentType === 'ppcp' && !isWalletPayment) {
await updateWalletPayOrderAction(paymentData['order_id']);
}

return getRequiredOrderDataAction(
paymentPayload['require_order_data'] || []
);
Expand Down

0 comments on commit 91c264f

Please sign in to comment.