一、原因
paypal SDK已经不再维护,并且上架的时候会被拒提示里面还有UIWebView,
所以替换成Braintree(二者已统一为一家,流程类似).
二、准备工作
1. 注册并登录paypal business账号
2. 点击My Apps & Credentials -> create App创建App成功之后你会得到三个 值Sandbox account、Client ID 、secret
3. 注册并登录Braintree沙盒注册链接
4. 进入Settings->API keys,点击Generate New APIkey生成公钥、私钥由服务端生成token,也可以直接点击Generate New Tokenization Key生成新的Tokenization Keys
5. 把支付方的paypal支付打开点击Setting->Processing点击Link Sandbox,将上面的三个值依次填入,然后点击link PayPal Sandbox,之后在支付的时候就可以使用paypal沙盒的买家账号直接登录支付了。
三、集成
1. CocoaPods 引入三方库
pod 'Braintree'
2.因为涉及到H5页面支付,需要设注册一个URL类型1).在Xcode中,在Project Navigator中点击您的项目,然后导航到App Target > Info > URL Types2).点击[+]添加新的URL类型3).在URL Schemes下,输入您的应用切换返回URL方案。该方案必须以您的应用程序的Bundle ID开始,并致力于Braintree应用程序切换返回。例如,如果应用程序包ID是com.your-company.Your-App,那么您的URL方案可能是com.your-company.Your-App.payments。
3.引入头文件
#import <BraintreePayPal.h>
#import <BraintreeCore.h>
4. 在AppDelegate中的application:didFinishLaunchingWithOptions:实现,使用setReturnURLScheme:与您在上面设置的值
[BTAppSwitch setReturnURLScheme:@"Bundle ID.payments"];
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
if ([url.scheme localizedCaseInsensitiveCompare:@"com.your-company.Your-App.payments"] == NSOrderedSame) {
return [BTAppSwitch handleOpenURL:url options:options];
}
return NO;}
// If you support iOS 7 or 8, add the following method.- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
if ([url.scheme localizedCaseInsensitiveCompare:@"com.your-company.Your-App.payments"] == NSOrderedSame) {
return [BTAppSwitch handleOpenURL:url sourceApplication:sourceApplication];
}
return NO;}
5.通过接口获取braintreeToken
6.进行支付
NSString*moneys = minstr([detaiDic valueForKey:@"money"]);
BTAPIClient *braintreeClient = [[BTAPIClient alloc] initWithAuthorization:btcliceToken];
if(!braintreeClient){
//clientToken无效
return;
}
BTPayPalDriver *dribet = [[BTPayPalDriver alloc]initWithAPIClient:braintreeClient];
BTPayPalRequest *request = [[BTPayPalRequest alloc]initWithAmount:moneys];
request.currencyCode = @"USD";//货币类型
request.localeCode = @"zh_CN";//语言
BTPayPalLineItem *item = [[BTPayPalLineItem alloc] initWithQuantity:@"1" unitAmount:moneys name:minstr([detaiDic valueForKey:@"desc"]) kind:BTPayPalLineItemKindDebit];
item.productCode = orderid; //订单编号
request.lineItems = @[item];
[dribet requestOneTimePayment:request completion:^(BTPayPalAccountNonce * _NullabletokenizedPayPalAccount, NSError * _Nullable error) {
if (tokenizedPayPalAccount) {//支付成功
//TODO 调用后台接口,传递 tokenizedPayPalAccount.nonce 和 orderNo
[self uploadonce:tokenizedPayPalAccount.nonce andorderid:orderid andmoney:moneys];
} else if (error) {
// Handle error here...
NSLog(@"支付失败 :%@", error);
if ([self.delegate respondsToSelector:@selector(paypalFaile:)]) {
[self.delegate paypalFaile:1];
}
} else {
// Buyer canceled payment approval
NSLog(@"支付 取消");
if ([self.delegate respondsToSelector:@selector(paypalFaile:)]) {
[self.delegate paypalFaile:2];
}
}
NSLog(@"获取cc===%@===%@",tokenizedPayPalAccount.nonce,error);
}];
注意:URL Schemes添加的xxxx.payments以及在AppDelegate用设置的xxxx.payments中的xxxx一定要与项目的包名一直,否则打包后不会调起paypal,一直提示支付失败.(签名包确认下会不会改包名,如果改的话xxxx需要换成修改后的包名).
更多内容请关注小程序 代码汇集地