COCOS2dX

来源:互联网 发布:非主流签名 知乎 编辑:程序博客网 时间:2024/05/16 03:35
COCOS2dX -集成PayPal
paypal的世界里,他有多种支付方式:
直接支付(single payment):类似国内支付产品,直接对一件或多件商品使用paypal 余额支付
预支付(future payment):创建一个预支付订单,可能以后进行支付
信用卡支付(credit card)paypal SDK提供了一套 card.io的库,可以扫描信用卡,并且使用信用卡直接支付
大多数情况下我们只需要使用直接支付就好,这里用直接支付。
1.PayPal网站注册账号:https://developer.paypal.com
创建 application并获取 CLIENT_ID_FOR_PRODUCTIONCLIENT_ID_FOR_SANDBOX
2.添加paypal SDK 到你的项目
如果你用 cocoapods来管理你的三方库,那么你只需要以下两行代码:
platform :ios, '6.0'
pod 'PayPal-iOS-SDK'
 
如果你不使用 cocoapods,用以下方法
(1)添加paypalmobile目录Xcode项目中(包含.hlibPayPalMobile.a)
(2) 添加-lc++-ObjCOther 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"];
原创粉丝点击