简体   繁体   中英

Swift get stripe charge id when payment has succeeded

I am using stripe api to make a payment.

I am trying to receive the charge ID from the pay() function but do not see the charge id in any of the Param Variables. Is this something I have to do server side? I would assume the charge id would be in the payment intent params but it is not. Do I have to fetch the charge id some other way?

charge id example:

"id": "ch_1IXpNqHiSsWQPbMQ3dNTp73m"

I create a payment intent server side and process the payment like so: 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
      }
    }
  }

Param output:

succeeded:

nil

status:

STPPaymentHandlerActionStatus

paymentIntent:

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: 0x600000dc409 0; 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 = >

A PaymentIntent has all its Charges under the charges list [0] but that is only returned server-side, it is not currently retrievable via a Publishable key.

You would fetch the PaymentIntent server-side which will return you the entire charges list, with the most recent Charge being the first element in that list.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM