Pokepay 版本 2.0.16

Pokepay 版本 2.0.16

由以下人员维护:taniryoNaohiro TateishiEitaro FukamachiShin AoyamaTakashi IchiiAnn LinZwe Wai Yan HtetDanial



 
依赖于
APIKit~> 5.4.0
结果~> 5.0.0
 

Pokepay 版本 2.0.16

  • Eitaro Fukamachi

Pokepay iOS SDK

iOS SDK for Pocket Change Pay(《https://pay.pocket-change.jp》).

文档

使用方法

let client = Pokepay.Client(accessToken: "ZhwMsfoAyWZMGrCAKrrofmwYHV82GkUcf3kYSZYYf1oDKVvFAPIKuefyQoc1KDVr",
                            isMerchant: true,
                            env: .production)

client.getTerminalInfo() { result in
    switch result {
    case .success(let terminal):
        print(terminal)
        // => Terminal(id: "45046d7f-aa33-4d26-8cb0-8971aae5a487", name: "", hardwareId: "4e5c5d18-b27f-4b32-a0e0-e8900686fe23", pushToken: nil, user: Pokepay.User(id: "4abed0cc-6431-446f-aaf5-bebc208d84c1", name: "", isMerchant: true), account: Pokepay.Account(id: "1b4533c0-651c-4e79-8444-346419b18c77", name: "", balance: -15357.0, isSuspended: false, privateMoney: Pokepay.PrivateMoney(id: "090bf006-7450-4ed9-8da1-977ea3ff332c", name: "PocketBank", organization: Pokepay.Organization(code: "pocketchange", name: "ポケットチェンジ"), maxBalance: 30000.0, expirationType: "static")))
    case .failure(let error):
        print(error)
    }
}

// Create a token for sending 108 yen.
client.createToken(108) { result in
    switch result {
    case .success(let token):
        print(token)  // like 'https://www.pokepay.jp/cashtrays/dc204118-9e3b-493c-b396-b9259ce28663'
    case .failure(let error):
        print(error)
    }
}

// Scan a QR code
client.scanToken("https://www.pokepay.jp/cashtrays/dc204118-9e3b-493c-b396-b9259ce28663") { result in
    switch result {
    case .success(let transaction):
        print(transaction)
    case. failure(let error):
        print(error)
    }
}

环境

Pokepay API提供了多个服务器环境,用于开发环境和生产环境之间的隔离。《Pokepay.Client》(以及《Pokepay.OAuthClient》)通过传递:env参数来切换使用哪个环境。

// Sandbox (default)
let client = Pokepay.Client(accessToken: "ZhwMsfoAyWZMGrCAKrrofmwYHV82GkUcf3kYSZYYf1oDKVvFAPIKuefyQoc1KDVr",
                            env: .sandbox)
// Production
let client = Pokepay.Client(accessToken: "ZhwMsfoAyWZMGrCAKrrofmwYHV82GkUcf3kYSZYYf1oDKVvFAPIKuefyQoc1KDVr",
                            env: .production)

授权

Pokepay API为第三方应用提供OAuth身份验证。

  1. 在Web浏览器(如Safari或WKWebView)中打开授权URL
let oauth = Pokepay.OAuthClient(clientId: clientId, clientSecret: clientSecret, env: .production)
let url = oauth.getAuthorizationUrl()
// => https://www.pokepay.jp/oauth/authorize?client_id=xxxxxxxxxxx&response_type=code
  1. 等待用户在Pokepay网页上授权您的应用。
  2. 浏览器将重定向到带有授权代码的应用
  3. 用授权代码交换访问令牌
let accessToken = oauth.getAccessToken(code: code)
// => AccessToken(accessToken: "dXX1Guh7Ze0F_s6L8mAk-t4DXxvO2wd_IwWXbQBGdNo0nkj01tYA9EKY992H_mMP", refreshToken: "XKOfCZmLuRjLggDZzDfz", tokenType: "Bearer", expiresIn: 2591999)

刷新访问令牌

每个访问令牌都将过期。目前的有效期是30天,但将来可能会缩短。因此,请准备访问令牌过期以及API服务器返回403禁止的情况。

为了避免每次都使用OAuth重新身份验证,Pokepay为每个访问令牌返回一个刷新令牌,存储在《AccessToken》对象的refreshToken字段中。由于《OAuthClient》不会自动存储它,建议将其存储在任意安全的地方(如KeyChain)。

OAuthClient#refreshAccessToken是使用刷新令牌颁发另一个访问令牌的函数

oauth.refreshAccessToken(refreshToken: accessToken.refreshToken)
// => AccessToken(accessToken: "gtSn683mul_FFaMlB2jLyOyK-6LJ-u3Qiv-Iiy6cGoJZyKD242xe29BTHEYXXaqj", refreshToken: "-YvJULJ5rEhQ0fY86t80", tokenType: "Bearer", expiresIn: 2591999)

刷新令牌只能使用一次。务必在重新身份验证后更新刷新令牌。

APIs

[] Pokepay.Client

Options

  • accessToken (String): 用于请求Pokepay API的访问令牌。
  • isMerchant (Bool): 是否以商家身份访问的标志。(如果访问令牌不是为商家设计的,则应该出错。)
  • env (环境枚举): 用于指定要使用哪个环境的枚举值。

[] Client.send(_ request: Request)

向API发送HTTP请求并获取对象形式的响应。有关使用请求对象发送请求的详细信息,请参阅使用请求对象发送请求

[方法] Client.getTerminalInfo

获取访问 Terminal 的信息。

[方法] Client.scanToken(_ token: String, amount: Double? = nil)

通过银行API扫描令牌并执行新的交易。

[方法] Client.createToken(_ amount: Double? = nil, description: String? = nil, expiresIn: Int32? = nil)

创建一个新的令牌用于发送/接收金钱,可以通过 scanToken 执行。

[方法] Client.getTokenInfo(_ token: String)

获取由 createToken 创建的或使用 scanToken 读取的 token 的信息。结果类型可能为 BillCheckCashtray

[类] Pokepay.OAuthClient

选项

  • clientId (String): OAuth 客户端 ID
  • clientSecret: OAuth 客户端密钥
  • env (环境枚举): 用于指定要使用哪个环境的枚举值。

[方法] OAuthClient.getAuthorizationUrl() -> String

获取用于打开外部浏览器的 OAuth 授权 URL。(例如:https://www.pokepay.jp/oauth/authorize?client_id=xxxxxxxx&response_type=code

[方法] OAuthClient.getAccessToken(code: String)

将授权码(重定向 URL 中的字符串)与访问令牌进行交换。

[方法] OAuthClient.refreshAccessToken(refreshToken: String)

使用刷新令牌颁发新的访问令牌。

使用 Request 对象发送请求

所有到 RESTful API 的 HTTP 请求也可以使用在 BankAPI 下定义的请求结构完成(参见 Sources/Pokepay/BankAPI/)。

import Pokepay

// Same as Client.getTerminalInfo
let client = Pokepay.Client(accessToken: "ZhwMsfoAyWZMGrCAKrrofmwYHV82GkUcf3kYSZYYf1oDKVvFAPIKuefyQoc1KDVr")
client.send(BankAPI.Terminal.Get()) { result in
    switch result {
    case .success(let terminal):
        print(terminal)
    case .failure(let error):
        print(error)
    }
}

错误处理

client.send(BankAPI.Terminal.Get()) { result in
    switch result {
    case .success(let response):
        // Success. `response` is a Terminal object.
        print(response)
    case .failure(.responseError(let error as BankAPIError)):
        // Failure with response error. The content of Error object is returned from Bank API.
        switch error {
        case .clientError(let code, let apiError):
            // 4xx error
            print("code: \(code)")
            print("type: \(apiError.type)")
            print("message: \(apiError.message)")
        case .serverError:
            // 5xx error
        default:
            print("Other unknown error.")
        }
    case .failure:
        // Other error, like network disconnected
        print(result)
    }
}

要求

  • Xcode 9 或更高版本
  • iOS 10.0 或更高版本

安装

Carthage

github "pokepay/ios-sdk"

CocoaPods

pod 'Pokepay'

依赖

版权

版权所有(c)2018 Pocket Change, Inc.