COCOS2dX - 集成PayPa

COCOS2dX - 集成 PayPal
paypal 的世界里,他有多种支付方式:
直接支付 (single payment) :类似国内支付产品,直接对一件或多件商品使用 paypal 余额支付
预支付 (future payment) :创建一个预支付订单,可能以后进行支付
信用卡支付 (credit card) paypal SDK 提供了一套 card.io 的库,可以扫描信用卡,并且使用信用卡直接支付
大多数情况下我们只需要使用直接支付就好,这里用直接支付。
1. PayPal 网站注册账号: https://developer.paypal.com
创建 application 并获取 CLIENT_ID_FOR_PRODUCTION CLIENT_ID_FOR_SANDBOX
2. 添加 paypal SDK 到你的项目
如果你用 cocoapods 来管理你的三方库,那么你只需要以下两行代码:
platform :ios, '6.0'
pod 'PayPal-iOS-SDK'
 
如果你不使用 cocoapods ,用以下方法
(1) 添加 paypalmobile 目录 Xcode 项目中 ( 包含 .h libPayPalMobile.a)
(2) 添加 -lc++ -ObjC Other Linker Flags
Enable Modules (C and Objective-C) 设为 enable
Link Frameworks Automatically 设为 enable
(3) 导入必须的框架:
Accelerate.framework
AudioToolbox.framework
AVFoundation.framework
CoreLocation.framework
CoreMedia.framework
MessageUI.framework
MobileCoreServices.framework
SystemConfiguration.framework
SafariServices.framework
不管你有没有使用 cocoapods ,都需要给 URL Scheme 里面加以下几行 :
com.paypal.ppclient.touch.v1
com.paypal.ppclient.touch.v2
org-appextension-feature-password-management

3 初始化 SDK ,需要你提供 client ID ,这个过程在 AppDelegate.m 里面实现。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ ...
[PayPalMobile initializeWithClientIdsForEnvironments:@{PayPalEnvironmentProduction : @"YOUR_CLIENT_ID_FOR_PRODUCTION",
PayPalEnvironmentSandbox : @"YOUR_CLIENT_ID_FOR_SANDBOX"}];
...
}
4. 结算页面添加代理
//PayPalHelper.h
@interface PayPalHelper : UIViewController<PayPalPaymentDelegate>
    + (PayPalHelper*) getInstance;
    @property(nonatomic, strong, readwrite) NSString *environment;
    - (void)viewDidAppera;
    - (void)viewWillAppear:(BOOL)animated;
    - (void)setPayPalEnvironment:(NSString *)environment;
    - (void)pay:(NSString *)amount;
@end
5. 创建自己创建的类的对象
//PayPalHelper.m
// Set the environment:
// - For live charges, use PayPalEnvironmentProduction (default).
// - To use the PayPal sandbox, use PayPalEnvironmentSandbox.
// - For testing, use PayPalEnvironmentNoNetwork.
#define kPayPalEnvironment PayPalEnvironmentNoNetwork
@interface PayPalHelper()
@property (nonatomic, strong, readwrite) PayPalConfiguration *payPalConfig;
@end

@implementation PayPalHelper

static PayPalHelper*  g_PayPalHelper;
@implementation PayPalHelper
#pragma mark -
#pragma mark getInstance
+ (PayPalHelper*) getInstance {
    if  (g_PayPalHelper == nil){
        g_PayPalHelper = [[PayPalHelper alloc] init];
    }
    return g_PayPalHelper;
}
6. 初始化 PayPalConfiguration
//PayPalHelper.m
- (void)viewDidLoad{
  [super viewDidLoad];
  _payPalConfig = [[PayPalConfiguration alloc] init];
  _payPalConfig.acceptCreditCards = NO;
  _payPalConfig.merchantName = @"Awesome Shirts";
  _payPalConfig.languageOrLocale = [NSLocale preferredLanguages][0];
  _payPalConfig.payPalShippingAddressOption = PayPalShippingAddressOptionPayPal;
  self.environment = kPayPalEnvironment;
  NSLog(@"PayPal iOS SDK version: %@", [PayPalMobile libraryVersion]);
}
7. 建立支付环境,然后和 paypal 服务器进行预连接
//PayPalHelper.m
- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated ];
// Preconnect to PayPal early
[PayPalMobile preconnectWithEnvironment:self.environment]];
}
8.pay 支付
//PayPalHelper.m
- (void)pay:(NSString *)amount{
  NSDecimalNumber *subtotal = [[NSDecimalNumber alloc] initWithString:amount];
  PayPalPayment *payment = [[PayPalPayment alloc] init];
  payment.amount = subtotal;
  payment.currencyCode = @"USD";
  payment.shortDescription = @"Hipster clothing";
  payment.items = nil;  // if not including multiple items, then leave payment.items as nil
payment.paymentDetails = nil; // if not including payment details, then leave payment.paymentDetails as nil
  if (!payment.processable) {
        // This particular payment will always be processable. If, for
        // example, the amount was negative or the shortDescription was
        // empty, this payment wouldn't be processable, and you'd want
        // to handle that here.
  }
  // 下面这一段是为了由控制类调用 paypal 页面的方法
UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
  while (topRootViewController.presentedViewController)
  {
    topRootViewController = topRootViewController.presentedViewController;
  } 
PayPalPaymentViewController *paymentViewController = [[PayPalPaymentViewController alloc] initWithPayment:payment
   configuration:self.payPalConfig
    delegate:self];
  [topRootViewController presentViewController:paymentViewController animated:YES completion:nil];
 
// 这是例子中由页面中的支付按钮调用使用的方法
//    [self presentViewController:paymentViewController animated:YES completion:nil];
}
9. 代理监听
//PayPalHelper.m
#pragma mark - PayPalPaymentDelegate methods
- (void)payPalPaymentViewController:(PayPalPaymentViewController *)paymentViewController
                 didCompletePayment:(PayPalPayment *)completedPayment {
    // Payment was processed successfully; send to server for verification and fulfillment.
    NSLog(@"payPalPaymentViewController");
    [self verifyCompletedPayment:completedPayment];
UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topRootViewController.presentedViewController)
    {
        topRootViewController = topRootViewController.presentedViewController;
    }
    // Dismiss the PayPalPaymentViewController.
    [topRootViewController dismissViewControllerAnimated:YES completion:nil];
}
 
- (void)payPalPaymentDidCancel:(PayPalPaymentViewController *)paymentViewController {
// The payment was canceled; dismiss the PayPalPaymentViewController.
NSLog(@"payPalPaymentDidCancel");
UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topRootViewController.presentedViewController)
    {
        topRootViewController = topRootViewController.presentedViewController;
    }
    [topRootViewController dismissViewControllerAnimated:YES completion:nil];
}
10. 给你的服务器发送支付成功、支付失败等信息请求
//PayPalHelper.m
- (void)verifyCompletedPayment:(PayPalPayment *)completedPayment {
// Send the entire confirmation dictionary
NSData *confirmation = [NSJSONSerialization dataWithJSONObject:completedPayment.confirmation    
options:0                                        
error:nil];
}

@end


11 调用方式
//AppDelegate.m
// 初始化
PayPalHelper *g_PayPalHelper = [PayPalHelper getInstance];
  [g_PayPalHelper viewDidAppera];
[g_PayPalHelper viewWillAppear:YES];
 
// 自己的购买按钮的回调方法中调用 paypal 支付
// 支付
 PayPalHelper *g_PayPalHelper = [PayPalHelper getInstance];
 [g_PayPalHelper pay:@"20"];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值