javascript - 使用 Stripe、Google Cloud Functions 和 Swift 为用户订阅计划有效,但是
问题描述
我已经实施了以下解决方案来为用户订阅一个在 99% 的时间都有效的计划。问题是即使用户已成功订阅,应用程序也会不时显示错误消息。
流程如下:
- 用户在应用程序中点击订阅,然后它将以下值写入 Firebase
- 使用以下部署的谷歌云功能,然后我根据上面的书面价值为用户订阅/取消订阅计划
subscriptionTriggered
。来自的响应Stripe
被写回Firebase
其中还包含一个密钥status
.
exports.subscribeCustomer = functions.database.ref('/stripe_customers/{userId}/subscription/subscriptionTriggered')
.onWrite((change, context) => {
const subscriptionTriggered = change.after.val();
if (subscriptionTriggered === true) {
console.log('subscriptionTriggered: ', subscriptionTriggered);
// Look up the Stripe customer id written in createStripeCustomer
return admin.database().ref(`/stripe_customers/${context.params.userId}/customer_id`)
.once('value').then((snapshot) => {
return snapshot.val();
}).then((customer) => {
return admin.database().ref('/app-config/stripePlan').once('value').then((snapshot) => {
const plan = snapshot.val();
return stripe.subscriptions.create({
customer: customer,
items: [
{
plan: plan,
},
]
});
});
}).then((response) => {
// If the result is successful, write it back to the database
return change.after.ref.parent.child('response').set(response);
//return snap.ref.child('response').set(response);
}).catch((error) => {
// We want to capture errors and render them in a user-friendly way, while
// still logging an exception with StackDriver
return change.after.ref.parent.child('error').set(userFacingMessage(error));
}).then((error) => {
return reportError(error, {user: context.params.userId});
});
} else if (subscriptionTriggered === false) {
console.log('subscriptionTriggered: ', subscriptionTriggered);
return admin.database().ref(`/stripe_customers/${context.params.userId}/subscription/response/id`)
.once('value').then((snapshot) => {
return snapshot.val();
}).then((sub) => {
return stripe.subscriptions.del(
sub
);
}).then((response) => {
// If the result is successful, write it back to the database
return change.after.ref.parent.child('response').set(response);
//return snap.ref.child('response').set(response);
}).catch((error) => {
// We want to capture errors and render them in a user-friendly way, while
// still logging an exception with StackDriver
return change.after.ref.parent.child('error').set(userFacingMessage(error));
}).then((error) => {
return reportError(error, {user: context.params.userId});
});
}//end else-if
});
- 然后应用程序等待 5 秒,然后读取写入的响应
status
密钥。并且将加载下一个视图或显示错误消息。问题是有时会显示错误消息,即使当我Firebase
手动检查响应时,status
关键是active
它应该相应地处理。
我感觉网络写入的时间超过 5 秒,Firebase
因此返回status = canceled
。是否有另一种方法可以检查订阅状态,而不是简单地将等待时间从 5 秒增加到更长的等待时间以应对网络延迟?
.
func checkSubscriptionIfSuccessfulAndAction(){
guard let discoverVC = storyboard?.instantiateViewController(withIdentifier: "DiscoverVC") as? DiscoverVC else { return }
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(5000)) {
DataService.run.checkUserHasActiveSubscription(handler: { (status) in
print("inside checkSubscriptionIfSuccessfulAndAction status: \(status)")
if status == "active" {
//Subscription was processed successfully and is active
print("Customer Subscribed successfully to view Carousel")
DataService.run.getLatestSubscriptionAndPaymentSourceStatus()
Utilities.run.dismissSVHUD(delay: 0.0)
discoverVC.initData(forVenue: self.venue!)
DispatchQueue.main.async {
self.presentDetail(discoverVC)
}//end DispatchQueue.main.async
} else {
// There was an error Subscribing the customer
print("There was an error processing the charge by Stripe and it returned an error message")
Utilities.run.dismissSVHUD(delay: 1.0)
self.showMessageViaNotificationBannerSwift(message: "oops! We hit a snag, please try again!")
}//end if-else
})//end checkUserHasActiveSubscription
}//end DispatchQueue.main.asyncAfter
}//end func
解决方案
推荐阅读
- command-line - 如何在终端的 txt 文件中执行命令行?
- javascript - 从 FIREBASE 发送 POST 请求不起作用
- assembly - 访问数组元素的 MIPS 程序
- excel - 自升级到 Office Pro Plus 后,工作表的 VBA 方法“删除”失败
- swift - 在后台队列上读取更新的 Realm ThreadSafeReference
- android - 如何使用 ViewPager2 删除 TabLayout 上的滚动条?
- python - KeyError:“['belongs_to_collection' 'homepage' 'original_title' 'overview'\n 'poster_path' 'status' 'tagline'] 在轴中找不到”
- python - DataFrame - 追加结果并求和
- excel - IF 公式作为 VBA 复制和粘贴的一部分
- java - 不要为基本类型(如字符串、数组、基元和盒装基元)建议 'var' 类型