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

时间:2014-08-07 10:51:18

标签: ios xcode paypal paypal-sandbox

我在我的应用程序中使用Paypal SDK。我将我的环境设置为PayPalEnvironmentSandbox,但是当我运行我的应用程序时,会发出警告说#34;与Paypal服务器通信时出现问题。请再试一次"。我google了很多次,其他说我需要更改clientId但我不知道如何更改客户端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

4 个答案:

答案 0 :(得分:1)

戴夫来自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;
}

答案 1 :(得分:1)

对于Swift开发

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

无论你需要做什么工作

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

你必须在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;}