【问题标题】:How do I use a Firebase Function to get a stripe emphemeral key in iOS?如何使用 Firebase 函数在 iOS 中获取条带临时密钥?
【发布时间】:2021-01-14 13:51:38
【问题描述】:

我正在尝试使用 firebase 函数从条带中获取临时密钥。这是我在 Firebase 日志中看到的错误:

getStripeEphemeralKeysUnhandled 错误类型错误:无法在 func (/workspace/node_modules/firebase-functions/lib) 处读取未定义的属性“create”。 /providers/https.js:273:32) 在 process._tickCallback (internal/process/next_tick.js:68:7)

这是在 xcode 控制台中显示的错误:

创建临时密钥时出错:Error Domain=com.firebase.functions Code=13 "INTERNAL" UserInfo={NSLocalizedDescription=INTERNAL} INTERNAL

用 Javascript 编写的 firebase 函数:

const functions = require('firebase-functions');
const stripe_key = "sk_test_LT2Wc4v9kUD6XxG8Nq2LNh4P00thTqtiSa"  
const admin = require('firebase-admin');
var stripe = require('stripe')(stripe_key);
admin.initializeApp(functions.config().firebase);

exports.createStripeCustomer = functions.https.onCall(async (data, context) => {
  const full_name = data.full_name;
  const email = data.email;
  const customer = await stripe.customers.create({
      email: email,
      name: full_name,
      description: full_name
  });
  console.log('new customer created: ', customer.id)
  return {
      customer_id: customer.id
  }
});

exports.getStripeEphemeralKeys = functions.https.onCall(async (data, context) => {
  const api_version = data.api_version;
  const customer_id = data.customer_id;
  const key = await stripe.ephemeralKeys.create (
      {customer: customer_id},
      {apiVersion: api_version}
  );
  return key;
});

使用 swift 调用 iOS

func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
        let stripe_customer_id = MyDefaults.getDefaultsForCustID()
        functions.httpsCallable("getStripeEphemeralKeys").call(["api_version" : apiVersion, "customer_id" : stripe_customer_id]) { (response, error) in
            if let error = error {
                completion(nil, error)
            }
            if let response = (response?.data as? [String: Any]) {
                completion(response, nil) 
            }
        }
    }

我对 Javascript 不够精通,无法知道错误的含义,除非 create 这个词似乎有问题。任何想法都非常感谢!

【问题讨论】:

  • 错误信息告诉您stripe.ephemeralKeys 未定义。你希望它是什么? stripe 来自哪里?请编辑问题以显示整个代码示例
  • 嗨@DougStevenson,谢谢-我现在正在展示整个代码。我期待使用这个 firebase 函数从条带中获取一个临时密钥。 stripe 是我在云函数中使用的依赖项。
  • 你在这里使用的是什么版本的 Stripe?
  • @floatingLomas 我的 package.json 说 "stripe": "^8.103.0" 但条纹客户服务说我使用的是过时的版本......仍在试图弄清楚这是怎么回事。
  • stripe_key 的值可能是错误的,因此 stripe 变量未正确填充并导致 Doug 提到的 undefined 错误。试试看值是否正确,另外,Stripe的最新版本是8.104.0,你也可以试试升级一下。

标签: javascript ios firebase google-cloud-functions stripe-payments


【解决方案1】:

好的,谢谢@floatingLomas #Rafael Lemos。你们走在正确的轨道上。这是条纹版本。我遇到了一些 pod 错误,后来我正在努力修复,所以清理并更新了 Cocoa pods ruby​​ 和 npm ......现在当我进行 firebase 部署时 - 我终于将正确版本的条带升级到 firebase ......

我现在很好——谢谢!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 2018-11-26
    相关资源
    最近更新 更多