/
transactions.test.ts
167 lines (135 loc) · 4.46 KB
/
transactions.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { PaddleSDK } from '../sdk';
import {
DEFAULT_ERROR,
EXPECTED_BODY,
VENDOR_API_KEY,
VENDOR_ID,
} from '../../utils/constants';
import nock, { SERVER } from '../../utils/nock';
describe('transactions methods', () => {
let instance: PaddleSDK;
// https://developer.paddle.com/api-reference/b3A6MzA3NDQ3MjQ-list-transactions#request-parameters
const RESPONSE = {
success: true,
response: [
{
order_id: '1042907-384786',
checkout_id: '4795118-chre895f5cfaf61-4d7dafa9df',
amount: '5.00',
currency: 'USD',
status: 'completed',
created_at: '2017-01-22 00:38:43',
passthrough: null,
product_id: 12345,
is_subscription: true,
is_one_off: false,
subscription: {
subscription_id: 123456,
status: 'active',
},
user: {
user_id: 29777,
email: 'example@paddle.com',
marketing_consent: true,
},
receipt_url:
'https://my.paddle.com/receipt/1042907-384786/4795118-chre895f5cfaf61-4d7dafa9df',
},
{
order_id: '1042907-384785',
checkout_id: '4795118-chre895f5cfaf61-4d7dafa9df',
amount: '5.00',
currency: 'USD',
status: 'refunded',
created_at: '2016-12-07 12:25:09',
passthrough: null,
product_id: 12345,
is_subscription: true,
is_one_off: true,
subscription: {
subscription_id: 123456,
status: 'active',
},
user: {
user_id: 29777,
email: 'example@paddle.com',
marketing_consent: true,
},
receipt_url:
'https://my.paddle.com/receipt/1042907-384785/4795118-chre895f5cfaf61-4d7dafa9df',
},
],
};
beforeEach(() => {
instance = new PaddleSDK(VENDOR_ID, VENDOR_API_KEY, '', {
server: SERVER,
});
});
describe('getUserTransactions', () => {
const path = '/user/123/transactions';
test('resolves on successful request', async () => {
const scope = nock().post(path, EXPECTED_BODY).reply(200, RESPONSE);
const response = await instance.getUserTransactions(123);
expect(response).toEqual(RESPONSE.response);
expect(scope.isDone()).toBeTruthy();
});
test('resolves on paged request', async () => {
const scope = nock()
.post(path, Object.assign({ page: 2 }, EXPECTED_BODY))
.reply(200, RESPONSE);
const response = await instance.getUserTransactions(123, 2);
expect(response).toEqual(RESPONSE.response);
expect(scope.isDone()).toBeTruthy();
});
test('rejects on error response', async () => {
const scope = nock().post(path, EXPECTED_BODY).reply(400, DEFAULT_ERROR);
await expect(instance.getUserTransactions(123)).rejects.toThrow(
'Request failed with status code 400'
);
expect(scope.isDone()).toBeTruthy();
});
test('rejects on 200 response with error', async () => {
const scope = nock().post(path, EXPECTED_BODY).reply(200, DEFAULT_ERROR);
await expect(instance.getUserTransactions(123)).rejects.toThrow(
'Request https://test.paddle.com/user/123/transactions returned an error!'
);
expect(scope.isDone()).toBeTruthy();
});
});
describe('getSubscriptionTransactions', () => {
const path = '/subscription/123/transactions';
test('resolves on successful request', async () => {
const scope = nock().post(path, EXPECTED_BODY).reply(200, RESPONSE);
const response = await instance.getSubscriptionTransactions(123);
expect(response).toEqual(RESPONSE.response);
expect(scope.isDone()).toBeTruthy();
});
});
describe('getOrderTransactions', () => {
const path = '/order/123/transactions';
test('resolves on successful request', async () => {
const scope = nock().post(path, EXPECTED_BODY).reply(200, RESPONSE);
const response = await instance.getOrderTransactions(123);
expect(response).toEqual(RESPONSE.response);
expect(scope.isDone()).toBeTruthy();
});
});
describe('getCheckoutTransactions', () => {
const path = '/checkout/123/transactions';
test('resolves on successful request', async () => {
const scope = nock().post(path, EXPECTED_BODY).reply(200, RESPONSE);
const response = await instance.getCheckoutTransactions('123');
expect(response).toEqual(RESPONSE.response);
expect(scope.isDone()).toBeTruthy();
});
});
describe('getProductTransactions', () => {
const path = '/product/123/transactions';
test('resolves on successful request', async () => {
const scope = nock().post(path, EXPECTED_BODY).reply(200, RESPONSE);
const response = await instance.getProductTransactions(123);
expect(response).toEqual(RESPONSE.response);
expect(scope.isDone()).toBeTruthy();
});
});
});