为凭证 IOS SDK 2.1.6 设置 Paypal 客户端 ID

Posted

技术标签:

【中文标题】为凭证 IOS SDK 2.1.6 设置 Paypal 客户端 ID【英文标题】:Setup Paypal client ID for credentials IOS SDK 2.1.6 【发布时间】:2014-09-30 13:57:12 【问题描述】:

我在我的应用中使用 Paypal SDK。我将我的环境设置为 PayPalEnvironmentSandbox,但是当我运行我的应用程序时,会出现一条警报,提示“与 Paypal 服务器通信时出现问题。请重试”。我用谷歌搜索了很多次,其他人说我需要更改客户端 ID,但我不知道如何更改客户端 ID,我在 paypal 的沙盒上有一个测试帐户,但我不知道如何在我的应用程序中实现它们。我阅读了文档,他们说我需要更改可能的客户 ID,但我不知道如何。需要你们的帮助,谢谢。我正在使用 ios paypal SDK 2.1.6。

代码:

#import "ZZMainViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "PayPalMobile.h"

// Set the environment:
// - For live charges, use PayPalEnvironmentProduction (default).
// - To use the PayPal sandbox, use PayPalEnvironmentSandbox.
// - For testing, use PayPalEnvironmentNoNetwork.
//#define kPayPalEnvironment PayPalEnvironmentNoNetwork
#define kPayPalEnvironment PayPalEnvironmentSandbox

@interface ZZMainViewController ()

@property(nonatomic, strong, readwrite) IBOutlet UIButton *payNowButton;
@property(nonatomic, strong, readwrite) IBOutlet UIButton *payFutureButton;
@property(nonatomic, strong, readwrite) IBOutlet UIView *successView;

@property(nonatomic, strong, readwrite) PayPalConfiguration *payPalConfig;

@end

@implementation ZZMainViewController

