简体   繁体   English

将Paypal SDK与本机iOS应用程序集成

[英]Integrating Paypal SDK with native iOS App

I am referring and integrating PayPal-iOS-SDK with my native iOS app. 我指的是将PayPal-iOS-SDK与本机iOS应用集成。

I have successfully integrated the app(this is what I feel ) 我已经成功集成了应用程序(这就是我的感受)

Please refer the code 请参考代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        PayPalMobile .initializeWithClientIdsForEnvironments([PayPalEnvironmentProduction: "YOUR_CLIENT_ID_FOR_PRODUCTION",
            PayPalEnvironmentSandbox: "SANDBOX CODE"])
        return true
    }

View Controller

//
//  ViewController.swift
//  PaypalDemo
//

import UIKit

class ViewController: UIViewController,PayPalPaymentDelegate  {

    var environment:String = PayPalEnvironmentNoNetwork {
        willSet(newEnvironment) {
            if (newEnvironment != environment) {
                PayPalMobile.preconnectWithEnvironment(newEnvironment)
            }
        }
    }



    #if HAS_CARDIO
    // You should use the PayPal-iOS-SDK+card-Sample-App target to enable this setting.
    // For your apps, you will need to link to the libCardIO and dependent libraries. Please read the README.md
    // for more details.
    var acceptCreditCards: Bool = true {
    didSet {
    payPalConfig.acceptCreditCards = acceptCreditCards
    }
    }
    #else
    var acceptCreditCards: Bool = false {
        didSet {
            payPalConfig.acceptCreditCards = acceptCreditCards
        }
    }
    #endif


    var resultText = "" // empty
    var payPalConfig = PayPalConfiguration() // default



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // Do any additional setup after loading the view.
        title = "PayPal SDK Demo"
        //successView.hidden = true

        // Set up payPalConfig
        print("this is credit card status \(acceptCreditCards)")
        payPalConfig.acceptCreditCards = true;
        payPalConfig.merchantName = "Awesome Shirts, Inc."
        payPalConfig.merchantPrivacyPolicyURL = NSURL(string: "https://www.paypal.com/webapps/mpp/ua/privacy-full")
        payPalConfig.merchantUserAgreementURL = NSURL(string: "https://www.paypal.com/webapps/mpp/ua/useragreement-full")



        payPalConfig.languageOrLocale = NSLocale.preferredLanguages()[0]

        // Setting the payPalShippingAddressOption property is optional.
        //
        // See PayPalConfiguration.h for details.

        payPalConfig.payPalShippingAddressOption = .PayPal;

        print("PayPal iOS SDK Version: \(PayPalMobile.libraryVersion())")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        PayPalMobile.preconnectWithEnvironment(environment)
    }


    @IBAction func payByPaypal(sender: UIButton) {
        print("integrate paypal here")
        resultText = ""

        // 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
        let item1 = PayPalItem(name: "Old jeans with holes", withQuantity: 2, withPrice: NSDecimalNumber(string: "84.99"), withCurrency: "USD", withSku: "Hip-0037")
        let item2 = PayPalItem(name: "Free rainbow patch", withQuantity: 1, withPrice: NSDecimalNumber(string: "0.00"), withCurrency: "USD", withSku: "Hip-00066")
        let item3 = PayPalItem(name: "Long-sleeve plaid shirt (mustache not included)", withQuantity: 1, withPrice: NSDecimalNumber(string: "37.99"), withCurrency: "USD", withSku: "Hip-00291")

        let items = [item1, item2, item3]
        let subtotal = PayPalItem.totalPriceForItems(items)

        // Optional: include payment details
        let shipping = NSDecimalNumber(string: "5.99")
        let tax = NSDecimalNumber(string: "2.50")
        let paymentDetails = PayPalPaymentDetails(subtotal: subtotal, withShipping: shipping, withTax: tax)

        let total = subtotal.decimalNumberByAdding(shipping).decimalNumberByAdding(tax)

        let payment = PayPalPayment(amount: total, currencyCode: "AUD", shortDescription: "My Shop", intent: .Sale)

        payment.items = items
        payment.paymentDetails = paymentDetails

        if (payment.processable) {
            let paymentViewController = PayPalPaymentViewController(payment: payment, configuration: payPalConfig, delegate: self)
            presentViewController(paymentViewController!, animated: true, completion: nil)
        }
        else {
            // 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.
            print("Payment not processalbe: \(payment)")
        }
    }


    func payPalPaymentDidCancel(paymentViewController: PayPalPaymentViewController) {
        print("PayPal Payment Cancelled")
        resultText = ""
        // successView.hidden = true
        paymentViewController.dismissViewControllerAnimated(true, completion: nil)
    }

    func payPalPaymentViewController(paymentViewController: PayPalPaymentViewController, didCompletePayment completedPayment: PayPalPayment) {
        print("PayPal Payment Success !")
        paymentViewController.dismissViewControllerAnimated(true, completion: { () -> Void in
            // send completed confirmaion to your server
          //  print("Here is your proof of payment:\n\n\(completedPayment.confirmation)\n\nSend this to your server for confirmation and fulfillment.")

            self.resultText = completedPayment.description
            self.showSuccess(completedPayment.confirmation)
        })
    }

    func showSuccess(inputString: NSObject) {

        print("this is done successfully :) ")
        print("send this to server \(inputString)")
    }



}

