Pockyt 0.5.4

Pockyt 0.5.4

fly.zhu 维护。



Pockyt 0.5.4

  • 作者
  • Fly

简介

CocoaPods
这是一个支持主流支付方式,如微信支付、支付宝和 Braintree 等的支付 SDK。

入门

  • 在集成支付之前,请联系 Pockyt 团队创建账户。我们可能需要您提交文档文件。
  • 对于微信支付,在微信开放平台中配置包标识符和通用链接。 官方指南
  • 对于 Braintree,请联系 Pockyt 团队确认支付模式。当与 Braintree 集成时,您需要在官方平台上创建账户并配置。官方指南

安装

  • Pockyt 通过 CocoaPods 提供使用,由于支付方式独立,您可以自由选择要安装和组合的支付方式。Pockyt 默认包含支付宝和微信支付,如果只需这两种方式,则可以这样安装:只需将以下行添加到您的 Podfile 中。
  pod 'Pockyt'
  • 以下是各种支付方法的单独安装形式,作为 'Pockyt/xxx' 子组件下载。请注意,'Pockyt/DropIn' 会自动包含其他 Braintree 支付方式。DropIn 是一种使用官方 UI 库的快速集成方法,但您仍需要从 SDK 中导入子模块,以方便使用。而其他非 DropIn 方法则需要逐个添加每个支付组件。
  pod 'Pockyt/WechatPay'
  pod 'Pockyt/Alipay'
  pod 'Pockyt/DropIn'
  pod 'Pockyt/ApplePay'
  pod 'Pockyt/CardPal'
  pod 'Pockyt/Venmo'
  pod 'Pockyt/ThreeDSecure'
  pod 'Pockyt/DataCollect'

配置

支付宝

  • 在 Xcode 中,选择您的项目设置,选择“TARGETS”选项卡,然后选择“info”选项卡。在“URL Types”部分,添加一个应用标识符的“URL Scheme”,例如“pockyt2alipay”。建议使用独特的标识符,避免与其他商户应用冲突。否则,可能会导致支付宝无法正确地将用户重定向回商户应用。

微信支付

  • 在 Xcode 中,选择您的项目设置,选择“TARGETS”选项卡,然后选择“info”选项卡。在“URL Types”部分,添加已经注册的应用 ID 的“URL Scheme”。
  • 在 Xcode 中,选择您的项目设置,选择“TARGETS”选项卡,然后选择“info”选项卡。在“Queried URL Schemes”部分,添加“weixin”和“weixinULAPI”。
  • 配置应用程序的通用链接。
  1. 根据 Apple 文档 配置应用程序的通用链接。
  2. 在 Xcode 中,启用“Associated Domains”开关并将通用链接域名添加到配置中。
3. 请前往微信开放平台 - 开发者应用注册页面进行注册。注册并选择移动应用配置后,您将立即获得一个可用于开发的App ID。然而,在完成应用程序注册后,它仍然需要经过提交和审查流程。只有通过审查的应用程序才可以正式发布和使用。

集成UI

  • Xcode 12+
  • 最小部署目标为iOS 12.0
  • Swift 5.1+(或Objective-C)

PayPal

  • 在Xcode中,选择您的项目设置,选择“TARGETS”选项卡,然后选择“info”选项卡。在“URL Types”部分,将标识符添加为“braintree”。请注意,“braintree”是一个固定术语,不能更改,因为它将影响Venmo的集成。添加一个带有应用程序标识符的“URL Scheme”,例如“com.yuansfer.msdk.pockyt2braintree”。
  • 您必须在您的应用程序info.plist中将以下内容添加到查询允许列表中
  • 您必须在您的应用程序info.plist中有一个显示名称,以便帮助Venmo识别您的应用程序

