一、创建并获取对应的ClientID
1、登录paypal开发者网站
PayPal开发者官网地址:https://developer.paypal.com/classic-home/
2、创建App
DASHBOARD -> My Apps -> Creat APP
3、获取ClientID
Sandbox:测试环境
Live:正式环境
获取Sandbox对应的ClientID
二、引入sdk
1、采用CocoaPods
pod 'PayPal-iOS-SDK'
2、注册环境
/// AppDelegate中注册
+ (void)kj_registerPaypal{
NSDictionary *dict = @{PayPalEnvironmentProduction:kPayPalLive,
PayPalEnvironmentSandbox:kPayPalSandbox,
};
[PayPalMobile initializeWithClientIdsForEnvironments:dict];
}
3、支付处理
备注:由于PayPal支付不需要唤起客户端,内部sdk有相对应的控制器处理
/// 支付处理
+ (void)kj_paypalWithViewController:(UIViewController*)vc PaypalInfo:(KJPaypalInfo*)info SuccessBlock:(void(^)(bool success,NSDictionary *dict))block{
KJPaypalTool *tool = [[KJPaypalTool alloc]init];
[tool kj_paypalWithViewController:vc PaypalInfo:info SuccessBlock:block];
}
- (void)kj_paypalWithViewController:(UIViewController*)vc PaypalInfo:(KJPaypalInfo*)info SuccessBlock:(void(^)(bool success,NSDictionary *dict))block{
/// 配置支付环境
[PayPalMobile preconnectWithEnvironment:PayPalEnvironmentSandbox];
/// Paypal配置
PayPalConfiguration *paypalConfig = [[PayPalConfiguration alloc] init];
//是否接受信用卡
paypalConfig.acceptCreditCards = NO;
//商家名称
paypalConfig.merchantName = @"商家名";
//paypal账号下的地址信息
paypalConfig.payPalShippingAddressOption = PayPalShippingAddressOptionPayPal;
//配置语言环境
paypalConfig.languageOrLocale = [NSLocale preferredLanguages][0];
/// 配置支付相关信息
PayPalPayment *payment = [[PayPalPayment alloc] init];
//订单总额
payment.amount = [NSDecimalNumber decimalNumberWithString:info.amount];
//货币类型-RMB是没用的
payment.currencyCode = @"USD";
//订单描述
payment.shortDescription = info.shortDescription;
//生成paypal控制器,并模态出来(push也行)
//将之前生成的订单信息和paypal配置传进来,并设置订单VC为代理
PayPalPaymentViewController *paymentViewController = [[PayPalPaymentViewController alloc] initWithPayment:payment configuration:paypalConfig delegate:self];
[vc presentViewController:paymentViewController animated:YES completion:nil];
}
登录和充值展示
4、监听回调状态
#pragma mark - PayPalPaymentDelegate
//订单支付完成后回调此方法
- (void)payPalPaymentViewController:(PayPalPaymentViewController*)paymentViewController didCompletePayment:(PayPalPayment*)completedPayment {
NSLog(@"PayPal Payment Success!");
}
//用户取消支付回调此方法
- (void)payPalPaymentDidCancel:(PayPalPaymentViewController*)paymentViewController {
NSLog(@"PayPal Payment Canceled");
}
5、查看订单状态
登录到官网:https://www.sandbox.paypal.com/signin