Skip to content

Commit

Permalink
fix(checkout session): correct query param, change type, add coverage…
Browse files Browse the repository at this point in the history
… PE-5790
  • Loading branch information
fedellen committed Mar 11, 2024
1 parent f5bbf4b commit 4fc6115
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 33 deletions.
18 changes: 11 additions & 7 deletions src/common/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -125,12 +125,14 @@ export class TurboUnauthenticatedPaymentService
uiMode = 'hosted',
}: TurboCheckoutSessionParams,
headers?: TurboSignedRequestHeaders,
) {
): Promise<TurboCheckoutSessionResponse> {
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<TopUpRawResponse>({
Expand All @@ -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,
};
Expand Down Expand Up @@ -190,7 +194,7 @@ export class TurboAuthenticatedPaymentService
return this.httpService.get<TurboWincForFiatResponse>({
endpoint: `/price/${amount.type}/${
amount.amount
}${this.appendPromoCodesToQuery(promoCodes)}`,
}?${this.appendPromoCodesToQuery(promoCodes)}`,
headers: await this.signer.generateSignedRequestHeaders(),
});
}
Expand Down
10 changes: 8 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TurboPriceResponse, 'adjustments'>;
Expand Down
44 changes: 32 additions & 12 deletions tests/turbo.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
Expand Down
44 changes: 32 additions & 12 deletions tests/turbo.web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
Expand Down

0 comments on commit 4fc6115

Please sign in to comment.