如何使用 Stripe 为 Apple Pay 创建令牌

how can I create a token for Apple Pay with Stripe

所以我的最终目标是能够在 Stripe 上使用 Apple Pay 进行测试支付,无论是使用令牌还是其他东西。

**编辑**我有这个功能可以用来从钱包中存储的卡中收集卡信息:

 func applePayContext(_ context: STPApplePayContext, didCreatePaymentMethod paymentMethod: STPPaymentMethod, paymentInformation: PKPayment, completion: @escaping STPIntentClientSecretCompletionBlock) {
    guard let paymentIntentClientSecret = paymentIntentClientSecret else {
        return;
    }

    let backendUrlForToken = "https://us-central1-xxxxxx-41f12.cloudfunctions.net/createStripeToken"
    let url = URL(string: backendUrlForToken)
    let pkToken = String(data: paymentInformation.token.paymentData, encoding: .utf8)
    guard let name = paymentInformation.billingContact?.name else { return }
    let nameFormatter = PersonNameComponentsFormatter()
    nameFormatter.string(from: name)
    let json: [String: Any] = [
        "card": [
            
            "address_city": paymentInformation.billingContact?.postalAddress?.city,
            "address_country": paymentInformation.billingContact?.postalAddress?.country,
            "address_line1": paymentInformation.billingContact?.postalAddress?.street,
            "address_state": paymentInformation.billingContact?.postalAddress?.state,
            "address_zip": paymentInformation.billingContact?.postalAddress?.postalCode,
            "name": name
        ],
        "muid": UIDevice.current.identifierForVendor?.uuidString,
        "pk_token": pkToken,
        "pk_token_instrument_name": paymentInformation.token.paymentMethod.displayName,
        "pk_token_payment_network": paymentInformation.token.paymentMethod.network?.rawValue,
        "pk_token_transaction_id": paymentInformation.token.transactionIdentifier
    ]
    var request = URLRequest(url: url!)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.httpBody = try? JSONSerialization.data(withJSONObject: json)
    let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
        guard let response = response as? HTTPURLResponse,
              response.statusCode == 200,
              let data = data,
              let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
              let token = json["id"] as? String else {
                print("Error getting Stripe token")
                return
            }
        print("\(token)")
        self.stripeToken = token
        
    }
    
    task.resume()
    
    
    let error = NSError()
    completion(paymentIntentClientSecret, error)
    
}

我没有 API 调用的端点,所以我创建了这个令牌请求。这是我在 API 端点上的功能:

exports.createStripeToken = functions.https.onRequest(async (req, res) => {
var cardAddressCity = req.body.card.address_city;
var cardAddressCountry = req.body.card.address_country;
var cardAddressLine = req.body.card.address_line1;
var cardAddressProvince = req.body.card.address_state;
var cardAddressPostalCode = req.body.card.address_zip;
var cardHolderName = req.body.card.name;

var muid = req.body.muid;
var pkToken = req.body.pk_token;
var pkTokenInstrument = req.body.pk_token_instrument_name;
var pkTokenNetwork = req.body.pk_token_payment_network;
var pkTokenTransactionID =  req.body.pk_token_transaction_id;

const token = await stripe.tokens.create({
  card: {
    "address_city": cardAddressCity,
    "address_country": cardAddressCountry,
    "address_line1": cardAddressLine,
    "address_state": cardAddressProvince,
    "address_zip": cardAddressPostalCode,
    "name": cardHolderName
  },
  "muid": muid,
  "pk_token": pkToken,
  "pk_token_instrument_name": pkTokenInstrument,
  "pk_token_payment_network": pkTokenNetwork,
  "pk_token_transaction_id": pkTokenTransactionID 
});

res.send({
  id: token.id,
});

});

我不确定这是不是正确的方法,我在互联网上根本找不到太多关于此的信息。

我认为这是我在 api 日志中看到的参数,所以我使用了相同的参数,而且我还在 gist.github 上看到一个罕见的 post 一个人制作 API调用类似于这个,但由于某种原因它仍然根本不起作用。

您的 statements/logs 没有被击中,因为在 STPApplePayContext 内部创建令牌失败。对于背景,STPApplePayContext 做:

  1. 从 Apple Pay 加密卡详细信息创建令牌。

  2. 使用令牌,创建一个 PaymentMethod 对象。

  3. 然后触发 didCreatePaymentMethod() 委托实现。

第 1 步失败。

通常当我看到令牌创建请求 500 时,这通常意味着 XCode 上使用的商家 ID 与 Stripe 帐户上设置的证书可能不匹配。或者它可能是 Stripe 帐户国家/地区不支持的 PKPaymentNetwork 的某种组合。

我会重新设置商家 ID 和 Apple Pay 证书的步骤,最好是在一个新项目上,以消除任何混淆或冲突等。

由于这是 500 分,Stripe 支持人员将是处理该问题的合适人选,因为错误可能出在他们头上。我会为他们提供您的 /v1/tokens 创建请求的请求 ID。