Skip to content

Commit

Permalink
fix: tokenized cart support for special characters and local pickup (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
frosso authored Jul 19, 2024
1 parent ecb9976 commit ad90644
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 3 deletions.
5 changes: 5 additions & 0 deletions changelog/fix-tokenized-cart-special-chars
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fix
Comment: fix: tokenized cart support for special characters and local pickup


Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/**
* Internal dependencies
*/
import { transformPrice } from '../wc-to-stripe';
import {
transformPrice,
transformCartDataForShippingOptions,
} from '../wc-to-stripe';

global.wcpayPaymentRequestParams = {};
global.wcpayPaymentRequestParams.checkout = {};
Expand Down Expand Up @@ -51,4 +54,190 @@ describe( 'wc-to-stripe transformers', () => {
);
} );
} );

describe( 'transformCartDataForShippingOptions', () => {
it( 'transforms shipping rates', () => {
expect(
transformCartDataForShippingOptions( {
shipping_rates: [
{
package_id: 0,
name: 'Shipment 1',
destination: {},
items: [
{
key: 'aab3238922bcc25a6f606eb525ffdc56',
name: 'Beanie',
quantity: 1,
},
],
shipping_rates: [
{
rate_id: 'flat_rate:14',
name: 'CA Flat rate',
description: '',
delivery_time: '',
price: '1000',
taxes: '300',
instance_id: 14,
method_id: 'flat_rate',
meta_data: [
{
key: 'Items',
value: 'Beanie × 1',
},
],
selected: true,
currency_code: 'USD',
currency_symbol: '$',
currency_minor_unit: 2,
currency_decimal_separator: '.',
currency_thousand_separator: ',',
currency_prefix: '$',
currency_suffix: '',
},
{
rate_id: 'local_pickup:15',
name: 'Local pickup',
description: '',
delivery_time: '',
price: '350',
taxes: '105',
instance_id: 15,
method_id: 'local_pickup',
meta_data: [
{
key: 'Items',
value: 'Beanie × 1',
},
],
selected: false,
currency_code: 'USD',
currency_symbol: '$',
currency_minor_unit: 2,
currency_decimal_separator: '.',
currency_thousand_separator: ',',
currency_prefix: '$',
currency_suffix: '',
},
{
rate_id: 'free_shipping:13',
name: 'Free shipping',
description: '',
delivery_time: '',
price: '0',
taxes: '0',
instance_id: 13,
method_id: 'free_shipping',
meta_data: [
{
key: 'Items',
value: 'Beanie × 1',
},
],
selected: false,
currency_code: 'USD',
currency_symbol: '$',
currency_minor_unit: 2,
currency_decimal_separator: '.',
currency_thousand_separator: ',',
currency_prefix: '$',
currency_suffix: '',
},
],
},
],
} )
).toEqual( [
{
amount: 1000,
detail: '',
id: 'flat_rate:14',
label: 'CA Flat rate',
},
{
amount: 350,
detail: '',
id: 'local_pickup:15',
label: 'Local pickup',
},
{
amount: 0,
detail: '',
id: 'free_shipping:13',
label: 'Free shipping',
},
] );
} );

it( 'transforms shipping options for local pickup', () => {
expect(
transformCartDataForShippingOptions( {
shipping_rates: [
{
package_id: 0,
name: 'Shipment 1',
destination: {},
items: [
{
key: 'aab3238922bcc25a6f606eb525ffdc56',
name: 'Beanie',
quantity: 1,
},
],
shipping_rates: [
{
rate_id: 'pickup_location:1',
name:
'Local pickup – options coming from WooCommerce Blocks (Australian warehouse)',
description: '',
delivery_time: '',
price: '0',
taxes: '0',
instance_id: 0,
method_id: 'pickup_location',
meta_data: [
{
key: 'pickup_location',
value: 'Australian warehouse',
},
{
key: 'pickup_address',
value:
'42 Wallaby Way, Sydney New South Wales 200, Australia',
},
{
key: 'pickup_details',
value: 'Ask for P. Sherman',
},
{
key: 'Items',
value: 'Beanie × 1',
},
],
selected: false,
currency_code: 'USD',
currency_symbol: '$',
currency_minor_unit: 2,
currency_decimal_separator: '.',
currency_thousand_separator: ',',
currency_prefix: '$',
currency_suffix: '',
},
],
},
],
} )
).toEqual( [
{
amount: 0,
detail:
'42 Wallaby Way, Sydney New South Wales 200, Australia - Ask for P. Sherman',
id: 'pickup_location:1',
label:
'Local pickup – options coming from WooCommerce Blocks (Australian warehouse)',
},
] );
} );
} );
} );
15 changes: 13 additions & 2 deletions client/tokenized-payment-request/transformers/wc-to-stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';

/**
* Internal dependencies
Expand Down Expand Up @@ -94,7 +95,17 @@ export const transformCartDataForDisplayItems = ( cartData ) => {
export const transformCartDataForShippingOptions = ( cartData ) =>
cartData.shipping_rates[ 0 ].shipping_rates.map( ( rate ) => ( {
id: rate.rate_id,
label: rate.name,
label: decodeEntities( rate.name ),
amount: transformPrice( parseInt( rate.price, 10 ), rate ),
detail: '',
detail: [
rate.meta_data.find(
( metadata ) => metadata.key === 'pickup_address'
)?.value,
rate.meta_data.find(
( metadata ) => metadata.key === 'pickup_details'
)?.value,
]
.filter( Boolean )
.map( decodeEntities )
.join( ' - ' ),
} ) );

0 comments on commit ad90644

Please sign in to comment.