payment-kit 1.13.251 → 1.13.253
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.
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import env from '@blocklet/sdk/lib/env';
|
|
2
|
+
import merge from 'lodash/merge';
|
|
2
3
|
import pick from 'lodash/pick';
|
|
3
4
|
import pWaitFor from 'p-wait-for';
|
|
4
5
|
import type Stripe from 'stripe';
|
|
@@ -14,6 +15,7 @@ import {
|
|
|
14
15
|
Subscription,
|
|
15
16
|
SubscriptionItem,
|
|
16
17
|
TEventExpanded,
|
|
18
|
+
TInvoiceItem,
|
|
17
19
|
} from '../../../store/models';
|
|
18
20
|
|
|
19
21
|
export async function handleStripeInvoicePaid(invoice: Invoice, event: TEventExpanded) {
|
|
@@ -37,6 +39,21 @@ export async function handleStripeInvoicePaid(invoice: Invoice, event: TEventExp
|
|
|
37
39
|
});
|
|
38
40
|
}
|
|
39
41
|
|
|
42
|
+
export function getStripeInvoicePeriod(invoice: any) {
|
|
43
|
+
const lineItem: TInvoiceItem = (invoice.lines.data || []).find((x: any) => !x.proration && x.type === 'subscription');
|
|
44
|
+
if (lineItem && lineItem.period) {
|
|
45
|
+
return {
|
|
46
|
+
period_start: lineItem.period.start,
|
|
47
|
+
period_end: lineItem.period.end,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
period_start: invoice.period_start,
|
|
53
|
+
period_end: invoice.period_end,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
40
57
|
export async function syncStripeInvoice(invoice: Invoice) {
|
|
41
58
|
if (!invoice.metadata?.stripe_id) {
|
|
42
59
|
return;
|
|
@@ -52,21 +69,24 @@ export async function syncStripeInvoice(invoice: Invoice) {
|
|
|
52
69
|
if (stripeInvoice) {
|
|
53
70
|
await invoice.update(
|
|
54
71
|
// @ts-ignore
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
72
|
+
merge(
|
|
73
|
+
pick(stripeInvoice, [
|
|
74
|
+
'amount_due',
|
|
75
|
+
'amount_paid',
|
|
76
|
+
'amount_remaining',
|
|
77
|
+
'last_finalization_error',
|
|
78
|
+
'paid_out_of_band',
|
|
79
|
+
'paid',
|
|
80
|
+
'status_transitions',
|
|
81
|
+
'status',
|
|
82
|
+
'subtotal_excluding_tax',
|
|
83
|
+
'subtotal',
|
|
84
|
+
'tax',
|
|
85
|
+
'total_discount_amounts',
|
|
86
|
+
'total',
|
|
87
|
+
]),
|
|
88
|
+
getStripeInvoicePeriod(stripeInvoice)
|
|
89
|
+
)
|
|
70
90
|
);
|
|
71
91
|
logger.info('stripe invoice synced', { locale: invoice.id, remote: stripeInvoice.id });
|
|
72
92
|
}
|
|
@@ -86,8 +86,9 @@ export class SubscriptionWillRenewEmailTemplate
|
|
|
86
86
|
locale === 'en' ? this.getWillRenewDuration(locale) : this.getWillRenewDuration(locale).split(' ').join('');
|
|
87
87
|
|
|
88
88
|
const upcomingInvoiceAmount = await getUpcomingInvoiceAmount(subscription.id);
|
|
89
|
-
const
|
|
90
|
-
paymentDetail
|
|
89
|
+
const amount: string = fromUnitToToken(+upcomingInvoiceAmount.amount, upcomingInvoiceAmount.currency?.decimal);
|
|
90
|
+
const paymentDetail: PaymentDetail = await getPaymentDetail(userDid, invoice, amount);
|
|
91
|
+
paymentDetail.price = +amount;
|
|
91
92
|
|
|
92
93
|
const { isPrePaid, interval } = await this.getPaymentCategory({
|
|
93
94
|
subscriptionId: subscription.id,
|
package/api/src/libs/payment.ts
CHANGED
|
@@ -174,7 +174,11 @@ export interface PaymentDetail {
|
|
|
174
174
|
price: number;
|
|
175
175
|
symbol: LiteralUnion<'ABT' | 'USD', string>;
|
|
176
176
|
}
|
|
177
|
-
export async function getPaymentDetail(
|
|
177
|
+
export async function getPaymentDetail(
|
|
178
|
+
userDid: string,
|
|
179
|
+
invoice: Invoice,
|
|
180
|
+
amount: string = '0'
|
|
181
|
+
): Promise<PaymentDetail> {
|
|
178
182
|
const defaultResult = {
|
|
179
183
|
balance: 0,
|
|
180
184
|
price: 0,
|
|
@@ -188,11 +192,9 @@ export async function getPaymentDetail(userDid: string, invoice: Invoice): Promi
|
|
|
188
192
|
Object.assign(defaultResult, { symbol: paymentCurrency.symbol });
|
|
189
193
|
|
|
190
194
|
const paymentIntent = await PaymentIntent.findByPk(invoice.payment_intent_id);
|
|
191
|
-
|
|
192
|
-
return defaultResult;
|
|
193
|
-
}
|
|
195
|
+
const inputAmount: string = paymentIntent ? paymentIntent.amount : amount;
|
|
194
196
|
|
|
195
|
-
const paymentMethod = await PaymentMethod.findByPk(
|
|
197
|
+
const paymentMethod = await PaymentMethod.findByPk(invoice.default_payment_method_id);
|
|
196
198
|
if (!paymentMethod) {
|
|
197
199
|
return defaultResult;
|
|
198
200
|
}
|
|
@@ -203,7 +205,7 @@ export async function getPaymentDetail(userDid: string, invoice: Invoice): Promi
|
|
|
203
205
|
paymentMethod,
|
|
204
206
|
paymentCurrency,
|
|
205
207
|
userDid,
|
|
206
|
-
amount:
|
|
208
|
+
amount: inputAmount,
|
|
207
209
|
});
|
|
208
210
|
|
|
209
211
|
// Do not have enough permission
|
|
@@ -212,18 +214,18 @@ export async function getPaymentDetail(userDid: string, invoice: Invoice): Promi
|
|
|
212
214
|
}
|
|
213
215
|
|
|
214
216
|
// Do not have enough token
|
|
215
|
-
const
|
|
217
|
+
const price: number = +fromUnitToToken(inputAmount, paymentCurrency.decimal);
|
|
216
218
|
if (!result.delegator) {
|
|
217
219
|
return {
|
|
218
220
|
balance: 0,
|
|
219
|
-
price
|
|
221
|
+
price,
|
|
220
222
|
symbol: paymentCurrency.symbol,
|
|
221
223
|
};
|
|
222
224
|
}
|
|
223
225
|
|
|
224
226
|
return {
|
|
225
227
|
balance: +fromUnitToToken(result.token.balance, paymentCurrency.decimal),
|
|
226
|
-
price
|
|
228
|
+
price,
|
|
227
229
|
symbol: paymentCurrency.symbol,
|
|
228
230
|
};
|
|
229
231
|
}
|
|
@@ -219,7 +219,7 @@ router.get('/:id', authPortal, async (req, res) => {
|
|
|
219
219
|
});
|
|
220
220
|
|
|
221
221
|
if (doc) {
|
|
222
|
-
if (doc.status !== 'paid'
|
|
222
|
+
if (doc.metadata?.stripe_id && (doc.status !== 'paid' || req.query.forceSync)) {
|
|
223
223
|
await syncStripeInvoice(doc);
|
|
224
224
|
}
|
|
225
225
|
if (doc.payment_intent_id) {
|
package/blocklet.yml
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "payment-kit",
|
|
3
|
-
"version": "1.13.
|
|
3
|
+
"version": "1.13.253",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"dev": "blocklet dev --open",
|
|
6
6
|
"eject": "vite eject",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"@arcblock/ux": "^2.9.77",
|
|
52
52
|
"@arcblock/validator": "^1.18.116",
|
|
53
53
|
"@blocklet/logger": "1.16.26",
|
|
54
|
-
"@blocklet/payment-react": "1.13.
|
|
54
|
+
"@blocklet/payment-react": "1.13.253",
|
|
55
55
|
"@blocklet/sdk": "1.16.26",
|
|
56
56
|
"@blocklet/ui-react": "^2.9.77",
|
|
57
57
|
"@blocklet/uploader": "^0.1.6",
|
|
@@ -116,7 +116,7 @@
|
|
|
116
116
|
"devDependencies": {
|
|
117
117
|
"@abtnode/types": "1.16.26",
|
|
118
118
|
"@arcblock/eslint-config-ts": "^0.3.0",
|
|
119
|
-
"@blocklet/payment-types": "1.13.
|
|
119
|
+
"@blocklet/payment-types": "1.13.253",
|
|
120
120
|
"@types/cookie-parser": "^1.4.7",
|
|
121
121
|
"@types/cors": "^2.8.17",
|
|
122
122
|
"@types/dotenv-flow": "^3.3.3",
|
|
@@ -155,5 +155,5 @@
|
|
|
155
155
|
"parser": "typescript"
|
|
156
156
|
}
|
|
157
157
|
},
|
|
158
|
-
"gitHead": "
|
|
158
|
+
"gitHead": "4ad023bae87cc6b0c7480e701e5ba51b7b66ca19"
|
|
159
159
|
}
|