-
Notifications
You must be signed in to change notification settings - Fork 72
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
Cart and session restore with payment gateways #2231
Draft
mikekeehnen
wants to merge
6
commits into
canary
Choose a base branch
from
feature/GCOM-1350
base: canary
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+352
−14
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e437f5a
[GCOM-1350] feature: Added mollie revive cart
mikekeehnen 610cde8
[GCOM-1350] feature: Untested CartRevive for Multisafepay
mikekeehnen e49d668
[GCOM-1350] feature: Untested CartRevive for Multisafepay
mikekeehnen 25025ee
[GCOM-1350] feature: MSP cart revive
mikekeehnen da4d912
Fix yarn.lock
mikekeehnen d27c91c
[GCOM-1350] feature: PayPal cart revive functionality
mikekeehnen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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
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
65 changes: 65 additions & 0 deletions
65
packages/magento-payment-multisafepay/hooks/useMSPCartRevive.ts
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,65 @@ | ||
import { useApolloClient, useMutation } from '@graphcommerce/graphql' | ||
import { useAssignCurrentCartId, useCartQuery, useCurrentCartId } from '@graphcommerce/magento-cart' | ||
import { BillingPageDocument } from '@graphcommerce/magento-cart-checkout' | ||
import { CustomerTokenDocument } from '@graphcommerce/magento-customer' | ||
import { useEffect } from 'react' | ||
import { MSPPaymentHandlerDocument } from '../components/MSPPaymentHandler/MSPPaymentHandler.gql' | ||
import { useMSPCartLock } from './useMSPCartLock' | ||
|
||
export function useMSPReviveCart() { | ||
const [{ cart_id, method, customer_token }, , unlock] = useMSPCartLock() | ||
const billingPage = useCartQuery(BillingPageDocument, { fetchPolicy: 'cache-only' }) | ||
const client = useApolloClient() | ||
const { currentCartId } = useCurrentCartId() | ||
const assignCurrentCartId = useAssignCurrentCartId() | ||
const [restore, { error, loading }] = useMutation(MSPPaymentHandlerDocument) | ||
|
||
const reviveNow = | ||
cart_id && | ||
!billingPage.data?.cart?.id && | ||
method?.startsWith('multisafepay_') && | ||
currentCartId !== cart_id | ||
|
||
useEffect(() => { | ||
if (!reviveNow) return // eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
;(async () => { | ||
// In GraphCommerce 8 the user would automatically be presented with a restore session dialog | ||
if (customer_token) { | ||
client.cache.writeQuery({ | ||
query: CustomerTokenDocument, | ||
broadcast: true, | ||
data: { | ||
customerToken: { | ||
__typename: 'CustomerToken', | ||
token: customer_token, | ||
createdAt: new Date().toUTCString(), | ||
valid: true, | ||
}, | ||
}, | ||
}) | ||
} else { | ||
client.cache.evict({ fieldName: 'customerToken' }) | ||
} | ||
|
||
const restoredId = (await restore({ variables: { cartId: cart_id } })).data?.getPaymentMeta | ||
|
||
if (restoredId) assignCurrentCartId(restoredId) | ||
|
||
await unlock({ customer_token: null, order_number: null, success: null }) | ||
})() | ||
}, [ | ||
assignCurrentCartId, | ||
billingPage, | ||
cart_id, | ||
client.cache, | ||
currentCartId, | ||
customer_token, | ||
error, | ||
restore, | ||
reviveNow, | ||
unlock, | ||
]) | ||
|
||
if (billingPage.data) return !billingPage.loading | ||
return !loading | ||
} |
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
20 changes: 20 additions & 0 deletions
20
packages/magento-payment-multisafepay/plugins/UseMSPReviveCart.tsx
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,20 @@ | ||
import { WaitForPaymentQueries } from '@graphcommerce/magento-cart-payment-method' | ||
import type { PluginProps } from '@graphcommerce/next-config' | ||
import { ComponentProps } from 'react' | ||
import { useMSPReviveCart } from '../hooks/useMSPCartRevive' | ||
|
||
export const component = 'WaitForPaymentQueries' | ||
export const exported = '@graphcommerce/magento-cart-payment-method' | ||
|
||
function UseMSPReviveCart(props: PluginProps<ComponentProps<typeof WaitForPaymentQueries>>) { | ||
const { Prev, waitFor: w, ...rest } = props | ||
const reviving = useMSPReviveCart() | ||
|
||
let waitFor: typeof w | ||
|
||
if (w) waitFor = [...(Array.isArray(w) ? w : [w]), reviving] | ||
else waitFor = reviving | ||
|
||
return <Prev {...rest} waitFor={waitFor} /> | ||
} | ||
export const Plugin = UseMSPReviveCart |
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
15 changes: 15 additions & 0 deletions
15
packages/magento-payment-paypal/graphql/UsePayPalCartRevive.graphql
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,15 @@ | ||
mutation UsePayPalCartRevive($cartId: String!, $paymentMethod: PaymentMethodInput!) { | ||
setPaymentMethodOnCart(input: { cart_id: $cartId, payment_method: $paymentMethod }) { | ||
cart { | ||
__typename | ||
id | ||
...BillingAddress | ||
...ShippingAddress | ||
...Coupon | ||
...PaymentMethodContext | ||
...OrderSuccesPage | ||
...PaymentMethodUpdated | ||
} | ||
} | ||
} |
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
77 changes: 77 additions & 0 deletions
77
packages/magento-payment-paypal/hooks/usePayPalCartRevive.ts
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,77 @@ | ||
import { useApolloClient, useMutation } from '@graphcommerce/graphql' | ||
import { useCartQuery } from '@graphcommerce/magento-cart' | ||
import { BillingPageDocument } from '@graphcommerce/magento-cart-checkout' | ||
import { CustomerTokenDocument } from '@graphcommerce/magento-customer' | ||
import { useEffect } from 'react' | ||
import { PayPalPaymentHandlerDocument } from '../components/PayPalPaymentHandler/PayPalPaymentHandler.gql' | ||
import { usePayPalCartLock } from './usePayPalCartLock' | ||
import { UsePayPalCartReviveDocument } from '../graphql/UsePayPalCartRevive.gql' | ||
|
||
type UsePayPalCartReviveProps = { | ||
code: string | null | ||
} | ||
|
||
export function usePayPalCartRevive(props: UsePayPalCartReviveProps) { | ||
const { code } = props | ||
const [lockStatus] = usePayPalCartLock() | ||
|
||
const { token, PayerID, customerToken, method, cart_id: cartId } = lockStatus | ||
const [placeOrder, result] = useMutation(UsePayPalCartReviveDocument) | ||
|
||
const billingPage = useCartQuery(BillingPageDocument, { fetchPolicy: 'cache-only' }) | ||
const client = useApolloClient() | ||
|
||
const reviveNow = | ||
cartId && | ||
!billingPage.data?.cart?.id && | ||
method?.startsWith('paypal_') && | ||
PayerID && | ||
token && | ||
code | ||
|
||
useEffect(() => { | ||
if (!reviveNow) return // eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
;(async () => { | ||
// In GraphCommerce 8 the user would automatically be presented with a restore session dialog | ||
if (customerToken) { | ||
client.cache.writeQuery({ | ||
query: CustomerTokenDocument, | ||
broadcast: true, | ||
data: { | ||
customerToken: { | ||
__typename: 'CustomerToken', | ||
token: customerToken, | ||
createdAt: new Date().toUTCString(), | ||
valid: true, | ||
}, | ||
}, | ||
}) | ||
} else { | ||
client.cache.evict({ fieldName: 'customerToken' }) | ||
} | ||
|
||
const cart = ( | ||
await placeOrder({ | ||
variables: { | ||
cartId, | ||
paymentMethod: { code, paypal_express: { token, payer_id: PayerID } }, | ||
}, | ||
}) | ||
).data?.setPaymentMethodOnCart?.cart | ||
|
||
if (!cart) { | ||
console.log('Could not revive cart') | ||
return | ||
} | ||
client.cache.writeQuery({ | ||
query: BillingPageDocument, | ||
variables: { cartId }, | ||
data: { cart }, | ||
broadcast: true, | ||
}) | ||
})() | ||
}, [PayerID, cartId, client.cache, code, customerToken, placeOrder, reviveNow, token]) | ||
|
||
if (billingPage.data) return !billingPage.loading | ||
return !result.loading | ||
} |
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
21 changes: 21 additions & 0 deletions
21
packages/magento-payment-paypal/plugins/UsePayPalReviveCart.tsx
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,21 @@ | ||
import { WaitForPaymentQueries } from '@graphcommerce/magento-cart-payment-method' | ||
import type { PluginProps } from '@graphcommerce/next-config' | ||
import { ComponentProps } from 'react' | ||
import { usePayPalCartRevive } from '../hooks/usePayPalCartRevive' | ||
|
||
export const component = 'WaitForPaymentQueries' | ||
export const exported = '@graphcommerce/magento-cart-payment-method' | ||
|
||
function UsePayPalReviveCart(props: PluginProps<ComponentProps<typeof WaitForPaymentQueries>>) { | ||
const { Prev, waitFor: w, ...rest } = props | ||
|
||
const reviving = usePayPalCartRevive({ code: 'paypal_express' }) | ||
|
||
let waitFor: typeof w | ||
|
||
if (w) waitFor = [...(Array.isArray(w) ? w : [w]), reviving] | ||
else waitFor = reviving | ||
|
||
return <Prev {...rest} waitFor={waitFor} /> | ||
} | ||
export const Plugin = UsePayPalReviveCart |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need this for daxtrio