Alvin John Tandoc Alvin John Tandoc - 4 months ago 26x
iOS Question

Setup Paypal client ID for credentials IOS SDK 2.1.6

Im using Paypal SDK in my app. I set my environment into PayPalEnvironmentSandbox but when I run my app theres an alert saying "There was a problem communicating with the Paypal servers. Please Try again". I googled a lot of times, other said that I need to change the clientId but i dont know how to change the client Id, I have a test account on sandbox in paypal but i dont know how to implement them in my app. I read the documentation and they said that I need to change may Client Id something like that but i dont know how. Need your help guys thank you. I am using 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;


@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:@""];
_payPalConfig.merchantUserAgreementURL = [NSURL URLWithString:@""];

// 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"
withPrice:[NSDecimalNumber decimalNumberWithString:@"84.99"]
PayPalItem *item2 = [PayPalItem itemWithName:@"Free rainbow patch"
withPrice:[NSDecimalNumber decimalNumberWithString:@"0.00"]
PayPalItem *item3 = [PayPalItem itemWithName:@"Long-sleeve plaid shirt (mustache not included)"
withPrice:[NSDecimalNumber decimalNumberWithString:@"37.99"]
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

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
[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];



Dave from PayPal here.

To set your client IDs (one for Sandbox, one for Live), see step 1 in our Sample Code:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    // ...
    [PayPalMobile initializeWithClientIdsForEnvironments:@{PayPalEnvironmentProduction : @"YOUR_CLIENT_ID_FOR_PRODUCTION",
                                                     PayPalEnvironmentSandbox : @"YOUR_CLIENT_ID_FOR_SANDBOX"}];
  // ...
    return YES;