Moltin 3.1.2

Moltin 3.1.2

测试已测试
Lang语言 Obj-CObjective C
许可 MIT
Released最新版本2019 年 3 月

Craig TweedyGeorge FitzGibbonsCraig Tweedy 维护。



Moltin 3.1.2

  • Craig Tweedy

Moltin iOS SDK

Moltin

CocoaPods Compatible Carthage Compatible Twitter

用于 Moltin 平台的 iOS/tvOS/watchOS SDK,用 Swift 编写。

要求

  • iOS 10.0+ / tvOS 10.0+ / watchOS 3.0+
  • Swift 4.0+

安装

Cocoapods

向您的 Podfile 添加以下内容

pod 'Moltin', '~> 3.1.2'

或者,快速尝试我们的示例

pod try Moltin

Carthage

将以下内容添加到您的 Cartfile

github "Moltin/ios-sdk" ~> 3.1.2

Swift 包管理器

Package.swift 中的 dependencies 值下添加以下内容

dependencies: [
    .package(url: "https://github.com/moltin/ios-sdk.git", from: "3.1.2")
]

使用方法

发送请求

let moltin = Moltin(withClientID: "<your client ID>")

moltin.product.all { result in
    switch result {
        case .success(let response):
            print(response)
        case .failure(let error):
            print(error)
    }
}

moltin.product.get("<product ID>") { result in
    switch result {
        case .success(let response):
            print(response)
        case .failure(let error):
            print(error)
    }
}

moltin.product.tree { result in
    switch result {
        case .success(let response):
            print(response)
        case .failure(let error):
            print(error)
    }
}

结账与支付

在 Moltin 中,支付购物车是一个两个步骤的过程。

首先,检查您的购物车,这将返回一个订单

self.moltin.cart.checkout(
    cart: ...,
    withCustomer: ...,
    withBillingAddress: ...,
    withShippingAddress: ...) { (result) in
    switch result {
        case .success(let order):
            ...
        default: break
    }
}

现在您有了订单,您可以支付订单。Moltin 提供了多个网关供您使用

  • Stripe
  • BrainTree
  • Adyen
  • 手动

一旦您选择了支付网关,您就可以完成 Moltin 的 PaymentMethod 之一

let paymentMethod = StripeToken(withStripeToken: ...)

然后您可以使用此支付方式来支付订单

self.moltin.cart.pay(
    forOrderID: order.id,
    withPaymentMethod: paymentMethod) { (result) in
    ...
}

配置

配置Moltin SDK的基本方式是使用您的客户端ID创建一个Moltin类的实例,还可以选择性地指定应用程序的区域。但是,如果您想更改SDK的其他细节,例如您的Moltin实例的URL,您可以通过传递MoltinConfig来实现。

let moltin = Moltin(withClientID: ...) // Takes Locale.current
let moltin = Moltin(withClientID: ..., withLocale: ...)
let config = MoltinConfig(
    clientID: ...,
    scheme: ...,
    host: ...,
    version: ...,
    locale: ...)
    
let moltin = Moltin(withConfiguration: config)

或者

let config = MoltinConfig.default(
    withClientID: ...,
    withLocale: ...)
    
let moltin = Moltin(withConfiguration: config)

可用的资源

  • 品牌
  • 购物车
  • 分类
  • 收藏夹
  • 货币
  • 文件
  • 流程
  • 字段
  • 条目
  • 产品

身份验证

SDK会为您静默处理身份验证,以确保它不会进行不必要的请求。

目前iOS SDK只支持隐式身份验证。

过滤

操作

  • 过滤
  • 排序
  • 偏移/限制
  • 包含

过滤

moltin.product.filter(operator: .eq, key: "name", value: "ProductName").all {
   ...
}

排序

moltin.product.sort("order").all {
   ...
}
moltin.product.sort("-order").all {
   ...
}

偏移/限制

moltin.product.limit(10).offset(20).all {
    ...
}

包含

moltin.product.include([.mainImage, .files]).all {
    ...
}

合并操作

moltin.product.sort("-name").include([.mainImage]).limit(20).all {
   ...
}

流程

如果您通过流程在资源上实现了一个自定义字段,您可以通过类型提示您的结果将其转换为所需的类型,只要此类型符合 可编码

moltin.product.all { (result: Result<PaginatedResponse<[MyCustomProduct]>>) in
    switch result {
        case .success(let response):
            print(response.data) // [MyCustomProduct]
        case .failure(_):
            break
    }
}
moltin.product.get(forID: "<your ID>") { (result: Result<MyCustomProduct>) in
    switch result {
    case .success(let response):
        print(response) // MyCustomProduct
    case .failure(_):
        break
    }

我们建议确保您的类型从我们的基本类型继承以保证安全,然后实现 required init(from decoder: Decoder)

class MyCustomProduct: moltin.Product {
    let author: Author

    enum ProductCodingKeys : String, CodingKey { 
        case author 
    }

    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: ProductCodingKeys.self)
        self.author = try container.decode(Author.self, forKey: .author)
        try super.init(from: decoder)
    }
}

这将允许您根据需要添加更多类型,但确保基本类型(如产品)仍然正确解析。

更多文档

API 文档中找到更详细的文档。

沟通

  • 如果您需要帮助 SDK 或平台,请在 论坛 上联系。
  • 如果您发现 SDK 存在错误,请在 GitHub 上提交一个问题。
  • 如果您对 SDK 有功能请求,请提交一个问题。
  • 如果您想为 SDK 做贡献,请提交一个拉取请求。

许可

Moltin 适用于 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。