@attheodo

Integrating with

Two main classes

STPPaymentConfiguration

An STPPaymentConfiguration represents all the options you can set or change around a payment.

STPPaymentContext

An STPPaymentContext keeps track of all of the state around a payment. It will manage fetching a user’s saved payment methods, tracking any information they select, and prompting them for required additional information before completing their purchase

Global setup

STPPaymentConfiguration.shared().publishableKey = "pk_test_TYo"
STPPaymentConfiguration.shared().appleMerchantIdentifier = "mer_id"

 

Configure your publishable key

"Session" setup

Initialise your Customer Context

An STPCustomerContext retrieves and updates a Stripe customer using an ephemeral key, a short-lived API key scoped to a specific customer object. 

If your current user logs out of your app and a new user logs in, be sure to either create a new instance of STPCustomerContext or clear the current instance’s cached customer.

"Session" setup

Initialise your Customer Context

customerContext = STPCustomerContext(keyProvider: self)

extension ShoppingCartService: STPEphemeralKeyProvider {
    
    func createCustomerKey(withAPIVersion apiVersion: String, 
                           completion: @escaping STPJSONResponseCompletionBlock) 
{
        
        apiService.getStripeEphemeralKey { result in
                       
            switch result {
            
            case .failure(let error):
                completion(nil, error)
            
            case .success(let payload):
                
                if let payloadAsDict = convertToDictionary(text: payload) {
                    completion(payloadAsDict, nil)
                } else {
                    completion(nil, NSError())
                }
            
            }
            
        }
        
    }
    
}

"Session" setup

Initialise your Payment Context

paymentContext = STPPaymentContext(customerContext: customerContext!)
paymentContext?.delegate = self


extension ShoppingCartService: STPPaymentContextDelegate {
    
    func paymentContextDidChange(_ paymentContext: STPPaymentContext) {}
    
    func paymentContext(_ paymentContext: STPPaymentContext, 
                        didFailToLoadWithError error: Swift.Error) {}
    
    func paymentContext(_ paymentContext: STPPaymentContext,
                        didCreatePaymentResult paymentResult: STPPaymentResult,
                        completion: @escaping STPErrorBlock)
    {
        
    }
    
    func paymentContext(_ paymentContext: STPPaymentContext, 
                        didFinishWith status: STPPaymentStatus, 
                        error: Swift.Error?) 
    {
        
    }
    
}

Gather required payment info

STPShippingAddressViewController

This view controller contains a shipping address collection form. 

It renders a right bar button item that submits the form, so it must be shown inside a UINavigationController. 

Depending on your configuration’s shippingType, the view controller may present a shipping method selection form after the user enters an address.

Gather required payment info

STPPaymentMethodsViewController

This view controller presents a list of payment method options to the user, which they can select between. They can also add credit cards to the list.

DEMO

Questions?

Integrating with Stripe

By Thanos Theodoridis

Integrating with Stripe

  • 307