Skip to content
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

fix: itemized totals & pending amount on tokenized cart #8916

Merged
merged 3 commits into from
Jun 6, 2024
Merged
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
4 changes: 4 additions & 0 deletions changelog/fix-pending-amount-missing-itemized-totals
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

fix: itemized totals & pending amount on tokenized cart
31 changes: 21 additions & 10 deletions client/tokenized-payment-request/transformers/wc-to-stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,37 @@ import { __ } from '@wordpress/i18n';
export const transformCartDataForDisplayItems = ( cartData ) => {
const displayItems = cartData.items.map( ( item ) => ( {
amount: parseInt( item.prices.price, 10 ),
// TODO: should we also add variation attributes?
timur27 marked this conversation as resolved.
Show resolved Hide resolved
label: [ item.name, item.quantity > 1 && ` (x${ item.quantity })` ]
label: [
item.name,
item.quantity > 1 && `(x${ item.quantity })`,
item.variation &&
item.variation
.map(
( variation ) =>
`${ variation.attribute }: ${ variation.value }`
)
.join( ', ' ),
]
.filter( Boolean )
.join( '' ),
pending: true,
.join( ' ' ),
} ) );

if ( cartData.totals.total_tax ) {
const taxAmount = parseInt( cartData.totals.total_tax || '0', 10 );
if ( taxAmount ) {
displayItems.push( {
amount: parseInt( cartData.totals.total_tax, 10 ),
timur27 marked this conversation as resolved.
Show resolved Hide resolved
amount: taxAmount,
label: __( 'Tax', 'woocommerce-payments' ),
pending: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed pending to allow for the itemized amounts to be displayed - I checked the previous implementation, and they also weren't using it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity: do we happen to know what the business value of the pending property is? Which state does it represent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to be used by browsers to display things in different ways.
Stripe says:

If you might change this amount later (for example, after you have calcluated shipping costs), set this to true

In our case, it seems that pending: true will never be needed because there's always going to be an address selected on the Google Pay/Apple Pay modal. And if an address is selected, we'll also have the shipping options available from the Store API.

} );
}

if ( cartData.totals.total_shipping ) {
const shippingAmount = parseInt(
cartData.totals.total_shipping || '0',
10
);
if ( shippingAmount ) {
displayItems.push( {
amount: parseInt( cartData.totals.total_shipping, 10 ),
amount: shippingAmount,
label: __( 'Shipping', 'woocommerce-payments' ),
pending: true,
} );
}

Expand Down
Loading