Skip to content

Draft: MAT-5778 Added Apple Pay for checkout block payment #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions assets/js/blocks-apple-pay.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react'
import { useEffect, useRef } from '@wordpress/element'

const Shift4ApplePay = ({ onClick, onClose, onSubmit, billing }) => {
const Shift4ApplePay = ({ onClick, onClose, onSubmit, billing, setExpressPaymentError }) => {
const tokenInputRef = useRef(null)
const buttonRef = useRef(null)
const { cartTotal, currency } = billing
Expand Down Expand Up @@ -46,7 +45,7 @@ const Shift4ApplePay = ({ onClick, onClose, onSubmit, billing }) => {

try {
const applePayToken = await window.shift4PayWithApplePay({
value: cartTotal.value,
value: (cartTotal.value / Math.pow(10, currency.minorUnit)).toFixed(currency.minorUnit),
currency: currency.code
})

Expand All @@ -58,7 +57,7 @@ const Shift4ApplePay = ({ onClick, onClose, onSubmit, billing }) => {
// submits the checkout and begins processing
onSubmit()
} catch (error) {
console.error('Apple Pay Failed', error)
setExpressPaymentError(error)
onClose()
}
}
Expand Down
24 changes: 11 additions & 13 deletions assets/js/blocks.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
const { registerPaymentMethod } = window.wc.wcBlocksRegistry
// const { registerExpressPaymentMethod } = window.wc.wcBlocksRegistry
const { createElement } = window.wp.element
const { registerExpressPaymentMethod } = window.wc.wcBlocksRegistry
const { cardTitle, savedCardsEnabled } = window.shift4Config

import React from 'react'
import Shift4PaymentForm from './blocks-form'
// import Shift4ApplePay from './blocks-apple-pay'
import Shift4ApplePay from './blocks-apple-pay'

// Register Shift4 Apple Pay.
// registerExpressPaymentMethod({
// name: 'shift4_applepay',
// label: 'Apple Pay (Shift4)',
// edit: <p>Apple Pay (Shift4)</p>,
// content: <Shift4ApplePay />,
// canMakePayment: () => {
// return !!(window.ApplePaySession && window.ApplePaySession.canMakePayments())
// }
// })
registerExpressPaymentMethod({
name: 'shift4_applepay',
label: 'Apple Pay (Shift4)',
edit: <p>Apple Pay (Shift4)</p>,
content: <Shift4ApplePay />,
canMakePayment: () => {
return !!(window.ApplePaySession && window.ApplePaySession.canMakePayments())
}
})

// Register Shift4 Credit Card.
registerPaymentMethod({
Expand Down
7 changes: 7 additions & 0 deletions assets/js/shift4.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ function initShift4(blockOptions) {
})
} catch (error) {
errorCallback(error)
throw error
}
}

Expand Down Expand Up @@ -258,9 +259,15 @@ function initShift4(blockOptions) {
try {
const result = await paymentRequest.show()
const applePayToken = result.details.token.paymentData
if (!applePayToken) {
console.warn('Apple Pay Failed')
console.log('Apple Pay result: ', result)
throw new Error('Apple Pay Failed.')
}
return applePayToken
} catch (error) {
errorCallback(error)
throw error
}
}

Expand Down
2 changes: 1 addition & 1 deletion build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.