My concerns regarding the app is 我对应用程序的担心是

1.Whatever login I am using I am able to login . 1.无论我使用什么登录名,我都可以登录。 I have tried tim@cook.com as email and 1234 as password, still I was able to login. 我已经尝试将tim@cook.com作为电子邮件,将1234作为密码,但仍然可以登录。

2.I have tried using credit card as 4242-4242-4242-4242, cvv 123 12/12 and still payment was successful. 2.我曾尝试使用信用卡4242-4242-4242-4242,cvv 123 12/12进行付款,但仍然成功。

3.These are responses I am getting [client: { environment = mock; "paypal_sdk_version" = "2.14.3"; platform = iOS; "product_name" = "PayPal iOS SDK"; }, response_type: payment, response: { "create_time" = "2016-08-01T09:16:40Z"; id = "PAY-NONETWORKPAYIDEXAMPLE123"; intent = sale; state = approved; }] 3.这些是我得到的回复[client: { environment = mock; "paypal_sdk_version" = "2.14.3"; platform = iOS; "product_name" = "PayPal iOS SDK"; }, response_type: payment, response: { "create_time" = "2016-08-01T09:16:40Z"; id = "PAY-NONETWORKPAYIDEXAMPLE123"; intent = sale; state = approved; }] [client: { environment = mock; "paypal_sdk_version" = "2.14.3"; platform = iOS; "product_name" = "PayPal iOS SDK"; }, response_type: payment, response: { "create_time" = "2016-08-01T09:16:40Z"; id = "PAY-NONETWORKPAYIDEXAMPLE123"; intent = sale; state = approved; }] [client: { environment = mock; "paypal_sdk_version" = "2.14.3"; platform = iOS; "product_name" = "PayPal iOS SDK"; }, response_type: payment, response: { "create_time" = "2016-08-01T09:16:40Z"; id = "PAY-NONETWORKPAYIDEXAMPLE123"; intent = sale; state = approved; }] for another, { client = { environment = mock; "paypal_sdk_version" = "2.14.3"; platform = iOS; "product_name" = "PayPal iOS SDK"; }; response = { "create_time" = "2016-08-01T09:59:31Z"; id = "API-PAYMENT-ID-1843"; intent = sale; state = approved; }; "response_type" = payment; } [client: { environment = mock; "paypal_sdk_version" = "2.14.3"; platform = iOS; "product_name" = "PayPal iOS SDK"; }, response_type: payment, response: { "create_time" = "2016-08-01T09:16:40Z"; id = "PAY-NONETWORKPAYIDEXAMPLE123"; intent = sale; state = approved; }]代表另一个, { client = { environment = mock; "paypal_sdk_version" = "2.14.3"; platform = iOS; "product_name" = "PayPal iOS SDK"; }; response = { "create_time" = "2016-08-01T09:59:31Z"; id = "API-PAYMENT-ID-1843"; intent = sale; state = approved; }; "response_type" = payment; } { client = { environment = mock; "paypal_sdk_version" = "2.14.3"; platform = iOS; "product_name" = "PayPal iOS SDK"; }; response = { "create_time" = "2016-08-01T09:59:31Z"; id = "API-PAYMENT-ID-1843"; intent = sale; state = approved; }; "response_type" = payment; }

  1. Is the integration successful, or is there something that I am missing, because in response from paypal I don't find unique order id, not amount that has been charged from the user. 集成是否成功,或者我缺少什么,因为在贝宝的响应中,我找不到唯一的订单ID,而不是从用户那里收取的金额。

