我正在尝试在用户的设置中列出用户的支付方式,以防他们想要编辑或删除它们。主要是,我想得到品牌,最后4位数字和有效期。
这是我后端的功能:
exports.listPaymentMethods = functions.https.onCall(async (data, context) => {
const customerId = data.customer_id;
const paymentMethods = await stripe.paymentMethods.list({
customer: customerId,
type: "card",
});
});
我使用以下方法在客户端上调用此函数:
func listPaymentMethods(customerID: String) {
FirebaseReferenceManager.functions.httpsCallable("listPaymentMethods").call(["customer_id": customerID]) { (response, error) in
if let error = error {
print("failed to list customer's payment methods: (error.localizedDescription)")
}
if let response = (response?.data as? [String: Any]) {
print(response)
}
}
}
然而,我得到了错误";数据无法读取,因为它的格式不正确;
如有任何帮助,我们将不胜感激!:(
我不得不使用将paymentMethods.data从服务器返回到客户端
exports.listPaymentMethods = functions.https.onCall(async (data, context) => {
const customerId = data.customer_id;
const paymentMethods = await stripe.paymentMethods.list({
customer: customerId,
type: "card",
});
const paymentMethodsData = paymentMethods.data;
return {
paymentMethodsData: paymentMethodsData,
};
});
在我的客户端上,我不得不调用这样的函数:
func listPaymentMethods(customerID: String) {
FirebaseReferenceManager.functions.httpsCallable("listPaymentMethods").call(["customer_id": customerID]) { (response, error) in
if let error = error {
print("failed to list customer's payment methods: (error.localizedDescription)")
}
if let response = (response?.data as? [String: Any]) {
let data = (response["paymentMethodsData"] as! Array<Any>?)
print(data!)
}
}
}