Skip to content

Commit

Permalink
feat: Support the One-off Charge API (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
GusRuss89 committed Apr 24, 2023
1 parent 70f93db commit 9b28977
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/__tests__/subscriptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,50 @@ describe('subscription methods', () => {
expect(scope.isDone()).toBeTruthy();
});
});

describe('createOneOffCharge', () => {
const path = `/subscription/${SUBSCRIPTION_ID}/charge`;
const expectedBody = {
...EXPECTED_BODY,
amount: 10,
charge_name: 'Charge 1',
};
// https://developer.paddle.com/api-reference/23cf86225523f-create-one-off-charge
const responseBody = {
success: true,
response: {
invoice_id: 1,
subscription_id: SUBSCRIPTION_ID,
amount: '10.00',
currency: 'USD',
payment_date: '2018-09-21',
receipt_url:
'https://my.paddle.com/receipt/1-1/3-chre8a53a2724c6-42781cb91a',
status: 'success',
},
};

test('resolves on successful request', async () => {
const scope = nock().post(path, expectedBody).reply(200, responseBody);

const response = await instance.createOneOffCharge(
SUBSCRIPTION_ID,
10,
'Charge 1'
);

expect(response).toEqual(responseBody.response);
expect(scope.isDone()).toBeTruthy();
});

test('rejects on error request', async () => {
const scope = nock().post(path, expectedBody).reply(400, DEFAULT_ERROR);

await expect(
instance.createOneOffCharge(SUBSCRIPTION_ID, 10, 'Charge 1')
).rejects.toThrow('Request failed with status code 400');

expect(scope.isDone()).toBeTruthy();
});
});
});
27 changes: 27 additions & 0 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import serialize from './serialize';
import {
CreateSubscriptionModifierBody,
CreateSubscriptionModifierResponse,
CreateOneOffChargeBody,
CreateOneOffChargeResponse,
GeneratePaylinkBody,
GeneratePaylinkResponse,
GetProductCouponsBody,
Expand Down Expand Up @@ -474,6 +476,31 @@ s * @example
});
}

/**
* Make an immediate one-off charge on top of an existing user subscription
*
* API documentation: https://developer.paddle.com/api-reference/23cf86225523f-create-one-off-charge
*
* @example
* const result = await client.createOneOffCharge(123, 10, 'description');
*/
createOneOffCharge(
subscriptionID: number,
amount: number,
chargeName: string
) {
const body = {
amount,
charge_name: chargeName.substring(0, 50),
};
return this._request<CreateOneOffChargeResponse, CreateOneOffChargeBody>(
`/subscription/${subscriptionID}/charge`,
{
body,
}
);
}

/**
* Get the used server URL. Some of the requests go to Checkout server, while most will go to Vendor server.
*/
Expand Down
16 changes: 16 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,22 @@ export interface CreateSubscriptionModifierResponse {
modifier_id: number;
}

export interface CreateOneOffChargeBody {
amount: number;
charge_name: string;
}

export interface CreateOneOffChargeResponse {
subscription_id: number;
invoice_id: number;
amount: string;
currency: string;
payment_date: string;
receipt_url: string;
order_id: string;
status: 'success' | 'pending';
}

export interface RescheduleSubscriptionPaymentBody {
date: string;
payment_id: number;
Expand Down

0 comments on commit 9b28977

Please sign in to comment.