Swift 支付成功获取条带收费id

Swift get stripe charge id when payment has succeeded

我正在使用 stripe api 进行支付。

我正在尝试从 pay() 函数接收费用 ID,但在任何参数变量中都看不到费用 ID。这是我必须在服务器端做的事情吗?我假设收费 ID 会在付款意向参数中,但事实并非如此。我是否必须通过其他方式获取收费 ID?

费用 ID 示例:

"id": "ch_1IXpNqHiSsWQPbMQ3dNTp73m"

我创建了一个支付意向服务器端并像这样处理支付:https://stripe.com/docs/payments/integration-builder

@objc
  func pay() {
    guard let paymentIntentClientSecret = paymentIntentClientSecret else {
        return;
    }
    // Collect card details
    let cardParams = cardTextField.cardParams
    let paymentMethodParams = STPPaymentMethodParams(card: cardParams, billingDetails: nil, metadata: nil)
    let paymentIntentParams = STPPaymentIntentParams(clientSecret: paymentIntentClientSecret)
    paymentIntentParams.paymentMethodParams = paymentMethodParams
    // Submit the payment
    let paymentHandler = STPPaymentHandler.shared()
    paymentHandler.confirmPayment(withParams: paymentIntentParams, authenticationContext: self) { (status, paymentIntent, error) in
      switch (status) {
      case .failed:
          self.displayAlert(title: "Payment failed", message: error?.localizedDescription ?? "")
          break
      case .canceled:
          self.displayAlert(title: "Payment canceled", message: error?.localizedDescription ?? "")
          break
      case .succeeded:
          self.displayAlert(title: "Payment succeeded", message: paymentIntent?.description ?? "")
          break
      @unknown default:
          fatalError()
          break
      }
    }
  }

参数输出:

成功:

nil

状态:

STPPaymentHandlerActionStatus

付款意图:

Optional(<Stripe.STPPaymentIntent: 0x600003d443c0; stripeId = pi_xxx; amount = 555; canceledAt = nil; captureMethod = Optional("automatic"); clientSecret = ; confirmationMethod = Optional("automatic"); created = 2021-03-22 15:08:09 +0000; currency = usd; description = nil; lastPaymentError = nil; livemode = false; nextAction = nil; paymentMethodId = Optional("pm_1IXpMTKn7R1M6tqnP8vs3i7n"); paymentMethod = Optional(<Stripe.STPPaymentMethod: 0x600003d445a0; stripeId = pm_1IXpMTKn7R1M6tqnP8vs3i7n; alipay = nil; auBECSDebit = nil; bacsDebit = nil; bancontact = nil; billingDetails = Optional(<Stripe.STPPaymentMethodBillingDetails: 0x600002106d00; name = ; phone = ; email = ; address = Optional(<Stripe.STPPaymentMethodAddress: 0x600002c02b80; line1 = ; line2 = ; city = ; state = ; postalCode = ; country = >)>); card = Optional(<Stripe.STPPaymentMethodCard: 0x600003015320; brand = Visa; checks = <Stripe.STPPaymentMethodCardChecks: 0x600000dc4090; addressLine1Check: ; addressPostalCodeCheck: ; cvcCheck: >; country = US; expMonth = 4; expYear = 2024; funding = credit; last4 = 4242; fingerprint = ; networks = <Stripe.STPPaymentMethodCardNetworks: 0x600000dc4060; available: ["visa"]; preferred: >; threeDSecureUsage = <Stripe.STPPaymentMethodThreeDSecureUsage: 0x6000002a0200; supported: YES>; wallet = >); cardPresent = nil; created = Optional(2021-03-22 15:08:10 +0000); customerId = ; ideal = nil; eps = nil; fpx = nil; giropay = nil; netBanking = nil; oxxo = nil; grabPay = nil; payPal = nil; przelewy24 = nil; sepaDebit = nil; sofort = nil; upi = nil; afterpay_clearpay = nil; liveMode = NO; type = card>); paymentMethodTypes = Optional(["card"]); receiptEmail = nil; setupFutureUsage = nil; shipping = nil; sourceId = nil; status = Optional("succeeded")>)

paymentIntentParams:

<Stripe.STPPaymentIntentParams: 0x600003544000; stripeId = Optional("pi_1IXpMTKn7R1M6tqn8WIzEoyE"); clientSecret = ; receiptEmail = nil; returnURL = nil; savePaymentMethod = nil; setupFutureUsage = nil; shipping = nil; useStripeSDK = nil; sourceId = nil; sourceParams = nil; paymentMethodId = nil; paymentMethodParams = Optional(<Stripe.STPPaymentMethodParams: 0x60000377ee80>); mandateData = nil; paymentMethodOptions = @nil; additionalAPIParameters = [:]> card params: <Stripe.STPPaymentMethodCardParams: 0x600002163ca0; last4 = 4242; expMonth = 4; expYear = 24; cvc = ; token = >

PaymentIntent 的所有费用都在 charges 列表 [0] 下,但这只是 return 服务器端的,目前无法通过可发布密钥检索。

您将获取 PaymentIntent 服务器端,它将 return 您整个 charges 列表,最近的费用是该列表中的第一个元素。

[0] https://stripe.com/docs/api/payment_intents/object#payment_intent_object-charges