- (void)viewDidLoad 
  [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

  // Set up payPalConfig
  _payPalConfig = [[PayPalConfiguration alloc] init];
  _payPalConfig.acceptCreditCards = YES;
  _payPalConfig.languageOrLocale = @"en";
  _payPalConfig.merchantName = @"Awesome Shirts, Inc.";
  _payPalConfig.merchantPrivacyPolicyURL = [NSURL URLWithString:@"https://www.paypal.com/webapps/mpp/ua/privacy-full"];
  _payPalConfig.merchantUserAgreementURL = [NSURL URLWithString:@"https://www.paypal.com/webapps/mpp/ua/useragreement-full"];


  // Setting the languageOrLocale property is optional.
  //
  // If you do not set languageOrLocale, then the PayPalPaymentViewController will present
  // its user interface according to the device's current language setting.
  //
  // Setting languageOrLocale to a particular language (e.g., @"es" for Spanish) or
  // locale (e.g., @"es_MX" for Mexican Spanish) forces the PayPalPaymentViewController
  // to use that language/locale.
  //
  // For full details, including a list of available languages and locales, see PayPalPaymentViewController.h.

    _payPalConfig.languageOrLocale = [NSLocale preferredLanguages][0];


  // Do any additional setup after loading the view, typically from a nib.

  self.successView.hidden = YES;



  // use default environment, should be Production in real life
  //self.environment = kPayPalEnvironment;
   //self.environment = PayPalEnvironmentSandbox;
  [PayPalMobile preconnectWithEnvironment:PayPalEnvironmentSandbox];
  NSLog(@"PayPal iOS SDK version: %@", [PayPalMobile libraryVersion]);
 [self pay];


- (void)viewWillAppear:(BOOL)animated 
  [super viewWillAppear:YES];

  // Preconnect to PayPal early
  [PayPalMobile preconnectWithEnvironment:PayPalEnvironmentSandbox];


#pragma mark - Receive Single Payment

- (void)pay 
  // Remove our last completed payment, just for demo purposes.
  self.resultText = nil;

  // Note: For purposes of illustration, this example shows a payment that includes
  //       both payment details (subtotal, shipping, tax) and multiple items.
  //       You would only specify these if appropriate to your situation.
  //       Otherwise, you can leave payment.items and/or payment.paymentDetails nil,
  //       and simply set payment.amount to your total charge.

  // Optional: include multiple items
  PayPalItem *item1 = [PayPalItem itemWithName:@"Old jeans with holes"
                                  withQuantity:2
                                     withPrice:[NSDecimalNumber decimalNumberWithString:@"84.99"]
                                  withCurrency:@"USD"
                                       withSku:@"Hip-00037"];
  PayPalItem *item2 = [PayPalItem itemWithName:@"Free rainbow patch"
                                  withQuantity:1
                                     withPrice:[NSDecimalNumber decimalNumberWithString:@"0.00"]
                                  withCurrency:@"USD"
                                       withSku:@"Hip-00066"];
  PayPalItem *item3 = [PayPalItem itemWithName:@"Long-sleeve plaid shirt (mustache not included)"
                                  withQuantity:1
                                     withPrice:[NSDecimalNumber decimalNumberWithString:@"37.99"]
                                  withCurrency:@"USD"
                                       withSku:@"Hip-00291"];
  NSArray *items = @[item1, item2, item3];
  NSDecimalNumber *subtotal = [PayPalItem totalPriceForItems:items];

  // Optional: include payment details
  NSDecimalNumber *shipping = [[NSDecimalNumber alloc] initWithString:@"5.99"];
  NSDecimalNumber *tax = [[NSDecimalNumber alloc] initWithString:@"2.50"];
  PayPalPaymentDetails *paymentDetails = [PayPalPaymentDetails paymentDetailsWithSubtotal:subtotal
                                                                             withShipping:shipping
                                                                                  withTax:tax];

  NSDecimalNumber *total = [[subtotal decimalNumberByAdding:shipping] decimalNumberByAdding:tax];

  PayPalPayment *payment = [[PayPalPayment alloc] init];
  payment.amount = total;
  payment.currencyCode = @"USD";
  payment.shortDescription = @"Hipster clothing";
  payment.items = items;  // if not including multiple items, then leave payment.items as nil
  payment.paymentDetails = paymentDetails; // 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.
  

  // Update payPalConfig re accepting credit cards.
  self.payPalConfig.acceptCreditCards = self.acceptCreditCards;

  PayPalPaymentViewController *paymentViewController = [[PayPalPaymentViewController alloc] initWithPayment:payment
                                                                                              configuration:self.payPalConfig
                                                                                                   delegate:self];
  [self presentViewController:paymentViewController animated:YES completion:nil];



#pragma mark PayPalPaymentDelegate methods

- (void)payPalPaymentViewController:(PayPalPaymentViewController *)paymentViewController didCompletePayment:(PayPalPayment *)completedPayment 
  NSLog(@"PayPal Payment Success!");
  self.resultText = [completedPayment description];
  [self showSuccess];

  [self sendCompletedPaymentToServer:completedPayment]; // Payment was processed successfully; send to server for verification and fulfillment
  [self dismissViewControllerAnimated:YES completion:nil];


- (void)payPalPaymentDidCancel:(PayPalPaymentViewController *)paymentViewController 
  NSLog(@"PayPal Payment Canceled");
  self.resultText = nil;
  self.successView.hidden = YES;
  [self dismissViewControllerAnimated:YES completion:nil];


#pragma mark Proof of payment validation

- (void)sendCompletedPaymentToServer:(PayPalPayment *)completedPayment 
  // TODO: Send completedPayment.confirmation to server
  NSLog(@"Here is your proof of payment:\n\n%@\n\nSend this to your server for confirmation and fulfillment.", completedPayment.confirmation);



#pragma mark - Authorize Future Payments

- (IBAction)getUserAuthorization:(id)sender 

  PayPalFuturePaymentViewController *futurePaymentViewController = [[PayPalFuturePaymentViewController alloc] initWithConfiguration:self.payPalConfig delegate:self];
  [self presentViewController:futurePaymentViewController animated:YES completion:nil];



#pragma mark PayPalFuturePaymentDelegate methods

- (void)payPalFuturePaymentViewController:(PayPalFuturePaymentViewController *)futurePaymentViewController didAuthorizeFuturePayment:(NSDictionary *)futurePaymentAuthorization 
  NSLog(@"PayPal Future Payment Authorization Success!");
  self.resultText = futurePaymentAuthorization[@"code"];
  [self showSuccess];

  [self sendAuthorizationToServer:futurePaymentAuthorization];
  [self dismissViewControllerAnimated:YES completion:nil];


- (void)payPalFuturePaymentDidCancel:(PayPalFuturePaymentViewController *)futurePaymentViewController 
  NSLog(@"PayPal Future Payment Authorization Canceled");
  self.successView.hidden = YES;
  [self dismissViewControllerAnimated:YES completion:nil];


- (void)sendAuthorizationToServer:(NSDictionary *)authorization 
  // TODO: Send authorization to server
  NSLog(@"Here is your authorization:\n\n%@\n\nSend this to your server to complete future payment setup.", authorization);



#pragma mark - Helpers

- (void)showSuccess 
  self.successView.hidden = NO;
  self.successView.alpha = 1.0f;
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:0.5];
  [UIView setAnimationDelay:2.0];
  self.successView.alpha = 0.0f;
  [UIView commitAnimations];


