Just another awesome magic.
MIT License.
const paying = new Paying({
services: {
alipay: new AlipayService(),
'apple-iap': new AppleIAPService(),
},
});
const user = paying.user(userId);
const subscription = await user.findSubscription('premium');
// subscription.active;
// subscription.expiresAt;
const subscriptions = await user.getSubscriptions();
苹果订阅
const user = paying.user(userId);
// const result = await appleIAPService.resolveReceipt(receipt);
await user.submitReceipt('apple-iap', receipt);
// if (result.type === 'subscription') {
// // const subscription = await user.prepareSubscription({
// // });
// // await subscription.submit();
// // await user.submitSubscription({
// // name: 'premium',
// // service: 'apple-iap',
// // extend(origin) {
// // return addMonths(origin, 1);
// // },
// // });
// } else if (result.type === 'payment') {
// // const payment = await user.preparePayment({
// // product: result.product,
// // amount: result.amount,
// // });
// // await payment.submit();
// }
支付宝订阅
const user = paying.user(userId);
const subscription = await user.prepareSubscription({
name: 'premium',
service: 'alipay',
extend(origin) {
return addMonths(origin, 1);
},
});
// 创建订单,用户支付,回调
await subscription.submit(data);
支付宝付款
const user = paying.user(userId);
const payment = await user.preparePayment({
product: '<product-id>',
amount: '15.00',
});
await payment.submit(data);
// 创建 store
let alipayStore = new Store(
new AlipayAdapter({
appId: 'app-id',
privateKey: 'xxx',
publicKey: 'yyy',
callbackURL: 'https://example.com/callback',
}),
{purchaseExpires: 3000},
);
let appleStore = new Store(AppleAdapter, {
appId: 'app-id',
privateKey: 'xxx',
});
- 创建或更新订阅, 当 product.group 和用户当前订阅的 group 相同时, 会更新订阅
let {subscription, payload} = alipayStore.createOrUpdateSubscription({
product,
user,
});
// subscription.status === 'pending'
- 返回 payload 给客户端,调起支付宝付款
- 支付宝回调, 更新订阅状态,或是已经被定时任务更新了
let subscription = alipayStore.handleNotification(data);
// subscription.status === 'active' || subscription.status === 'canceled'
// subscription.expiresAt === "2020-01-01T00:00:00.000Z"
- 取消订阅
store.cancelSubscription(subscription);
- 购买
let {transaction, payload} = alipayStore.createPurchase(product);
-
返回 payload 给客户端,调起支付宝付款
-
退款
alipayStore.refoundPurchase(transaction);
- 通过 receipt 创建或更新订阅
let subscription = appleStore.validateAndSaveReceipt(receipt);
- 当订阅信息更新时的回调处理
let subscription = appleStore.handleNotification(data);
let transaction = appleStore.validateAndSaveReceipt(receipt);
定期检查过期订阅,以及订阅扣费
store.checkSubscriptions(10);
定期检查未完成的过期付款
store.checkTransactions(20);
let subscription = store.getSubscription(subscriptionId);
// subscription.transactions: Transaction[]
// subscription.expiresAt: "2020-01-01T00:00:00.000Z"
// subscription.status: "active" || "canceled" || 'pending'
let transaction = store.getTransaction(transactionId);
// transaction.status: "pending" || "completed" || "failed"
let subscriptions = store.getSubscriptionByUserId(userId);
let transactions = store.getTransactionsByUserId(userId);