Getting started with Swift

1

Install with CocoaPods

The recommended way to install the SDK is via CocoaPods. Add Moltin version 2 to your existing project’s Podfile.

— THEN —

Install the pod using the terminal of your choice (additional installation methods are defined in the README).

Podfile

pod 'Moltin', '~> 3.1'

 

pod install

2

Import

Import the Moltin SDK into your Swift file.

Swift File

import Moltin

3

Authenticate

Initialize the Moltin SDK. You will need a public ID to authenticate with our API. You can find your store keys in your store dashboard.

let moltin: Moltin = Moltin(withClientID: "XXXX")

A simple checkout flow

1

Get products

Now that we’ve authenticated with the Moltin API lets get one of our sample products. Please note you will need to create a product first via the API or store management dashboard.

This API call should contain an array of products. A good place to display this data is on a product or listing page.

moltin.product.all { (result) in
        switch result {
          case .success(let response):
            print("Got products: \(response.data)")
          case .failure(let error):
            print("Got failure: \(error)")
        }
      }

2

Add to cart

Using a product ID we can insert a quantity of this product into the cart with one simple request. The first parameter passed is the product ID, the second the quantity to be added to the cart. A third parameter can be added for modifiers and variations (e.g. size and color) if these are set up.

This response should contain an array of the individual cart item data e.g. title, quantity, price. A good example of integrating this would be to add a simple button or form to the product page that posts the product ID and quantity to this endpoint.

moltin.cart.addProduct(withID: product.id,
                                     ofQuantity: 1,
                                     toCart: "XXXX") { result in
        switch result {
          case .success:
            print("Added a product to the cart")
          case .failure(let error):
            print("Got error adding a product to the cart: \(error)")
        }
      }

3

Get cart contents

Now that there is at least one product in the cart let’s get the full cart contents.

This response should contain an array of all cart items and cart totals. A good place to show this data would be on the cart or orders page, or even a widget in the header of your website.

moltin.cart.items(forCartID: "XXXX") { (result) in
        switch result {
          case .success(let response):
            print("Got items: \(response.data)")
          case .failure(let error):
            print("Got failure: \(error)")
        }
      }
      

4

Convert to order

When the customer is ready to checkout we need to convert the cart to an order. This call lets you define the payment gateway and conditional order parameters such as customer, billing and shipping addresses.

This call will return an object containing data for the newly created order.

let customer = Customer(withEmail: "XXXX", withName: "XXXX")
      let billingAddress = Address(withFirstName: "XXXX", withLastName: "XXXX")
      billingAddress.line1 = "XXXX"
      billingAddress.county = "XXXX"
      billingAddress.country = "XXXX"
      billingAddress.postcode = "XXXX"
      
      moltin.cart.checkout(cart: "XXXX",
      withCustomer: customer,
      withBillingAddress: billingAddress,
      withShippingAddress: nil) { (result) in
        switch result {
          case .success(let order):
            print("Created order: \(order)")
          case .failure(let error):
            print("Could not create order: \(error)")
        }
      }
      

5

Process payment

Once we’ve converted a cart into an order we’re now ready to process a payment. In this example, we’ve used the dummy gateway so we just need to provide some card details. The data you need to provide in this step depends on your chosen gateway.

Congratulations, if you made it this far you’ve implemented a simple step by step checkout!

let paymentMethod: PaymentMethod = StripeCard(withFirstName: "XXXX",
        withLastName: "XXXX",
        withCardNumber: "XXXX",
        withExpiryMonth: "XXXX",
        withCVVNumber: "XXXX")
      moltin.cart.pay(forOrderID: "XXXX",
      withPaymentMethod: paymentMethod) { (result) in
        switch result {
          case .success(let status):
            print("Paid for order: \(status)")
          case .failure(let error):
            print("Could not pay for order: \(error)")
        }
      }
      

Ready to get started?

It always starts with a conversation.