Apple Pay

  • 为了在真实设备上使用Apple Pay,您必须在苹果开发者中心配置Apple Pay商户ID和支付处理证书,官方指南
  • 在Xcode中,在项目设置中的“Capabilities”下启用Apple Pay。然后启用两个Apple Pay商户ID。使用包含Apple Pay商户ID的Apple开发团队配对配置文件编译应用程序非常重要。Apple Pay不支持企业配对。

如何使用

Swift语言

  • 首先,在类文件的最上方导入库。
import Pockyt
  • 对于微信支付、支付宝和PayPal,您需要覆盖AppDelegate中的以下方法
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    return Pockyt.shared.handleOpenURL(url)
}
  
@available(iOS 9.0, *)
func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    return Pockyt.shared.handleOpenURL(url)
}

// For WeChat Pay
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    return Pockyt.shared.handleOpenUniversalLink(userActivity)
}
  • 如果您的应用程序已适配SceneDelegate,请在SceneDelegate中配置以下代码
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    if let urlContext = URLContexts.first {
        Pockyt.shared.handleOpenURL(urlContext.url)
    }
}
  
// For WeChat Pay
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    Pockyt.shared.handleOpenUniversalLink(userActivity)
}
  • 在调用Pockyt预付款API(/micropay/v3/prepay或/online/v3/secure-pay)后,创建一个付款对象并调用Pockyt.shared.requestPay方法。
// For Alipay
let payment = Alipay(payInfo: payInfo, fromScheme: "pockyt2alipay")
Pockyt.shared.requestPay(payment) { result in
    print("Paid: \(result.isSuccessful), cancelled: \(result.isCancelled), \(result)")
}

// For WeChat Pay
let request = WechatPayRequest(partnerId: partnerid, prepayId: prepayid, packageValue: package, nonceStr: noncestr, timeStamp: timestamp, sign: sign)
Pockyt.shared.requestPay(WechatPay(request)) { result in
    print("Paid: \(result.isSuccessful), cancelled: \(result.isCancelled), \(result)")
}

// For Drop-in UI
let dropReq = BTDropInRequest()
// BTDropInRequest has many configuration options
// ThreeDSeucre for card, optional
// dropReq.threeDSecureRequest = createThreeDSecure()
let payment = DropInPay(uiViewController: self, clientToken: authorization, dropInRequest: dropReq)
Pockyt.shared.requestPay(payment) { result in
    if let nonce = result.dropInResult?.paymentMethod?.nonce{
        print("Obtained nonce: \(result.isSuccessful), cancelled: \(result.isCancelled), nonce: \(nonce)")
    } else if .applePay == result.dropInResult?.paymentMethodType {
        print(result.respMsg)
        // Note that Apple Pay requires continuing the payment flow initiation
        self.startApplePay()
    } else if let error = result.respMsg {
        print("Failed to obtain nonce, cancelled: \(result.isCancelled), error: \(error)")
    } else {
        print("Failed to obtain nonce, cancelled: \(result.isCancelled)")
    }
}

// For PayPal
let request = BTPayPalCheckoutRequest(amount: "1.00")
let request = BTPayPalVaultRequest()
let paypal = PayPal(authorization: HttpUtils.CLIENT_TOKEN, paypalRequest: request)
Pockyt.shared.requestPay(paypal) { result in
    if result.isSuccessful {
        if let nonce = result.paypalAccountNonce?.nonce {
            print("Obtained nonce: \(nonce)")
        } else {
            print("Failed to obtain nonce")
        }
    } else {
        print("Failed to obtain nonce, error: \(result.respMsg ?? "Unknown error")")
    }
}

// For Venmo
let request = BTVenmoRequest()
request.paymentMethodUsage = .multiUse
let venmo = Venmo(authorization: HttpUtils.CLIENT_TOKEN, venmoRequest: request)
Pockyt.shared.requestPay(venmo) { result in
    if result.isSuccessful {
        if let nonce = result.venmoNonce?.nonce {
            print("Obtained nonce: \(nonce)")
        } else {
            print("Failed to obtain nonce")
        }
    } else {
        print("Failed to obtain nonce, error: \(result.respMsg ?? "Unknown error")")
    }
}

