Skip to content

Commit 1a25fb8

Browse files
committed
feat(payment): INT-1759 Using same order reference ID when an error occurs
1 parent 48cd426 commit 1a25fb8

File tree

4 files changed

+61
-40
lines changed

4 files changed

+61
-40
lines changed

src/order/order.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default interface Order {
1414
customerId: number;
1515
customerMessage: string;
1616
discountAmount: number;
17+
handlingCostTotal: number;
1718
hasDigitalItems: boolean;
1819
isComplete: boolean;
1920
isDownloadable: boolean;
@@ -22,13 +23,12 @@ export default interface Order {
2223
orderAmount: number;
2324
orderAmountAsInteger: number;
2425
orderId: number;
26+
payments?: OrderPayments;
2527
shippingCostTotal: number;
2628
shippingCostBeforeDiscount: number;
27-
handlingCostTotal: number;
29+
status: string;
2830
taxes: Tax[];
2931
taxTotal: number;
30-
payments?: OrderPayments;
31-
status: string;
3232
}
3333

3434
export type OrderPayments = Array<GatewayOrderPayment | GiftCertificateOrderPayment>;

src/order/orders.mock.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ export function getOrder(): Order {
2222
customerCanBeCreated: true,
2323
customerId: 0,
2424
discountAmount: 10,
25+
handlingCostTotal: 8,
2526
hasDigitalItems: false,
2627
isComplete: true,
27-
status: 'ORDER_STATUS_AWAITING_FULFILLMENT',
2828
isDownloadable: false,
2929
isTaxIncluded: false,
3030
lineItems: {
@@ -37,23 +37,23 @@ export function getOrder(): Order {
3737
],
3838
customItems: [],
3939
},
40-
taxes: [
41-
{
42-
name: 'Tax',
43-
amount: 3,
44-
},
45-
],
46-
taxTotal: 3,
47-
shippingCostTotal: 15,
48-
shippingCostBeforeDiscount: 20,
49-
handlingCostTotal: 8,
5040
orderAmount: 190,
5141
orderAmountAsInteger: 19000,
5242
orderId: 295,
5343
payments: [
5444
getGatewayOrderPayment(),
5545
getGiftCertificateOrderPayment(),
5646
],
47+
shippingCostTotal: 15,
48+
shippingCostBeforeDiscount: 20,
49+
status: 'ORDER_STATUS_AWAITING_FULFILLMENT',
50+
taxes: [
51+
{
52+
name: 'Tax',
53+
amount: 3,
54+
},
55+
],
56+
taxTotal: 3,
5757
};
5858
}
5959

