PayMayaSDK
PayMaya iOS SDK 是一个库,允许您轻松地将信用卡和借记卡添加为移动应用的支付选项。
兼容性
iOS 12.0 或更高版本
集成
通过 CocoaPods
如果你使用 CocoaPods,则将这些行添加到你的 podfile 中
pod 'PayMayaSDK'
通过 Swift 包管理器
.package(url: "https://github.com/PayMaya/PayMaya-iOS-SDK-v2.git")
初始化
通过指定目标环境(沙盒或生产)、可选的日志级别(默认关闭)和您要使用的特定支付方式的 API 密钥(可以有多个)来初始化 SDK。我们建议您在 app 代理的 didFinishLaunchingWithOptions: 方法中完成此操作。
import PayMayaSDK
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
...
PayMayaSDK.setup(environment: .sandbox, logLevel: .all, [
.checkout: "pk-Z0OSzLvIcOI2UIvDhdTGVVfRSSeiGStnceqwUE7n0Ah",
.payments: "pk-MOfNKu3FmHMVHtjyjG7vhr7vFevRkWxmxYL1Yq6iFk5",
.cardToken: "pk-Z0OSzLvIcOI2UIvDhdTGVVfRSSeiGStnceqwUE7n0Ah"
])
}
使用 Checkout
- 创建一个 CheckoutInfo 对象,包含总金额、项目、重定向 URL、可选的买方信息和可选的请求参考号(默认将自动生成)。
let itemsToBuy = [
CheckoutItem(name: "Shoes",
quantity: 1,
totalAmount: CheckoutItemAmount(value: 99)),
CheckoutItem(name: "Pants",
quantity: 1,
totalAmount: CheckoutItemAmount(value: 79)),
]
let totalAmount = CheckoutTotalAmount(value: itemsToBuy.map { $0.totalAmount.value }.reduce(0, +), currency: "PHP")
let redirectUrl = RedirectURL(success: "https://www.merchantsite.com/success",
failure: "https://www.merchantsite.com/failure",
cancel: "https://www.merchantsite.com/cancel")!
let checkoutInfo = CheckoutInfo(totalAmount: totalAmount, items: itemsToBuy, redirectUrl: redirectUrl, requestReferenceNumber: "1551191039")
- 调用 PayMayaSDK.presentCheckout 方法,传递将要展示 checkout 过程的控制器和包含交易详情的 CheckoutInfo 对象。注意:当 id 创建后,首先调用回调,然后在过程完成、发生错误或用户关闭控制器时再次调用回调。
import PayMayaSDK
class SomeViewController: UIViewController {
...
func buyButtonTapped() {
PayMayaSDK.presentCheckout(from: self, checkoutInfo: checkoutInfo) { result in
switch result {
// Called once the checkout id is created
case .prepared(let checkoutId):
// Called once the transaction is finished
case .processed(let status):
// The transaction status with your redirection url provided in the CheckoutInfo object
switch status {
case .success(let url):
case .failure(let url):
case .cancel(let url):
}
// Called when user dismisses the checkout controller, passes the last known status.
case .interrupted(let status):
// For error handling
case .error(let error):
}
}
}
...
}
- (如有必要)检查 checkout id 的状态
import PayMayaSDK
func getStatus() {
PayMayaSDK.getCheckoutStatus(id: "someCheckoutId") { result in
switch result {
// Status for that checkout id
case .success(let status):
// For error handling
case .failure(let error):
}
}
}
使用 Pay with PayMaya
单次支付
- 创建一个 SinglePaymentInfo 对象,包含总金额、重定向 URL 和可选的请求参考号(默认将自动生成)。
let redirectUrl = RedirectURL(success: "https://www.merchantsite.com/success",
failure: "https://www.merchantsite.com/failure",
cancel: "https://www.merchantsite.com/cancel")!
let totalAmount = SinglePaymentTotalAmount(currency: "PHP", value: 199)
let singlePaymentInfo = SinglePaymentInfo(totalAmount: totalAmount, redirectUrl: redirectUrl, requestReferenceNumber: "6319921")
- 调用PayMayaSDK.presentSinglePayment方法,传递将显示支付过程的控制器和包含交易详情的单次支付信息对象。注意:id创建后被调用的回调第一个,完成过程、发生错误或用户关闭控制器后,将再次调用。
import PayMayaSDK
class SomeViewController: UIViewController {
...
func payButtonTapped() {
PayMayaSDK.presentSinglePayment(from: self, singlePaymentInfo: singlePaymentInfo) { result in
switch result {
// Called once the payment id is created
case .prepared(let paymentId):
// Called once the transaction is finished
case .processed(let status):
// The transaction status with your redirection url provided in the SinglePaymentInfo object
switch status {
case .success(let url):
case .failure(let url):
case .cancel(let url):
}
// Called when user dismisses the payment controller, passes the last known status.
case .interrupted(let status):
// For error handling
case .error(let error):
}
}
}
...
}
- (如果需要) 检查支付id的状态
import PayMayaSDK
func getStatus() {
PayMayaSDK.getPaymentStatus(id: "somePaymentId") { result in
switch result {
// Status for that payment id
case .success(let status):
// For error handling
case .failure(let error):
}
}
}
创建钱包链接
- 创建一个包含重定向URLs以及可选的请求参考号(默认为自动生成)的WalletLinkInfo对象。
let redirectUrl = RedirectURL(success: "https://www.merchantsite.com/success",
failure: "https://www.merchantsite.com/failure",
cancel: "https://www.merchantsite.com/cancel")!
let walletLinkInfo = WalletLinkInfo(redirectUrl: redirectUrl, requestReferenceNumber: "123456")
- 调用PayMayaSDK.presentCreateWalletLink方法,传递将显示钱包链接创建过程的控制器以及WalletLinkInfo对象。注意:id创建后被调用的回调第一个,完成过程、发生错误或用户关闭控制器后,将再次调用。
import PayMayaSDK
class SomeViewController: UIViewController {
...
func createWalletButtonTapped() {
PayMayaSDK.presentCreateWalletLink(from: self, walletLinkInfo: walletLinkInfo) { result in
switch result {
// Called once the wallet link id is created
case .prepared(let walletId):
// Called once the wallet link is created
case .processed(let status):
// The transaction status with your redirection url provided in the WalletLinkInfo object
switch status {
case .success(let url):
case .failure(let url):
case .cancel(let url):
}
// Called when user dismisses the controller.
case .interrupted:
// For error handling
case .error(let error):
}
}
}
...
}
使用支付保险库
支付保险库为商家提供存储客户卡详细信息并在需要时进行支付的能力
创建支付令牌
调用PayMayaSDK.presentCardPayment方法,传递将显示卡支付过程的控制器。
import PayMayaSDK
class SomeViewController: UIViewController {
...
func payButtonTapped() {
PayMayaSDK.presentCardPayment(from: self) { result in
switch result {
// Called once the payment token is created
case .success(let token):
// For error handling
case .error(let error):
}
}
}
...
}
可选的样式
调用方法并选择使用自定义字体、标志和“立即支付”按钮样式的 .dark / .light。所有参数都是可选的,所以您只需更改所需的参数。
let tintColor = UIColor.green
let font = UIFont.systemFont(ofSize: 18)
let logo = UIImage(named: "myLogo")
let buttonStyling = PayButtonStyling(title: "Pay with card", backgroundColor: .blue, textColor: .white)
PayMayaSDK.presentCardPayment(from: self, styling: .dark(buttonStyling: buttonStyling, font: font, logo: logo)) { result in
...
}