This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Fix wrong keys being sent in canMakePayment
and customer data showing in the Checkout block in the editor
#7434
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
14b6810
Construct args for canMakePayment with correct keys
opr ca96edc
When the CheckoutEventsContext mounts, initialize payment store
opr fef7f02
Destructure useSelect correctly
opr b841eec
Dispatch __internalInitializePaymentStore in selector tests
opr 9774fcb
Update selector name to __internalUpdateAvailablePaymentMethods
opr 5ebe7fb
Remove check for editor when registering checkout store
opr 0f29710
Add check for when express payment methods have updated too
opr fb0f5fd
Ensure billingAddress key exists in canMakePayment arg
opr 6676231
Use editor context to know if we're in editor
opr 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,21 +7,34 @@ import { | |
} from '@woocommerce/type-defs/payments'; | ||
import { CURRENT_USER_IS_ADMIN, getSetting } from '@woocommerce/settings'; | ||
import { dispatch, select } from '@wordpress/data'; | ||
import { deriveSelectedShippingRates } from '@woocommerce/base-utils'; | ||
import { | ||
deriveSelectedShippingRates, | ||
emptyHiddenAddressFields, | ||
} from '@woocommerce/base-utils'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
|
||
import { | ||
getExpressPaymentMethods, | ||
getPaymentMethods, | ||
} from '@woocommerce/blocks-registry'; | ||
import { previewCart } from '@woocommerce/resource-previews'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { STORE_KEY as CART_STORE_KEY } from '../cart/constants'; | ||
import { STORE_KEY as PAYMENT_STORE_KEY } from './constants'; | ||
import { noticeContexts } from '../../base/context/event-emit'; | ||
import { | ||
EMPTY_CART_ERRORS, | ||
EMPTY_CART_ITEM_ERRORS, | ||
EMPTY_EXTENSIONS, | ||
} from '../../data/constants'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to import from file otherwise it will be a circular dependency. |
||
import { | ||
defaultBillingAddress, | ||
defaultShippingAddress, | ||
} from '../../base/context/providers/cart-checkout/customer/constants'; | ||
|
||
export const checkPaymentMethodsCanPay = async ( express = false ) => { | ||
const isEditor = !! select( 'core/editor' ); | ||
|
@@ -46,19 +59,95 @@ export const checkPaymentMethodsCanPay = async ( express = false ) => { | |
const noticeContext = express | ||
? noticeContexts.EXPRESS_PAYMENTS | ||
: noticeContexts.PAYMENTS; | ||
const cart = select( CART_STORE_KEY ).getCartData(); | ||
const selectedShippingMethods = deriveSelectedShippingRates( | ||
cart.shippingRates | ||
); | ||
const canPayArgument = { | ||
cart, | ||
cartTotals: cart.totals, | ||
cartNeedsShipping: cart.needsShipping, | ||
billingData: cart.billingAddress, | ||
shippingAddress: cart.shippingAddress, | ||
selectedShippingMethods, | ||
paymentRequirements: cart.paymentRequirements, | ||
}; | ||
|
||
let cartForCanPayArgument: Record< string, unknown > = {}; | ||
let canPayArgument: Record< string, unknown > = {}; | ||
|
||
if ( ! isEditor ) { | ||
const store = select( CART_STORE_KEY ); | ||
const cart = store.getCartData(); | ||
const cartErrors = store.getCartErrors(); | ||
const cartTotals = store.getCartTotals(); | ||
const cartIsLoading = ! store.hasFinishedResolution( 'getCartData' ); | ||
const isLoadingRates = store.isCustomerDataUpdating(); | ||
const selectedShippingMethods = deriveSelectedShippingRates( | ||
cart.shippingRates | ||
); | ||
|
||
cartForCanPayArgument = { | ||
cartCoupons: cart.coupons, | ||
cartItems: cart.items, | ||
crossSellsProducts: cart.crossSells, | ||
cartFees: cart.fees, | ||
cartItemsCount: cart.itemsCount, | ||
cartItemsWeight: cart.itemsWeight, | ||
cartNeedsPayment: cart.needsPayment, | ||
cartNeedsShipping: cart.needsShipping, | ||
cartItemErrors: cart.errors, | ||
cartTotals, | ||
cartIsLoading, | ||
cartErrors, | ||
billingData: emptyHiddenAddressFields( cart.billingAddress ), | ||
billingAddress: emptyHiddenAddressFields( cart.billingAddress ), | ||
shippingAddress: emptyHiddenAddressFields( cart.shippingAddress ), | ||
extensions: cart.extensions, | ||
shippingRates: cart.shippingRates, | ||
isLoadingRates, | ||
cartHasCalculatedShipping: cart.hasCalculatedShipping, | ||
paymentRequirements: cart.paymentRequirements, | ||
receiveCart: dispatch( CART_STORE_KEY ).receiveCart, | ||
}; | ||
|
||
canPayArgument = { | ||
cart: cartForCanPayArgument, | ||
cartTotals: cart.totals, | ||
cartNeedsShipping: cart.needsShipping, | ||
billingData: cart.billingAddress, | ||
billingAddress: cart.billingAddress, | ||
shippingAddress: cart.shippingAddress, | ||
selectedShippingMethods, | ||
paymentRequirements: cart.paymentRequirements, | ||
}; | ||
} else { | ||
cartForCanPayArgument = { | ||
cartCoupons: previewCart.coupons, | ||
cartItems: previewCart.items, | ||
crossSellsProducts: previewCart.cross_sells, | ||
cartFees: previewCart.fees, | ||
cartItemsCount: previewCart.items_count, | ||
cartItemsWeight: previewCart.items_weight, | ||
cartNeedsPayment: previewCart.needs_payment, | ||
cartNeedsShipping: previewCart.needs_shipping, | ||
cartItemErrors: EMPTY_CART_ITEM_ERRORS, | ||
cartTotals: previewCart.totals, | ||
cartIsLoading: false, | ||
cartErrors: EMPTY_CART_ERRORS, | ||
billingData: defaultBillingAddress, | ||
billingAddress: defaultBillingAddress, | ||
shippingAddress: defaultShippingAddress, | ||
extensions: EMPTY_EXTENSIONS, | ||
shippingRates: previewCart.shipping_rates, | ||
isLoadingRates: false, | ||
cartHasCalculatedShipping: previewCart.has_calculated_shipping, | ||
paymentRequirements: previewCart.paymentRequirements, | ||
receiveCart: | ||
typeof previewCart?.receiveCart === 'function' | ||
? previewCart.receiveCart | ||
: () => undefined, | ||
}; | ||
canPayArgument = { | ||
cart: cartForCanPayArgument, | ||
cartTotals: cartForCanPayArgument.totals, | ||
cartNeedsShipping: cartForCanPayArgument.needsShipping, | ||
billingData: cartForCanPayArgument.billingAddress, | ||
billingAddress: cartForCanPayArgument.billingAddress, | ||
shippingAddress: cartForCanPayArgument.shippingAddress, | ||
selectedShippingMethods: deriveSelectedShippingRates( | ||
cartForCanPayArgument.shippingRates | ||
), | ||
paymentRequirements: cartForCanPayArgument.paymentRequirements, | ||
}; | ||
} | ||
|
||
let paymentMethodsOrder; | ||
if ( express ) { | ||
|
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
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.
Given you can use hooks here, why not use
isEditor
fromuseEditorContext
?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.
Updated to use
useEditorContext
thanks @senadir