菜鸟教程小白 发表于 2022-12-13 13:54:47

ios - 将 Paypal SDK 与原生 iOS 应用集成


                                            <p><p>我指的是整合<a href="https://github.com/paypal/PayPal-iOS-SDK" rel="noreferrer noopener nofollow">PayPal-iOS-SDK</a>使用我的 nativeiOS 应用程序。</p>

<p>我已经成功集成了应用程序(这是我的感受)</p>

<p>请引用代码</p>

<pre><code>func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: ?) -&gt; Bool {
      // Override point for customization after application launch.
      PayPalMobile .initializeWithClientIdsForEnvironments([PayPalEnvironmentProduction: &#34;YOUR_CLIENT_ID_FOR_PRODUCTION&#34;,
            PayPalEnvironmentSandbox: &#34;SANDBOX CODE&#34;])
      return true
    }
</code></pre>

<p><code> ViewController </code></p>

<pre><code>//
//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 = &#34;&#34; // 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 = &#34;PayPal SDK Demo&#34;
      //successView.hidden = true

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



      payPalConfig.languageOrLocale = NSLocale.preferredLanguages()

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

      payPalConfig.payPalShippingAddressOption = .PayPal;

      print(&#34;PayPal iOS SDK Version: \(PayPalMobile.libraryVersion())&#34;)
    }

    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(&#34;integrate paypal here&#34;)
      resultText = &#34;&#34;

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

      let items =
      let subtotal = PayPalItem.totalPriceForItems(items)

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

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

      let payment = PayPalPayment(amount: total, currencyCode: &#34;AUD&#34;, shortDescription: &#34;My Shop&#34;, 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&#39;t be processable, and you&#39;d want
            // to handle that here.
            print(&#34;Payment not processalbe: \(payment)&#34;)
      }
    }


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

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

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

    func showSuccess(inputString: NSObject) {

      print(&#34;this is done successfully :) &#34;)
      print(&#34;send this to server \(inputString)&#34;)
    }



}
</code></pre>

<p>我对该应用的担忧是</p>

<p>1.无论我使用什么登录方式,我都可以登录。我已尝试将 <code>tim@cook.com</code> 作为电子邮件地址,将 <code>1234</code> 作为密码,但我仍然能够登录。</p>

<p>2.我尝试使用信用卡作为 4242-4242-4242-4242,cvv 123 12/12 仍然支付成功。</p>

<p>3.这些是我得到的回应
<code>[客户:{
    环境=模拟;
    "paypal_sdk_version"= "2.14.3";
    平台 = iOS;
    "product_name"= " Paypal iOS SDK";
},response_type:付款,响应:{
    “创建时间”=“2016-08-01T09:16:40Z”;
    id = "PAY-NONETWORKPAYIDEXAMPLE123";
    意图=销售;
    状态=批准;
}]
</code> 对于另一个,
<code>{
    客户= {
      环境=模拟;
      "paypal_sdk_version"= "2.14.3";
      平台 = iOS;
      "product_name"= " Paypal iOS SDK";
    };
    回应 = {
      “创建时间”=“2016-08-01T09:59:31Z”;
      id = "API-PAYMENT-ID-1843";
      意图=销售;
      状态=批准;
    };
    “response_type”=付款;
}</code></p>

<ol 开始="4">
<li>集成是否成功,或者我是否遗漏了什么,因为在 paypal 的回复中我没有找到唯一的订单 ID,而不是已向用户收取的金额。</li>
</ol>

<p><strong>编辑 1</strong>
这是我从中获得的信息</p>

<p>print("这也是信息 (completedPayment.description)")
来自</p>

<pre><code>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;

      &#34;paypal_sdk_version&#34; = &#34;2.14.3&#34;;

      platform = iOS;

      &#34;product_name&#34; = &#34;PayPal iOS SDK&#34;;

    };

    response =   {

      &#34;create_time&#34; = &#34;2016-08-01T11:05:51Z&#34;;

      id = &#34;PAY-NONETWORKPAYIDEXAMPLE123&#34;;

      intent = sale;

      state = approved;

    };

    &#34;response_type&#34; = 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: &#39;{2|Old jeans with holes|84.99|USD|Hip-0037}&#39;

Item: &#39;{1|Free rainbow patch|0.00|USD|Hip-00066}&#39;

Item: &#39;{1|Long-sleeve plaid shirt (mustache not included)|37.99|USD|Hip-00291}&#39;`
</code></pre>

<p>不足之处请指正</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果你通过下面的代码</p>

<pre><code>var environment:String = PayPalEnvironmentNoNetwork {
      willSet(newEnvironment) {
            if (newEnvironment != environment) {
                PayPalMobile.preconnectWithEnvironment(newEnvironment)
            }
      }
    }
</code></pre>

<p>在没有网络的环境下它会让你一直成功。</p>

<p>您需要根据您的要求更改环境。例如如果您在 PayPal 上注册了沙箱用户,那么您需要像这样更改</p>

<pre><code> var environment:String = PayPalEnvironmentSandbox
      {
      willSet(newEnvironment) {
            if (newEnvironment != environment)
            {
                PayPalMobile.preconnectWithEnvironment(newEnvironment)
            }
      }
    }
</code></pre>

<p>然后,如果您尝试与任何其他用户而不是注册沙箱用户一起尝试,它将给您错误。这仅适用于沙箱用户。</p>

<p>还有一个称为生产的环境,它用于实时目的,您需要向客户询问凭据。</p>

<pre><code> PayPalEnvironmentProduction
</code></pre>

<p>我希望以上信息是您正在寻找的清楚信息。</p>

<ul>
<li>不要忘记根据您各自的环境更改 AppDelegate 中的客户端 ID</li>
</ul>

<blockquote>
<p>PayPalMobile.initializeWithClientIdsForEnvironments([PayPalEnvironmentProduction:
&#34;&#34;, PayPalEnvironmentSandbox:
&#34;AYP......&#34;])</p>
</blockquote></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 将 Paypal SDK 与原生 iOS 应用集成,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38696375/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38696375/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 将 Paypal SDK 与原生 iOS 应用集成