【问题标题】:How to list customer's payment methods?如何列出客户的付款方式?
【发布时间】:2021-08-13 15:57:15
【问题描述】:

我正在尝试在他们的设置中列出用户的付款方式,以防他们想要编辑或删除它们。主要是想得到品牌、后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)
    }
}

}

但是,我收到错误消息“无法读取数据,因为它的格式不正确”

任何帮助将不胜感激! :)

【问题讨论】:

  • 我可能遗漏了一些东西,但您的后端似乎没有返回任何东西?您可能需要对 paymentMethodsreturn paymentMethods; 做点什么?但更有可能您需要在形成 JSON 响应方面做更多工作,例如运行一些代码来映射 paymentMethods.data 并创建一个包含应用程序所需特定信息的数组,而不是原始条带节点响应对象。

标签: ios node.js swift google-cloud-functions stripe-payments


【解决方案1】:

我必须使用以下命令将 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!)
    }
}

}

【讨论】:

    猜你喜欢
    • 2019-11-22
    • 1970-01-01
    • 2021-07-26
    • 2015-10-10
    • 1970-01-01
    • 2018-01-06
    • 2019-11-11
    • 1970-01-01
    • 2014-12-29
    相关资源
    最近更新 更多