#pragma mark - Flipside View Controller

- (void)flipsideViewControllerDidFinish:(ZZFlipsideViewController *)controller 
  if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
    [self dismissViewControllerAnimated:YES completion:nil];
   else 
    [self.flipsidePopoverController dismissPopoverAnimated:YES];
    self.flipsidePopoverController = nil;
  


- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController 
  self.flipsidePopoverController = nil;


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
  if ([[segue identifier] isEqualToString:@"pushSettings"]) 
    [[segue destinationViewController] setDelegate:self];

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) 
      UIPopoverController *popoverController = [(UIStoryboardPopoverSegue *)segue popoverController];
      self.flipsidePopoverController = popoverController;
      popoverController.delegate = self;
    
  


- (IBAction)togglePopover:(id)sender 
  if (self.flipsidePopoverController) 
    [self.flipsidePopoverController dismissPopoverAnimated:YES];
    self.flipsidePopoverController = nil;
   else 
    [self performSegueWithIdentifier:@"showAlternate" sender:sender];
  


@end

【问题讨论】:

【参考方案1】:

要获得 client_id,您需要在 https://developer.paypal.com/webapps/developer/applications/myapps 注册并创建一个应用程序。然后,您将获得沙盒(和实时)凭据。

【讨论】:

我已经完成了那个先生,但是我不知道如何在应用程序中更改 client_id。【参考方案2】:

这里是来自 PayPal 的戴夫。

要设置您的客户端 ID(一个用于 Sandbox,一个用于 Live),请参阅我们的Sample Code 中的第 1 步:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    // ...
    [PayPalMobile initializeWithClientIdsForEnvironments:@PayPalEnvironmentProduction : @"YOUR_CLIENT_ID_FOR_PRODUCTION",
                                                     PayPalEnvironmentSandbox : @"YOUR_CLIENT_ID_FOR_SANDBOX"];
  // ...
    return YES;

【讨论】:

谢谢你,我会试试这个:) @dave 我们计划使用新的 paypal ios sdk 来处理诸如用户会为他的问题的一些答案付费的东西,即用户提出了问题,如果有人会回答这个问题,我们会通过应用程序支付一些钱。付款将通过调解员进行,即我们将从用户那里拿钱,然后将钱转给回答的人,这会通过 APPLE 审核流程吗?你能进一步帮助我吗? @AniShroff 我很乐意帮助解决涉及我们的 PayPal iOS SDK 的技术问题。但我无法就 Apple 的应用审核流程向您提供建议。 @DaveGoldman 有没有办法就此事获得建议?我已经尝试搜索网络,但无法获得任何详细信息。 @Dave 我可以在 paypal 处理付款之前获取交易密钥吗?【参考方案3】:

您必须在 appdelegate.m 文件中进行更改。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary      *)launchOptions  #warning "Enter your credentials"
  [PayPalMobile initializeWithClientIdsForEnvironments:@PayPalEnvironmentProduction :    @"YOUR_CLIENT_ID_FOR_PRODUCTION",
  PayPalEnvironmentSandbox : @"ATIXyxAAjtL8-HdqfLq0kTCeefUi1SNI_xkfWktHelloqznRxsrm_Hello"]; return YES;

【讨论】:

【参考方案4】:

用于 Swift 开发

    [PayPalMobile initializeWithClientIdsForEnvironments:@PayPalEnvironmentProduction                             :@"YOUR_CLIENT_ID_FOR_PRODUCTION",
      PayPalEnvironmentSandbox : @"ATIXyxAAjtL8-HdqfLq0kTCeefUi1SNI_xkfWktHelloqznRxsrm_Hello"];

你需要做的不会工作

 PayPalMobile.initializeWithClientIdsForEnvironments([PayPalEnvironmentProduction: "ID1", PayPalEnvironmentSandbox: "ID2"])

【讨论】:

以上是关于为凭证 IOS SDK 2.1.6 设置 Paypal 客户端 ID的主要内容,如果未能解决你的问题,请参考以下文章

CredentialsError:无法从适用于 Javascript 的 AWS-SDK v2 中的 ChainableTemporaryCredentials 加载凭证

如何使用 XCode 4 + 基本 SDK 4.3 为 iOS 4.0 设备构建?

Amazon SDK - 临时凭证和 AssumeRoleRequest

如何从 CloudFoundry 向 AWS-sdk 提供凭证

从 SDK 获取 Paypal API 凭证

无法从 /.aws/credentials 读取凭证 - PHP 脚本调用 AWS-SDK