src/payment/strategies/amazon-pay/amazon-pay-payment-strategy.spec.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ describe('AmazonPayPaymentStrategy', () => {
7676
walletSpy(options);
7777

7878
options.onReady(orderReference);
79+
options.onPaymentSelect(orderReference);
7980
}
8081

8182
bind(id: string) {
@@ -469,26 +470,10 @@ describe('AmazonPayPaymentStrategy', () => {
469470
});
470471

471472
describe('When 3ds is enabled', () => {
473+
const amazon3ds = getAmazonPay();
474+
const payload = getOrderRequestBody();
472475
const paymentMethodsState = {
473-
data: [{
474-
id: 'amazon',
475-
logoUrl: '',
476-
method: 'widget',
477-
supportedCards: [],
478-
config: {
479-
displayName: 'AmazonPay',
480-
is3dsEnabled: true,
481-
merchantId: '0c173620-beb6-4421-99ef-03dc71a60685',
482-
testMode: false,
483-
},
484-
type: 'PAYMENT_TYPE_API',
485-
initializationData: {
486-
clientId: '087eccf4-7f68-4384-b0a9-5f2fd6b0d344',
487-
region: 'US',
488-
redirectUrl: '/remote-checkout/amazon/redirect',
489-
tokenPrefix: 'ABCD|',
490-
},
491-
}],
476+
data: [amazon3ds],
492477
meta: {
493478
geoCountryCode: 'AU',
494479
deviceSessionId: 'a37230e9a8e4ea2d7765e2f3e19f7b1d',
@@ -497,11 +482,11 @@ describe('AmazonPayPaymentStrategy', () => {
497482
errors: {},
498483
statuses: {},
499484
};
500-
const payload = getOrderRequestBody();
501485
let options: PaymentInitializeOptions;
502486
let store3ds: CheckoutStore;
503487
let strategy3ds: AmazonPayPaymentStrategy;
504488
let amazonConfirmationFlow: AmazonPayConfirmationFlow;
489+
amazon3ds.config.is3dsEnabled = true;
505490

506491
beforeEach(async () => {
507492
options = { methodId: paymentMethod.id };
@@ -522,13 +507,36 @@ describe('AmazonPayPaymentStrategy', () => {
522507
error: jest.fn(),
523508
};
524509

525-
jest.spyOn(store3ds, 'dispatch').mockReturnValue(Promise.resolve());
526-
jest.spyOn(store3ds.getState().remoteCheckout, 'getCheckout')
527-
.mockReturnValue({ referenceId: 'referenceId' });
528510
await strategy3ds.initialize({ methodId: paymentMethod.id, amazon: { container: 'wallet' } });
529511
});
530512

531513
it('redirects to confirmation flow success when support 3ds', async () => {
514+
515+
const remoteCheckout = {
516+
referenceId: 'referenceId',
517+
shipping: {},
518+
};
519+
520+
jest.spyOn(store3ds, 'getState');
521+
522+
jest.spyOn(store3ds.getState().remoteCheckout, 'getCheckout')
523+
.mockReturnValue(remoteCheckout);
524+
525+
if (hostWindow.OffAmazonPayments) {
526+
hostWindow.OffAmazonPayments.initConfirmationFlow = jest.fn((_sellerId, _referenceId, callback) => {
527+
callback(amazonConfirmationFlow);
528+
});
529+
530+
strategy3ds.execute(payload, options);
531+
532+
await new Promise(resolve => process.nextTick(resolve));
533+
534+
expect(hostWindow.OffAmazonPayments.initConfirmationFlow).toHaveBeenCalled();
535+
}
536+
537+
});
538+
539+
it('redirects to confirmation flow success when support 3ds and extract OrderReferenceId from InitializationData', async () => {
532540
if (hostWindow.OffAmazonPayments) {
533541
hostWindow.OffAmazonPayments.initConfirmationFlow = jest.fn((_sellerId, _referenceId, callback) => {
534542
callback(amazonConfirmationFlow);

src/payment/strategies/amazon-pay/amazon-pay-payment-strategy.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default class AmazonPayPaymentStrategy implements PaymentStrategy {
3434
private _paymentMethod?: PaymentMethod;
3535
private _walletOptions?: AmazonPayPaymentInitializeOptions;
3636
private _window: AmazonPayWindow;
37+
private _isPaymentMethodSelected: boolean;
3738

3839
constructor(
3940
private _store: CheckoutStore,
@@ -43,6 +44,7 @@ export default class AmazonPayPaymentStrategy implements PaymentStrategy {
4344
private _scriptLoader: AmazonPayScriptLoader
4445
) {
4546
this._window = window;
47+
this._isPaymentMethodSelected = false;
4648
}
4749

4850
initialize(options: PaymentInitializeOptions): Promise<InternalCheckoutSelectors> {
@@ -92,6 +94,10 @@ export default class AmazonPayPaymentStrategy implements PaymentStrategy {
9294
throw new InvalidArgumentError('Unable to proceed because "payload.payment.methodId" argument is not provided.');
9395
}
9496

97+
if (!this._isPaymentMethodSelected) {
98+
throw new MissingDataError(MissingDataErrorType.MissingPaymentMethod);
99+
}
100+
95101
const { payment: { paymentData, ...paymentPayload }, useStoreCredit = false } = payload;
96102

97103
if (options && this._paymentMethod && this._paymentMethod.config.is3dsEnabled) {
@@ -138,10 +144,14 @@ export default class AmazonPayPaymentStrategy implements PaymentStrategy {
138144
return amazon ? amazon.referenceId : undefined;
139145
}
140146

147+
private _getOrderReferenceIdFromInitializationData(): string | undefined {
148+
return this._paymentMethod ? this._paymentMethod.initializationData.orderReferenceId : undefined;
149+
}
150+
141151
private _createWallet(options: AmazonPayPaymentInitializeOptions): Promise<AmazonPayWallet> {
142152
return new Promise((resolve, reject) => {
143153
const { container, onError = noop, onPaymentSelect = noop, onReady = noop } = options;
144-
const referenceId = this._getOrderReferenceId();
154+
const referenceId = this._getOrderReferenceId() || this._getOrderReferenceIdFromInitializationData();
145155
const merchantId = this._getMerchantId();
146156

147157
if (!document.getElementById(container)) {
@@ -167,7 +177,10 @@ export default class AmazonPayPaymentStrategy implements PaymentStrategy {
167177
},
168178
onPaymentSelect: orderReference => {
169179
this._synchronizeBillingAddress()
170-
.then(() => onPaymentSelect(orderReference))
180+
.then(() => {
181+
this._isPaymentMethodSelected = true;
182+
onPaymentSelect(orderReference);
183+
})
171184
.catch(onError);
172185
},
173186
onReady: orderReference => {
@@ -176,7 +189,7 @@ export default class AmazonPayPaymentStrategy implements PaymentStrategy {
176189
},
177190
};
178191

179-
if (!walletOptions.amazonOrderReferenceId) {
192+
if (!this._getOrderReferenceId()) {
180193
walletOptions.onReady = orderReference => {
181194
this._updateOrderReference(orderReference)
182195
.then(() => {

0 commit comments

Comments
 (0)