// For Apple Pay, There are a few additional steps in the payment process compared to the ones mentioned above
// First, initialize the Apple Pay request parameters
let applePay = ApplePay(viewController: self, authorization: HttpUtils.CLIENT_TOKEN)
applePay.initPaymentRequest() { paymentRequest, error in
    if let paymentRequest = paymentRequest {
        print("Payment request initialized")
        self.showApplePaySheet(paymentRequest: paymentRequest)
        self.presentAuthorizationViewController(applePay)
    } else {
        print("Failed to initialize payment request")
    }
}
// Secondly, present the Apple Pay sheet
private func showApplePaySheet(paymentRequest: PKPaymentRequest) {
    paymentRequest.requiredBillingContactFields = [.postalAddress]
    // Set other PKPaymentRequest properties here
    paymentRequest.merchantCapabilities = .capability3DS
    paymentRequest.paymentSummaryItems =
    [
        PKPaymentSummaryItem(label: "test_item", amount: NSDecimalNumber(string: "0.02")),
        // Add add'l payment summary items...
        PKPaymentSummaryItem(label: "Pockyt.io", amount: NSDecimalNumber(string: "0.02")),
    ]
    // ...
}
// Then, present the Apple Pay authorization view controller
private func presentAuthorizationViewController(_ applePay: ApplePay) {
    applePay.requestPay() { result in
        if result.isSuccessful {
            print("Payment processing, please wait...")
            self.submitNonceToServer(applePay: applePay, transactionNo: "xxx", nonce: result.applePayNonce!.nonce)
            print("Payment successful, nonce: \(result.applePayNonce!.nonce)")
        } else {
            print(result.respMsg)
        }
    }
}
// Finally, Submit the Apple Pay nonce to your server, Notify the Apple Pay wallet of the payment status based on the API result
if (apiSuccess) {
    applePay.notifyPaymentCompletion(true)
} else {
    applePay.notifyPaymentCompletion(false)
}
  • 对于Braintree,在从支付结果中获取nonce后,调用Pockyt处理API(/creditpay/v3/process)来完成支付。

Objective-C语言

  • 首先,在'.m'文件的最上方导入库。
#import "Pockyt-Swift.h"
  • 请参阅上面Swift的配置,在AppDelegate或SceneDelegate中添加从支付应用程序返回应用程序的配置。
  • 在Objective-C语言中,而不是调用Pockyt的统一requestPay方法,可以直接通过相应的支付实现类的requestPay方法来触发支付。
// For Alipay
Alipay *alipay = [[Alipay alloc] initWithPayInfo:payInfo fromScheme:@"pockytToalipay"];
[alipay requestPayWithCompletion: ^(AlipayResult * result) {
    NSLog(@"Alipay result: %d, %d, %@", result.isSuccessful, result.isCancelled, result.memo);
}];

// For WeChat Pay
WechatPayRequest *request = [[WechatPayRequest alloc] initWithPartnerId:partnerId prepayId:prepayId packageValue:packageValue nonceStr:nonceStr timeStamp:timeStamp sign:sign];
WechatPay *wechatPay = [[WechatPay alloc] init:request];
[wechatPay requestPayWithCompletion:^(WechatPayResult * result) {
    NSLog(@"Wechat Pay result: %d, %d, %@", result.isSuccessful, result.isCancelled, result.respMsg);
}];

// Other payment methods are similar
// ...

注意

  • "deviceData"用于降低退款率。建议使用DataCollector的collectData方法获取数据并提交给服务器进行处理。
  • 当生成客户端令牌时传递'customerNo',Drop-in将显示该客户的已保存付款方式,并将任何新输入的付款方式自动添加到他们的安全记录库中。《创建客户API创建客户API》。如果存在安全记录库中的付款方法,它们将这样显示在Drop-in中。
  • 有关其他详细使用方法,请参阅示例程序。