EDIT 1 This is info I am getting from 编辑1这是我从中得到的信息

print("this is also info (completedPayment.description)") from 来自的print(“这也是信息(completedPayment.description)”)

func payPalPaymentViewController(paymentViewController: PayPalPaymentViewController, didCompletePayment completedPayment: PayPalPayment) 
`CurrencyCode: AUD

Amount: 216.46

Short Description: My Shop

Intent: sale

Processable: Already processed

Display: $216.46

Confirmation: {

    client =     {

        environment = mock;

        "paypal_sdk_version" = "2.14.3";

        platform = iOS;

        "product_name" = "PayPal iOS SDK";

    };

    response =     {

        "create_time" = "2016-08-01T11:05:51Z";

        id = "PAY-NONETWORKPAYIDEXAMPLE123";

        intent = sale;

        state = approved;

    };

    "response_type" = payment;

}

Details: Subtotal: 207.97, Shipping: 5.99, Tax: 2.5

Shipping Address: (null)

Invoice Number: (null)

Custom: (null)

Soft Descriptor: (null)

BN code: (null)

Item: '{2|Old jeans with holes|84.99|USD|Hip-0037}'

Item: '{1|Free rainbow patch|0.00|USD|Hip-00066}'

Item: '{1|Long-sleeve plaid shirt (mustache not included)|37.99|USD|Hip-00291}'`

Please correct, me where I am lacking. 请纠正,我缺少的地方。

If you go through below code 如果您通过下面的代码

var environment:String = PayPalEnvironmentNoNetwork {
        willSet(newEnvironment) {
            if (newEnvironment != environment) {
                PayPalMobile.preconnectWithEnvironment(newEnvironment)
            }
        }
    }

Which is no network environment it will give you all the time success. 在没有网络环境的情况下,它将始终为您提供成功。

You need to change environment as per your requirement. 您需要根据需要更改环境。 For eg If you have sandbox user registered on PayPal then you need to change like this 例如,如果您已经在PayPal上注册了沙箱用户,则需要像这样进行更改

 var environment:String = PayPalEnvironmentSandbox
        {
        willSet(newEnvironment) {
            if (newEnvironment != environment)
            {
                PayPalMobile.preconnectWithEnvironment(newEnvironment)
            }
        }
    }

And then if you try with any other user rather than registered sandbox user it will give you error. 然后,如果您尝试使用任何其他用户而不是已注册的沙箱用户,则会出现错误。 This will work for only sandbox user. 这仅对沙盒用户有效。

There is one more environment called Production which is for live purpose that you need to ask credentials from your client. 还有一个称为生产的环境,该环境用于现场目的,您需要向客户询问凭据。

 PayPalEnvironmentProduction 

I hope the above information is clear which you was looking for. 希望以上信息清楚您所要查找的内容。

  • Don't forget to change client IDs in AppDelegate as per your respective environment 不要忘记根据您各自的环境在AppDelegate中更改客户端ID

PayPalMobile.initializeWithClientIdsForEnvironments([PayPalEnvironmentProduction: "", PayPalEnvironmentSandbox: "AYP......"]) PayPalMobile.initializeWithClientIdsForEnvironments([PayPalEnvironmentProduction:“”,PayPalEnvironmentSandbox:“ AYP ......”])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM