Swiftpack.co - tabby-ai/tabby-ios-sdk as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
tabby-ai/tabby-ios-sdk
Tabby iOS SDK
.package(url: "https://github.com/tabby-ai/tabby-ios-sdk.git", from: "TabbyPresentationSnippet")

Tabby

Requirements

iOS 13.0+ Swift 5.3+ Xcode 12.0+

Integration

Swift Package Manager (recommended)

dependencies: [
    .package(url: "https://github.com/tabby-ai/tabby-ios-sdk.git", .upToNextMajor(from: "0.0.1"))
]

import UIKit
import SwiftUI
import Tabby

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        if #available(iOS 13.0, *) {
            let vc = UIHostingController(
                rootView: Tabby.TabbyPresentationSnippet(
                    amount: 800,
                    currency: .AED,
                    lang: Lang.en)
            )
            addChild(vc)
            vc.view.frame = view.frame
            view.addSubview(vc.view)
            vc.didMove(toParent: self)
        } else {
            // Fallback on earlier versions
        }
    }
}

UIKit

Result

Snippet EN Snippet AR

SDK usage

1. Init Tabby when your app starts (AppDelegete or @main)

TabbySDK.shared.setup(withApiKey: "__API_KEY_HERE__")

2. Prepare view

import Tabby

let customerPayment = Payment(
    amount: "340",
    currency: .AED,
    description: "tabby Store Order #3",
    buyer: Buyer(
    email: "successful.payment@tabby.ai",
    phone: "500000001",
    name: "Yazan Khalid"
        dob: nil
    ), order: Order(
        reference_id: "#xxxx-xxxxxx-xxxx",
        items: [
            OrderItem(
                description: "Jersey",
                product_url: "https://tabby.store/p/SKU123",
                quantity: 1,
                reference_id: "SKU123",
                title: "Pink jersey",
                unit_price: "300"
            )],
        shipping_amount: "50",
        tax_amount: "100"
    ),
    shipping_address: ShippingAddress(
        address: "Sample Address #2",
        city: "Dubai"
    )
)

let myTestPayment = TabbyCheckoutPayload(merchant_code: "ae", lang: .en, payment: customerPayment)

...
in your CartScreenView etc in .onAppear or viewDidLoad 
...
.onAppear() {
    TabbySDK.shared.configure(forPayment: myTestPayment) { result in
        switch result {
        case .success(let s):
            // 1. Do something sith sessionId (this step is optional)
            print("sessionid: \(s.sessionId)")
            // 2. Grab avaibable products from session and enable proper
            // payment method buttons in your UI (this step is required)
            print("tabby available products: \(s.tabbyProductTypes)")
            if (s.tabbyProductTypes.contains(.installments)) {
                self.isTabbyInstallmentsAvailable = true
            }
            if (s.tabbyProductTypes.contains(.pay_later)) {
                self.isTabbyPaylatersAvailable = true
            }
        case .failure(let error):
            // Do something when Tabby checkout session POST requiest failed
            print(error)
        }
    }
}

3. Launch Tabby checkout

SDK is built for your convenience - one TabbySDK.shared.configure(forPayment: myTestPayment) { result in ... } is called - you can render something like this if modal / sheet / seguway / NavigationLink / ViewController etc - whatever fits your UI and app structure. With both SwiftUI and UIKit

.sheet(isPresented: $isTabbyOpened, content: {
    TabbyCheckout(productType: openedProduct, onResult: { result in
        print("TABBY RESULT: \(result) !")
        switch result {
        case .authorized:
            // Do something else when Tabby authorized customer
            self.isTabbyOpened = false
            break
        case .rejected:
            // Do something else when Tabby rejected customer
            self.isTabbyOpened = false
            break
        case .close:
            // Do something else when customer closed Tabby checkout
            self.isTabbyOpened = false
            break
        }
    })
})

Full example code goes here


struct CheckoutExampleWithTabby: View {
    @State var isTabbyInstallmentsAvailable = false
    @State var isTabbyPaylatersAvailable = false
    
    @State var openedProduct: TabbyProductType = .installments
    @State var isTabbyOpened: Bool = false
    
    var body: some View {
        VStack {
            Button(action: {
                openedProduct = .installments
                isTabbyOpened = true
            }, label: {
                HStack {
                    Text("My Tabby 'Installments' fancy Button")
                        .font(.headline)
                        .foregroundColor(isTabbyInstallmentsAvailable ? .black : .white)
                }
            })
            .disabled(!isTabbyInstallmentsAvailable)
            
            Button(action: {
                openedProduct = .pay_later
                isTabbyOpened = true
            }, label: {
                HStack {
                    Text("My Tabby 'PayLater' fancy Button")
                        .font(.headline)
                        .foregroundColor(isTabbyPaylatersAvailable ? .black : .white)
                }
            })
            .disabled(!isTabbyPaylatersAvailable)
            
        }
        .sheet(isPresented: $isTabbyOpened, content: {
            TabbyCheckout(productType: openedProduct, onResult: { result in
                print("TABBY RESULT: \(result)!!!")
                switch result {
                case .authorized:
                    // Do something else when Tabby authorized customer
                    self.isTabbyOpened = false
                    break
                case .rejected:
                    // Do something else when Tabby rejected customer
                    self.isTabbyOpened = false
                    break
                case .close:
                    // Do something else when customer closed Tabby checkout
                    self.isTabbyOpened = false
                    break
                }
            })
        })
        .onAppear() {
            TabbySDK.shared.configure(forPayment: myTestPayment) { result in
                switch result {
                case .success(let s):
                    // 1. Do something sith sessionId (this step is optional)
                    print("sessionid: \(s.sessionId)")
                    // 2. Grab avaibable products from session and enable proper
                    // payment method buttons in your UI (this step is required)
                    print("tabby available products: \(s.tabbyProductTypes)")
                    if (s.tabbyProductTypes.contains(.installments)) {
                        self.isTabbyInstallmentsAvailable = true
                    }
                    if (s.tabbyProductTypes.contains(.pay_later)) {
                        self.isTabbyPaylatersAvailable = true
                    }
                case .failure(let error):
                    // Do something when Tabby checkout session POST requiest failed
                    print(error)
                }
            }
        }
    }
}

GitHub

link
Stars: 0
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics