iOS 集成 Braintree (Paypal) 支付

2020-09-26  本文已影响0人  93b3d3ccb7e6

我这里用到的是 自定义 UI 以及 一次性付款,也就是官方所说的 Checkout with PayPal

参考的官方开发文档:

https://developers.braintreepayments.com/guides/paypal/client-side/ios/v4

https://developers.braintreepayments.com/guides/paypal/client-side/ios/v4#setup-for-app-switch

Step1、注册以及配置沙盒账号

链接:https://www.braintreepayments.com/sandbox

注册并登录完成之后,点击右上角的 设置 - API,然后 点击按钮 生成一个 token,用于 iOS 项目中,(用谷歌浏览器点击设置无响应时,可以试试 Safari ) 操作如下图:

image1.png image2.png

Step2、下载 SDK

链接:https://github.com/braintree/braintree_ios

从 github 下载代码之后,导入 BraintreeCoreBraintreePaypal 文件夹到自己的项目中。

也可以使用 cocoapods 或其他方式,参考链接:https://developers.braintreepayments.com/start/hello-client/ios/v4#ios-sdk-setup

Step3、配置 URL Schemes

格式:Bundle Identifier + .payments

在 TARGETS - Info - UTL Types 中添加 URL Schemes: com.hope.xxxx.payments

Step4、编码

1、在 AppDelegate.m 中添加如下代码:

#import "BTAppSwitch.h"

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    [BTAppSwitch setReturnURLScheme:@"com.hope.xxxx.payments"];
    
    return YES;
} 

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    if ([url.scheme localizedCaseInsensitiveCompare:@"com.hope.xxxx.payments"] == NSOrderedSame) {
        return [BTAppSwitch handleOpenURL:url options:options];
    }
    
    return NO;
}

2、在购买界面调用 Api 发送请求

#import "BraintreePayPal.h"

@interface WebShopViewController ()<BTViewControllerPresentingDelegate> 

@property (strong, nonatomic) BTPayPalDriver *payPalDriver;

@end

@implementation WebShopViewController

- (BTPayPalDriver *)payPalDriver
{
    if (!_payPalDriver) {
        //TODO 替换 token
        BTAPIClient *braintreeClient = [[BTAPIClient alloc] initWithAuthorization:@"sandbox_pgqt98zr_473xx9pwmbkmf3dh"];
        _payPalDriver = [[BTPayPalDriver alloc] initWithAPIClient:braintreeClient];
        _payPalDriver.viewControllerPresentingDelegate = self; 
    }
    return _payPalDriver;
}

- (void)paypalBtnOnClick
{
    NSString *price = @"19.9";
    NSString *orderNo = @"12xxxxxxxxxx38"; 
    BTPayPalRequest *request= [[BTPayPalRequest alloc] initWithAmount:price];
    request.currencyCode = @"USD";  
    
    BTPayPalLineItem *item = [[BTPayPalLineItem alloc] initWithQuantity:@"1" unitAmount:price name:@"商品名称" kind:BTPayPalLineItemKindDebit];
    item.productCode = orderNo; //订单编号
    request.lineItems = @[item];
    
    [self.payPalDriver requestOneTimePayment:request completion:^(BTPayPalAccountNonce * _Nullable tokenizedPayPalAccount, NSError * _Nullable error) {
        
        if (tokenizedPayPalAccount) {//支付成功
            //TODO 调用后台接口,传递 tokenizedPayPalAccount.nonce 和 orderNo
             
        } else if (error) {
            // Handle error here...
            NSLog(@"支付失败 :%@", error);
            
        } else {
            // Buyer canceled payment approval
            NSLog(@"支付 取消");
        }
    }];
}
 
#pragma mark - BTViewControllerPresentingDelegate
// Required
- (void)paymentDriver:(id)paymentDriver requestsPresentationOfViewController:(UIViewController *)viewController
{ 
    [self presentViewController:viewController animated:YES completion:nil];
}

// Required
- (void)paymentDriver:(id)paymentDriver requestsDismissalOfViewController:(UIViewController *)viewController
{
    [viewController dismissViewControllerAnimated:YES completion:nil];
}

😄 😄 这两天搞这个踩了一些坑,写得不好请指教!
最后附上 Demo 链接:https://github.com/hw20101101/TestBraintree

上一篇下一篇

猜你喜欢

热点阅读