In order for our prebuilt UI elements to function, you’ll need to provide them with an ephemeral key, a short-lived API key with restricted API access. You can think of an ephemeral key as a session, authorizing the SDK to retrieve and update a specific Customer object for the duration of the session.
client.makeJson(options: options, completion: { result in switch result { case .success(let json): completion(json, nil) case .failure(let error): completion(nil, error) } }) } }
Setting up STPCustomerContext and STPPaymentContext
finalclassMainController: UIViewController{ let client = EphemeralKeyClient() let customerContext: STPCustomerContext let paymentContext: STPPaymentContext
If we use stripe_id from card, which has the form of card_xxx, we need to include customer info
If we use token, which has the form tok_xxx, then no need for customer info
From STPPaymentResult
When you’re using STPPaymentContext to request your user’s payment details, this is the object that will be returned to your application when they’ve successfully made a payment. It currently just contains a source, but in the future will include any relevant metadata as well. You should pass source.stripeID to your server, and call the charge creation endpoint. This assumes you are charging a Customer, so you should specify the customer parameter to be that customer’s ID and the source parameter to the value returned here. For more information, see https://stripe.com/docs/api#create_charge
type ApplePayRequest struct { Token string`json:"token"` }
funchandleChargeUsingApplePay(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body) var t ApplePayRequest err := decoder.Decode(&t) if err != nil { panic(err) }
params := &stripe.ChargeParams{ Amount: stripe.Int64(150), Currency: stripe.String(string(stripe.CurrencyUSD)), Description: stripe.String("Charge from my Go backend for Apple Pay"), } params.SetSource(t.Token) ch, err := charge.New(params) if err != nil { fmt.Fprintf(w, "Could not process payment: %v", err) fmt.Println(ch) w.WriteHeader(400) } w.WriteHeader(200) }
The PKPaymentAuthorizationController class performs the same role as the PKPaymentAuthorizationViewController class, but it does not depend on the UIKit framework. This means that the authorization controller can be used in places where a view controller cannot (for example, in watchOS apps or in SiriKit extensions).
client.useApplePay(payment: payment, completion: { result in switch result { case .success: completion(.init(status: .success, errors: nil)) case .failure(let error): completion(.init(status: .failure, errors: [error])) } }) } }
Showing Apple Pay option
From appleMerchantIdentifier
The Apple Merchant Identifier to use during Apple Pay transactions. To create one of these, see our guide at https://stripe.com/docs/mobile/apple-pay . You must set this to a valid identifier in order to automatically enable Apple Pay.
Requests payment from the user. This may need to present some supplemental UI to the user, in which case it will be presented on the payment context’s hostViewController. For instance, if they’ve selected Apple Pay as their payment method, calling this method will show the payment sheet. If the user has a card on file, this will use that without presenting any additional UI. After this is called, the paymentContext:didCreatePaymentResult:completion: and paymentContext:didFinishWithStatus:error: methods will be called on the context’s delegate.
Use STPPaymentOptionsViewController to show cards and Apple Pay options
funcpaymentOptionsViewController(_ paymentOptionsViewController: STPPaymentOptionsViewController, didSelect paymentOption: STPPaymentOption) { // No op }
After user selects payment option, the change is saved in dashboard https://dashboard.stripe.com/test/customers, but for card only. Select Apple Pay does not reflect change in web dashboard.
Apple pay option is added manually locally, from STPCustomer+SourceTuple.m 😲
STPApplePayPaymentOptionis not available inpaymentContext.paymentOptions` immediately
Change selected payment option
In STPPaymentContext
setSelectedPaymentOption is read only and trigger paymentContextDidChange, but it checks if the new selected payment option is equal to existing selected payment option
Which in turns call STPCustomerEphemeralKeyProvider. As stripe does not save Apple Pay option in dashboard, this method return list of card payment options, together with the default card as selected payment option 😲
Although the new STPCard has a different address, it is the exact same card with the same info, and the isEqual method of STPCard is