From 4fc6115ebf74812cd6cf926a525457569d2153fe Mon Sep 17 00:00:00 2001 From: Derek Sonnenberg Date: Mon, 11 Mar 2024 10:16:35 -0500 Subject: [PATCH] fix(checkout session): correct query param, change type, add coverage PE-5790 --- src/common/payment.ts | 18 +++++++++------- src/types.ts | 10 +++++++-- tests/turbo.node.test.ts | 44 +++++++++++++++++++++++++++++----------- tests/turbo.web.test.ts | 44 +++++++++++++++++++++++++++++----------- 4 files changed, 83 insertions(+), 33 deletions(-) diff --git a/src/common/payment.ts b/src/common/payment.ts index 72192f42..924396e8 100644 --- a/src/common/payment.ts +++ b/src/common/payment.ts @@ -114,7 +114,7 @@ export class TurboUnauthenticatedPaymentService protected appendPromoCodesToQuery(promoCodes: string[]): string { const promoCodesQuery = promoCodes.join(','); - return promoCodesQuery ? `?promoCode=${promoCodesQuery}` : ''; + return promoCodesQuery ? `promoCode=${promoCodesQuery}` : ''; } protected async getCheckout( @@ -125,12 +125,14 @@ export class TurboUnauthenticatedPaymentService uiMode = 'hosted', }: TurboCheckoutSessionParams, headers?: TurboSignedRequestHeaders, - ) { + ): Promise { const { amount: paymentAmount, type: currencyType } = amount; - const endpoint = `/top-up/checkout-session/${owner}/${currencyType}/${paymentAmount}${this.appendPromoCodesToQuery( - promoCodes, - )}&uiMode=${uiMode}`; + const endpoint = `/top-up/checkout-session/${owner}/${currencyType}/${paymentAmount}?uiMode=${uiMode}${ + promoCodes.length > 0 + ? `&${this.appendPromoCodesToQuery(promoCodes)}` + : '' + }`; const { adjustments, paymentSession, topUpQuote } = await this.httpService.get({ @@ -141,7 +143,9 @@ export class TurboUnauthenticatedPaymentService return { winc: topUpQuote.winstonCreditAmount, adjustments, - url: paymentSession.url, + url: paymentSession.url ?? undefined, + id: paymentSession.id, + client_secret: paymentSession.client_secret ?? undefined, paymentAmount: topUpQuote.paymentAmount, quotedPaymentAmount: topUpQuote.quotedPaymentAmount, }; @@ -190,7 +194,7 @@ export class TurboAuthenticatedPaymentService return this.httpService.get({ endpoint: `/price/${amount.type}/${ amount.amount - }${this.appendPromoCodesToQuery(promoCodes)}`, + }?${this.appendPromoCodesToQuery(promoCodes)}`, headers: await this.signer.generateSignedRequestHeaders(), }); } diff --git a/src/types.ts b/src/types.ts index d6f337eb..d85e6788 100644 --- a/src/types.ts +++ b/src/types.ts @@ -85,12 +85,18 @@ export type TopUpRawResponse = { quotedPaymentAmount: number; winstonCreditAmount: string; }; - paymentSession: { url: string }; + paymentSession: { + url: string | null; + id: string; + client_secret: string | null; + }; adjustments: Adjustment[]; }; export type TurboCheckoutSessionResponse = TurboWincForFiatResponse & { - url: string; + id: string; + client_secret?: string; + url?: string; }; export type TurboBalanceResponse = Omit; diff --git a/tests/turbo.node.test.ts b/tests/turbo.node.test.ts index a6a52346..87671eb2 100644 --- a/tests/turbo.node.test.ts +++ b/tests/turbo.node.test.ts @@ -183,28 +183,48 @@ describe('Node environment', () => { describe('createCheckoutSession()', () => { it('should properly get a checkout session', async () => { - const { adjustments, paymentAmount, quotedPaymentAmount, url } = - await turbo.createCheckoutSession({ - amount: USD(10), - owner: '43-character-stub-arweave-address-000000000', - }); + const { + adjustments, + paymentAmount, + quotedPaymentAmount, + url, + client_secret, + id, + winc, + } = await turbo.createCheckoutSession({ + amount: USD(10), + owner: '43-character-stub-arweave-address-000000000', + }); expect(adjustments).to.deep.equal([]); expect(paymentAmount).to.equal(1000); expect(quotedPaymentAmount).to.equal(1000); expect(url).to.be.a('string'); + expect(id).to.be.a('string'); + expect(client_secret).to.equal(undefined); + expect(winc).to.be.a('string'); }); it('should properly get a checkout session with a embedded ui mode', async () => { - const { adjustments, paymentAmount, quotedPaymentAmount, url } = - await turbo.createCheckoutSession({ - amount: USD(20), - owner: '43-character-stub-arweave-address-000000000', - uiMode: 'embedded', - }); + const { + adjustments, + paymentAmount, + quotedPaymentAmount, + url, + id, + client_secret, + winc, + } = await turbo.createCheckoutSession({ + amount: USD(20), + owner: '43-character-stub-arweave-address-000000000', + uiMode: 'embedded', + }); expect(adjustments).to.deep.equal([]); expect(paymentAmount).to.equal(2000); expect(quotedPaymentAmount).to.equal(2000); - expect(url).to.be.a('string'); + expect(url).to.equal(undefined); + expect(id).to.be.a('string'); + expect(client_secret).to.be.a('string'); + expect(winc).to.be.a('string'); }); }); }); diff --git a/tests/turbo.web.test.ts b/tests/turbo.web.test.ts index 2b55859a..f8fd3ba7 100644 --- a/tests/turbo.web.test.ts +++ b/tests/turbo.web.test.ts @@ -171,29 +171,49 @@ describe('Browser environment', () => { describe('createCheckoutSession()', () => { it('should properly get a checkout session', async () => { - const { adjustments, paymentAmount, quotedPaymentAmount, url } = - await turbo.createCheckoutSession({ - amount: USD(10), - owner: '43-character-stub-arweave-address-000000000', - }); + const { + adjustments, + paymentAmount, + quotedPaymentAmount, + url, + id, + client_secret, + winc, + } = await turbo.createCheckoutSession({ + amount: USD(10), + owner: '43-character-stub-arweave-address-000000000', + }); expect(adjustments).to.deep.equal([]); expect(paymentAmount).to.equal(1000); expect(quotedPaymentAmount).to.equal(1000); expect(url).to.be.a('string'); + expect(id).to.be.a('string'); + expect(client_secret).to.equal(undefined); + expect(winc).to.be.a('string'); }); }); it('should properly get a checkout session with a embedded ui mode', async () => { - const { adjustments, paymentAmount, quotedPaymentAmount, url } = - await turbo.createCheckoutSession({ - amount: USD(20), - owner: '43-character-stub-arweave-address-000000000', - uiMode: 'embedded', - }); + const { + adjustments, + paymentAmount, + quotedPaymentAmount, + url, + id, + client_secret, + winc, + } = await turbo.createCheckoutSession({ + amount: USD(20), + owner: '43-character-stub-arweave-address-000000000', + uiMode: 'embedded', + }); expect(adjustments).to.deep.equal([]); expect(paymentAmount).to.equal(2000); expect(quotedPaymentAmount).to.equal(2000); - expect(url).to.be.a('string'); + expect(url).to.equal(undefined); + expect(id).to.be.a('string'); + expect(client_secret).to.be.a('string'); + expect(winc).to.be.a('string'); }); }); });