payjp 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/built/resource.d.ts +3 -0
- package/built/resource.js +24 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,6 +26,13 @@ payjp.charges.create({
|
|
|
26
26
|
currency: 'jpy',
|
|
27
27
|
card: 'token_id_by_Checkout_or_payjp.js'
|
|
28
28
|
}).then(console.log).catch(console.error);
|
|
29
|
+
|
|
30
|
+
payjp.charges.create({
|
|
31
|
+
amount: 1000,
|
|
32
|
+
currency: 'jpy',
|
|
33
|
+
customer: 'cus_xxx',
|
|
34
|
+
metadata: { user_id: 123 }
|
|
35
|
+
}).then(console.log).catch(console.error);
|
|
29
36
|
```
|
|
30
37
|
|
|
31
38
|
Typescript
|
package/built/resource.d.ts
CHANGED
|
@@ -10,6 +10,9 @@ export default class Resource {
|
|
|
10
10
|
get apikey(): string;
|
|
11
11
|
private buildHeader;
|
|
12
12
|
private getCurrentDelay;
|
|
13
|
+
private isPlainObject;
|
|
14
|
+
private appendFormValue;
|
|
15
|
+
private serializeQuery;
|
|
13
16
|
protected request<I>(method: string, endpoint: string, query?: object, headers?: object): Promise<I>;
|
|
14
17
|
}
|
|
15
18
|
export {};
|
package/built/resource.js
CHANGED
|
@@ -26,6 +26,28 @@ class Resource {
|
|
|
26
26
|
const delay = Math.min(initialDelay * 2 ** retryCount, maxDelay);
|
|
27
27
|
return Math.ceil((delay / 2) * (1 + Math.random()));
|
|
28
28
|
}
|
|
29
|
+
isPlainObject(value) {
|
|
30
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
31
|
+
}
|
|
32
|
+
appendFormValue(params, key, value) {
|
|
33
|
+
if (value === undefined) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (this.isPlainObject(value)) {
|
|
37
|
+
for (const [nestedKey, nestedValue] of Object.entries(value)) {
|
|
38
|
+
this.appendFormValue(params, `${key}[${nestedKey}]`, nestedValue);
|
|
39
|
+
}
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
params.append(key, String(value));
|
|
43
|
+
}
|
|
44
|
+
serializeQuery(query) {
|
|
45
|
+
const params = new URLSearchParams();
|
|
46
|
+
for (const [key, value] of Object.entries(query)) {
|
|
47
|
+
this.appendFormValue(params, key, value);
|
|
48
|
+
}
|
|
49
|
+
return params;
|
|
50
|
+
}
|
|
29
51
|
request(method, endpoint, query = {}, headers = {}) {
|
|
30
52
|
const header = Object.assign(this.buildHeader(method), headers);
|
|
31
53
|
const doRequest = async () => {
|
|
@@ -37,7 +59,7 @@ class Resource {
|
|
|
37
59
|
// Set query parameters or request body
|
|
38
60
|
if (method === "GET" || method === "DELETE") {
|
|
39
61
|
// For GET and DELETE, add query parameters to URL
|
|
40
|
-
const params =
|
|
62
|
+
const params = this.serializeQuery(query);
|
|
41
63
|
const queryString = params.toString();
|
|
42
64
|
if (queryString) {
|
|
43
65
|
url = `${url}?${queryString}`;
|
|
@@ -45,7 +67,7 @@ class Resource {
|
|
|
45
67
|
}
|
|
46
68
|
else {
|
|
47
69
|
// For POST and PUT, send as request body
|
|
48
|
-
const body =
|
|
70
|
+
const body = this.serializeQuery(query);
|
|
49
71
|
fetchOptions.body = body.toString();
|
|
50
72
|
}
|
|
51
73
|
// Set timeout
|