-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add express button on block checkout
- Loading branch information
Showing
7 changed files
with
259 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { request } from "./applePayRequest"; | ||
import { createAppleErrors } from "./applePayError"; | ||
import { handleApplePaySession } from './applePaySessionHandler'; | ||
|
||
export const registerApplePayButton = ({ mollieApplePayBlockDataCart, wc, isAppleSession }) => { | ||
if (!mollieApplePayBlockDataCart || mollieApplePayBlockDataCart.length === 0) { | ||
return; | ||
} | ||
|
||
const { ApplePaySession } = window; | ||
if (!(ApplePaySession && ApplePaySession.canMakePayments() && isAppleSession)) { | ||
return null; | ||
} | ||
|
||
const { registerExpressPaymentMethod } = wc.wcBlocksRegistry; | ||
|
||
const { | ||
product: { needShipping = true, subtotal }, | ||
shop: { countryCode, currencyCode = 'EUR', totalLabel = '' }, | ||
ajaxUrl, | ||
} = mollieApplePayBlockDataCart; | ||
|
||
const ApplePayButtonComponent = ({ cart, extensions }) => { | ||
const nonce = document.getElementById("woocommerce-process-checkout-nonce").value; | ||
|
||
const applePaySession = handleApplePaySession({ | ||
countryCode, | ||
currencyCode, | ||
totalLabel, | ||
subtotal, | ||
ajaxUrl, | ||
needShipping, | ||
nonce, | ||
createAppleErrors, | ||
request, | ||
}); | ||
|
||
return ( | ||
<button | ||
type="button" | ||
id="mollie_applepay_button" | ||
className="apple-pay-button apple-pay-button-black" | ||
onClick={applePaySession} | ||
> | ||
</button> | ||
); | ||
}; | ||
|
||
const options = { | ||
name: 'mollie_wc_gateway_applepay_express', | ||
content: < ApplePayButtonComponent/>, | ||
edit: < ApplePayButtonComponent/>, | ||
ariaLabel: 'Apple Pay', | ||
canMakePayment: () => true, | ||
paymentMethodId: 'mollie_wc_gateway_applepay', | ||
supports: { | ||
features: ['products', 'subscriptions'] | ||
} | ||
}; | ||
|
||
registerExpressPaymentMethod(options); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
// This file is used to handle the Apple Pay session and the communication with the server | ||
export const handleApplePaySession = ({ | ||
countryCode, | ||
currencyCode, | ||
totalLabel, | ||
subtotal, | ||
ajaxUrl, | ||
needShipping, | ||
nonce, | ||
createAppleErrors, | ||
request | ||
}) => { | ||
let updatedContactInfo = [] | ||
let selectedShippingMethod = [] | ||
let redirectionUrl = '' | ||
let applePaySession = (event) => { | ||
|
||
event.preventDefault(); | ||
const session = new ApplePaySession(3, request(countryCode, currencyCode, totalLabel, subtotal)) | ||
console.log(session) | ||
session.onshippingmethodselected = function (event) { | ||
console.log('onshippingmethodselected') | ||
jQuery.ajax({ | ||
url: ajaxUrl, | ||
method: 'POST', | ||
data: { | ||
action: 'mollie_apple_pay_update_shipping_method', | ||
shippingMethod: event.shippingMethod, | ||
callerPage: 'cart', | ||
simplifiedContact: updatedContactInfo, | ||
'woocommerce-process-checkout-nonce': nonce, | ||
}, | ||
complete: (jqXHR, textStatus) => { | ||
}, | ||
success: (applePayShippingMethodUpdate, textStatus, jqXHR) => { | ||
let response = applePayShippingMethodUpdate.data | ||
selectedShippingMethod = event.shippingMethod | ||
if (applePayShippingMethodUpdate.success === false) { | ||
response.errors = createAppleErrors(response.errors) | ||
} | ||
this.completeShippingMethodSelection(response) | ||
}, | ||
error: (jqXHR, textStatus, errorThrown) => { | ||
console.warn(textStatus, errorThrown) | ||
session.abort() | ||
}, | ||
}) | ||
} | ||
session.onshippingcontactselected = function (event) { | ||
console.log('onshippingcontactselected') | ||
jQuery.ajax({ | ||
url: ajaxUrl, | ||
method: 'POST', | ||
data: { | ||
action: 'mollie_apple_pay_update_shipping_contact', | ||
simplifiedContact: event.shippingContact, | ||
callerPage: 'cart', | ||
needShipping: needShipping, | ||
'woocommerce-process-checkout-nonce': nonce, | ||
}, | ||
complete: (jqXHR, textStatus) => { | ||
}, | ||
success: (applePayShippingContactUpdate, textStatus, jqXHR) => { | ||
let response = applePayShippingContactUpdate.data | ||
updatedContactInfo = event.shippingContact | ||
if (applePayShippingContactUpdate.success === false) { | ||
response.errors = createAppleErrors(response.errors) | ||
} | ||
if (response.newShippingMethods) { | ||
selectedShippingMethod = response.newShippingMethods[0] | ||
} | ||
this.completeShippingContactSelection(response) | ||
}, | ||
error: (jqXHR, textStatus, errorThrown) => { | ||
console.warn(textStatus, errorThrown) | ||
session.abort() | ||
}, | ||
}) | ||
} | ||
session.onvalidatemerchant = (applePayValidateMerchantEvent) => { | ||
console.log('onvalidatemerchant') | ||
console.log(ajaxUrl) | ||
console.log(applePayValidateMerchantEvent) | ||
console.log(nonce) | ||
jQuery.ajax({ | ||
url: ajaxUrl, | ||
method: 'POST', | ||
data: { | ||
action: 'mollie_apple_pay_validation', | ||
validationUrl: applePayValidateMerchantEvent.validationURL, | ||
'woocommerce-process-checkout-nonce': nonce, | ||
}, | ||
complete: (jqXHR, textStatus) => { | ||
}, | ||
success: (merchantSession, textStatus, jqXHR) => { | ||
if (merchantSession.success === true) { | ||
session.completeMerchantValidation(JSON.parse(merchantSession.data)) | ||
} else { | ||
console.warn(merchantSession.data) | ||
session.abort() | ||
} | ||
}, | ||
error: (jqXHR, textStatus, errorThrown) => { | ||
console.warn(textStatus, errorThrown) | ||
session.abort() | ||
}, | ||
}) | ||
} | ||
session.onpaymentauthorized = (ApplePayPayment) => { | ||
console.log('onpaymentauthorized') | ||
const {billingContact, shippingContact} = ApplePayPayment.payment | ||
|
||
jQuery.ajax({ | ||
url: ajaxUrl, | ||
method: 'POST', | ||
data: { | ||
action: 'mollie_apple_pay_create_order_cart', | ||
shippingContact: ApplePayPayment.payment.shippingContact, | ||
billingContact: ApplePayPayment.payment.billingContact, | ||
token: ApplePayPayment.payment.token, | ||
shippingMethod: selectedShippingMethod, | ||
'mollie-payments-for-woocommerce_issuer_applepay': 'applepay', | ||
'woocommerce-process-checkout-nonce': nonce, | ||
'billing_first_name': billingContact.givenName || '', | ||
'billing_last_name': billingContact.familyName || '', | ||
'billing_company': '', | ||
'billing_country': billingContact.countryCode || '', | ||
'billing_address_1': billingContact.addressLines[0] || '', | ||
'billing_address_2': billingContact.addressLines[1] || '', | ||
'billing_postcode': billingContact.postalCode || '', | ||
'billing_city': billingContact.locality || '', | ||
'billing_state': billingContact.administrativeArea || '', | ||
'billing_phone': billingContact.phoneNumber || '000000000000', | ||
'billing_email': shippingContact.emailAddress || '', | ||
'shipping_first_name': shippingContact.givenName || '', | ||
'shipping_last_name': shippingContact.familyName || '', | ||
'shipping_company': '', | ||
'shipping_country': shippingContact.countryCode || '', | ||
'shipping_address_1': shippingContact.addressLines[0] || '', | ||
'shipping_address_2': shippingContact.addressLines[1] || '', | ||
'shipping_postcode': shippingContact.postalCode || '', | ||
'shipping_city': shippingContact.locality || '', | ||
'shipping_state': shippingContact.administrativeArea || '', | ||
'shipping_phone': shippingContact.phoneNumber || '000000000000', | ||
'shipping_email': shippingContact.emailAddress || '', | ||
'order_comments': '', | ||
'payment_method': 'mollie_wc_gateway_applepay', | ||
'_wp_http_referer': '/?wc-ajax=update_order_review' | ||
}, | ||
complete: (jqXHR, textStatus) => { | ||
|
||
}, | ||
success: (authorizationResult, textStatus, jqXHR) => { | ||
let result = authorizationResult.data | ||
if (authorizationResult.success === true) { | ||
redirectionUrl = result['returnUrl']; | ||
session.completePayment(result['responseToApple']) | ||
window.location.href = redirectionUrl | ||
} else { | ||
result.errors = createAppleErrors(result.errors) | ||
session.completePayment(result) | ||
} | ||
}, | ||
error: (jqXHR, textStatus, errorThrown) => { | ||
console.warn(textStatus, errorThrown) | ||
session.abort() | ||
}, | ||
}) | ||
} | ||
session.begin() | ||
} | ||
|
||
return applePaySession; | ||
}; |
Oops, something went wrong.