一沐绒、原因
paypal SDK已經(jīng)不再維護(hù)瘫里,并且上架的時(shí)候會被拒提示里面還有UIWebView,
所以替換成Braintree(二者已統(tǒng)一為一家付呕,流程類似).
二、準(zhǔn)備工作
1.?注冊并登錄paypal business賬號
2.?點(diǎn)擊My Apps & Credentials -> create App創(chuàng)建App成功之后你會得到三個(gè)?值Sandbox account告私、Client ID 、secret
3.?注冊并登錄Braintree沙盒注冊鏈接????
4.?進(jìn)入Settings->API keys屿良,點(diǎn)擊Generate New?APIkey生成公鑰屎蜓、私鑰由服務(wù)端生成token,也可以直接點(diǎn)擊Generate New Tokenization Key生成新的Tokenization Keys
5.?把支付方的paypal支付打開點(diǎn)擊Setting->Processing點(diǎn)擊Link Sandbox茂嗓,將上面的三個(gè)值依次填入餐茵,然后點(diǎn)擊link PayPal Sandbox,之后在支付的時(shí)候就可以使用paypal沙盒的買家賬號直接登錄支付了在抛。
三钟病、集成
1.?CocoaPods?引入三方庫
pod?'Braintree'
2.因?yàn)樯婕暗紿5頁面支付,需要設(shè)注冊一個(gè)URL類型1).在Xcode中刚梭,在Project Navigator中點(diǎn)擊您的項(xiàng)目肠阱,然后導(dǎo)航到App Target > Info > URL Types2).點(diǎn)擊[+]添加新的URL類型3).在URL Schemes下,輸入您的應(yīng)用切換返回URL方案朴读。該方案必須以您的應(yīng)用程序的Bundle ID開始屹徘,并致力于Braintree應(yīng)用程序切換返回。例如衅金,如果應(yīng)用程序包ID是com.your-company.Your-App噪伊,那么您的URL方案可能是com.your-company.Your-App.payments。
3.引入頭文件
#import?<BraintreePayPal.h>
#import?<BraintreeCore.h>
4.?在AppDelegate中的application:didFinishLaunchingWithOptions:實(shí)現(xiàn)氮唯,使用setReturnURLScheme:與您在上面設(shè)置的值
[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.進(jìn)行支付
?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 調(diào)用后臺接口鉴吹,傳遞 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用設(shè)置的xxxx.payments中的xxxx一定要與項(xiàng)目的包名一直,否則打包后不會調(diào)起paypal,一直提示支付失敗.(簽名包確認(rèn)下會不會改包名,如果改的話xxxx需要換成修改后的包名).
更多內(nèi)容請關(guān)注小程序 代碼匯集地