mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 13:44:42 +08:00
Add PayNow support to ApplePay
This commit is contained in:
parent
cb598ccbca
commit
ad32a4ff21
5 changed files with 109 additions and 42 deletions
|
@ -244,6 +244,11 @@ class ApplepayButton {
|
||||||
requiredShippingContactFields: ["postalAddress", "email", "phone"],
|
requiredShippingContactFields: ["postalAddress", "email", "phone"],
|
||||||
requiredBillingContactFields: ["postalAddress", "email", "phone"],
|
requiredBillingContactFields: ["postalAddress", "email", "phone"],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!this.contextHandler.shippingAllowed()) {
|
||||||
|
baseRequest.requiredShippingContactFields = [];
|
||||||
|
}
|
||||||
|
|
||||||
const paymentDataRequest = Object.assign({}, baseRequest);
|
const paymentDataRequest = Object.assign({}, baseRequest);
|
||||||
paymentDataRequest.currencyCode = buttonConfig.shop.currencyCode;
|
paymentDataRequest.currencyCode = buttonConfig.shop.currencyCode;
|
||||||
paymentDataRequest.total = {
|
paymentDataRequest.total = {
|
||||||
|
@ -508,6 +513,38 @@ class ApplepayButton {
|
||||||
if (confirmOrderResponse && confirmOrderResponse.approveApplePayPayment) {
|
if (confirmOrderResponse && confirmOrderResponse.approveApplePayPayment) {
|
||||||
if (confirmOrderResponse.approveApplePayPayment.status === "APPROVED") {
|
if (confirmOrderResponse.approveApplePayPayment.status === "APPROVED") {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
if (!this.contextHandler.shippingAllowed()) {
|
||||||
|
// No shipping, expect immediate capture, ex: PayNow.
|
||||||
|
|
||||||
|
let approveFailed = false;
|
||||||
|
await this.contextHandler.approveOrder({
|
||||||
|
orderID: id
|
||||||
|
}, { // actions mock object.
|
||||||
|
restart: () => new Promise((resolve, reject) => {
|
||||||
|
approveFailed = true;
|
||||||
|
resolve();
|
||||||
|
}),
|
||||||
|
order: {
|
||||||
|
get: () => new Promise((resolve, reject) => {
|
||||||
|
resolve(null);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!approveFailed) {
|
||||||
|
this.log('onpaymentauthorized approveOrder OK');
|
||||||
|
session.completePayment(ApplePaySession.STATUS_SUCCESS);
|
||||||
|
} else {
|
||||||
|
this.log('onpaymentauthorized approveOrder FAIL');
|
||||||
|
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
||||||
|
session.abort()
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Default payment.
|
||||||
|
|
||||||
let data = {
|
let data = {
|
||||||
billing_contact: event.payment.billingContact,
|
billing_contact: event.payment.billingContact,
|
||||||
shipping_contact: event.payment.shippingContact,
|
shipping_contact: event.payment.shippingContact,
|
||||||
|
@ -520,6 +557,9 @@ class ApplepayButton {
|
||||||
} else {
|
} else {
|
||||||
session.completePayment(ApplePaySession.STATUS_FAILURE)
|
session.completePayment(ApplePaySession.STATUS_FAILURE)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
session.completePayment(ApplePaySession.STATUS_FAILURE);
|
||||||
session.abort()
|
session.abort()
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
import ErrorHandler from "../../../../ppcp-button/resources/js/modules/ErrorHandler";
|
import ErrorHandler from "../../../../ppcp-button/resources/js/modules/ErrorHandler";
|
||||||
import CartActionHandler
|
import CartActionHandler
|
||||||
from "../../../../ppcp-button/resources/js/modules/ActionHandler/CartActionHandler";
|
from "../../../../ppcp-button/resources/js/modules/ActionHandler/CartActionHandler";
|
||||||
import onApprove
|
|
||||||
from "../../../../ppcp-button/resources/js/modules/OnApproveHandler/onApproveForContinue";
|
|
||||||
|
|
||||||
class BaseHandler {
|
class BaseHandler {
|
||||||
|
|
||||||
|
@ -11,6 +9,10 @@ class BaseHandler {
|
||||||
this.ppcpConfig = ppcpConfig;
|
this.ppcpConfig = ppcpConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shippingAllowed() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
transactionInfo() {
|
transactionInfo() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
|
@ -42,30 +44,25 @@ class BaseHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
createOrder() {
|
createOrder() {
|
||||||
const errorHandler = new ErrorHandler(
|
return this.actionHandler().configuration().createOrder(null, null);
|
||||||
this.ppcpConfig.labels.error.generic,
|
|
||||||
document.querySelector('.woocommerce-notices-wrapper')
|
|
||||||
);
|
|
||||||
|
|
||||||
const actionHandler = new CartActionHandler(
|
|
||||||
this.ppcpConfig,
|
|
||||||
errorHandler,
|
|
||||||
);
|
|
||||||
|
|
||||||
return actionHandler.configuration().createOrder(null, null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
approveOrderForContinue(data, actions) {
|
approveOrder(data, actions) {
|
||||||
const errorHandler = new ErrorHandler(
|
return this.actionHandler().configuration().onApprove(data, actions);
|
||||||
|
}
|
||||||
|
|
||||||
|
actionHandler() {
|
||||||
|
return new CartActionHandler(
|
||||||
|
this.ppcpConfig,
|
||||||
|
this.errorHandler(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
errorHandler() {
|
||||||
|
return new ErrorHandler(
|
||||||
this.ppcpConfig.labels.error.generic,
|
this.ppcpConfig.labels.error.generic,
|
||||||
document.querySelector('.woocommerce-notices-wrapper')
|
document.querySelector('.woocommerce-notices-wrapper')
|
||||||
);
|
);
|
||||||
|
|
||||||
let onApproveHandler = onApprove({
|
|
||||||
config: this.ppcpConfig
|
|
||||||
}, errorHandler);
|
|
||||||
|
|
||||||
return onApproveHandler(data, actions);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import CheckoutHandler from "./CheckoutHandler";
|
||||||
import CartBlockHandler from "./CartBlockHandler";
|
import CartBlockHandler from "./CartBlockHandler";
|
||||||
import CheckoutBlockHandler from "./CheckoutBlockHandler";
|
import CheckoutBlockHandler from "./CheckoutBlockHandler";
|
||||||
import MiniCartHandler from "./MiniCartHandler";
|
import MiniCartHandler from "./MiniCartHandler";
|
||||||
|
import PayNowHandler from "./PayNowHandler";
|
||||||
|
|
||||||
class ContextHandlerFactory {
|
class ContextHandlerFactory {
|
||||||
|
|
||||||
|
@ -14,8 +15,9 @@ class ContextHandlerFactory {
|
||||||
case 'cart':
|
case 'cart':
|
||||||
return new CartHandler(buttonConfig, ppcpConfig);
|
return new CartHandler(buttonConfig, ppcpConfig);
|
||||||
case 'checkout':
|
case 'checkout':
|
||||||
case 'pay-now': // same as checkout
|
|
||||||
return new CheckoutHandler(buttonConfig, ppcpConfig);
|
return new CheckoutHandler(buttonConfig, ppcpConfig);
|
||||||
|
case 'pay-now':
|
||||||
|
return new PayNowHandler(buttonConfig, ppcpConfig);
|
||||||
case 'mini-cart':
|
case 'mini-cart':
|
||||||
return new MiniCartHandler(buttonConfig, ppcpConfig);
|
return new MiniCartHandler(buttonConfig, ppcpConfig);
|
||||||
case 'cart-block':
|
case 'cart-block':
|
||||||
|
|
35
modules/ppcp-applepay/resources/js/Context/PayNowHandler.js
Normal file
35
modules/ppcp-applepay/resources/js/Context/PayNowHandler.js
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
import Spinner from "../../../../ppcp-button/resources/js/modules/Helper/Spinner";
|
||||||
|
import BaseHandler from "./BaseHandler";
|
||||||
|
import CheckoutActionHandler
|
||||||
|
from "../../../../ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler";
|
||||||
|
|
||||||
|
class PayNowHandler extends BaseHandler {
|
||||||
|
|
||||||
|
shippingAllowed() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
transactionInfo() {
|
||||||
|
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
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
actionHandler() {
|
||||||
|
return new CheckoutActionHandler(
|
||||||
|
this.ppcpConfig,
|
||||||
|
this.errorHandler(),
|
||||||
|
new Spinner()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export default PayNowHandler;
|
|
@ -48,23 +48,16 @@ class SingleProductHandler extends BaseHandler {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
createOrder() {
|
actionHandler() {
|
||||||
const errorHandler = new ErrorHandler(
|
return new SingleProductActionHandler(
|
||||||
this.ppcpConfig.labels.error.generic,
|
|
||||||
document.querySelector('.woocommerce-notices-wrapper')
|
|
||||||
);
|
|
||||||
|
|
||||||
const actionHandler = new SingleProductActionHandler(
|
|
||||||
this.ppcpConfig,
|
this.ppcpConfig,
|
||||||
new UpdateCart(
|
new UpdateCart(
|
||||||
this.ppcpConfig.ajax.change_cart.endpoint,
|
this.ppcpConfig.ajax.change_cart.endpoint,
|
||||||
this.ppcpConfig.ajax.change_cart.nonce,
|
this.ppcpConfig.ajax.change_cart.nonce,
|
||||||
),
|
),
|
||||||
document.querySelector('form.cart'),
|
document.querySelector('form.cart'),
|
||||||
errorHandler,
|
this.errorHandler(),
|
||||||
);
|
);
|
||||||
|
|
||||||
return actionHandler.configuration().createOrder();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue