PayPaliOSSDK
[Swift3.0.2]PayPal iOSSDKの組み込み方
[環境]
Xcode:8.2.1
Swift:3.0.2
iOS:10.2
PayPaliOSSDKの組み込み・利用方法になります。
前提として、下図のように必要ファイルはプロジェクト内に組み込みが完了している必要があります。
単純に使える状態にする方法に焦点を絞り、
下図のようにボタンをタップしたらPayPal支払いに遷移するというシンプルなコードの紹介です。
(※ご自身の状況や環境に合わせてカスタマイズしてください。)
細かい説明は割愛し、コードを貼ります。
こんな感じです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
import UIKit class TestPaymentViewController: UIViewController, PayPalPaymentDelegate { var payPalConfig = PayPalConfiguration() // Note: デフォルトは接続・通信が発生しないPayPalEnvironmentNoNetworkが設定されている // テスト環境への接続はPayPalEnvironmentSandboxを設定 // 本番環境への接続はPayPalEnvironmentProductionを設定 var environment:String = PayPalEnvironmentSandbox { willSet(newEnvironment) { if (newEnvironment != environment) { PayPalMobile.preconnect(withEnvironment: newEnvironment) } } } var acceptCreditCards: Bool = true { didSet { payPalConfig.acceptCreditCards = acceptCreditCards } } override func viewDidLoad() { super.viewDidLoad() // Set Up PayPalConfig payPalConfig.acceptCreditCards = acceptCreditCards //false payPalConfig.merchantName = "TestPayment" payPalConfig.merchantPrivacyPolicyURL = URL(string: "https://www.paypal.com/webapps/mpp/ua/privacy-full") payPalConfig.merchantUserAgreementURL = URL(string: "https://www.paypal.com/webapps/mpp/ua/useragreement-full") payPalConfig.languageOrLocale = Locale.preferredLanguages[0] // 決済画面への配送先住所表示の有無 // payPalConfig.payPalShippingAddressOption = .payPal PayPalMobile.preconnect(withEnvironment: environment) // 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. // // Setting the payPalShippingAddressOption property is optional. // // See PayPalConfiguration.h for details. let paymentButton = UIButton() paymentButton.frame = CGRect(x: UIScreen.main.bounds.width / 4, y: UIScreen.main.bounds.height / 2, width: UIScreen.main.bounds.width / 2, height: 48) paymentButton.addTarget(self, action: #selector(moveToPayPalPayment), for: .touchUpInside) paymentButton.setTitle("PayPalPayment", for: .normal) paymentButton.backgroundColor = #colorLiteral(red: 0, green: 0.4784313725, blue: 1, alpha: 1) self.view.addSubview(paymentButton) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // PayPalPaymentDelegate func payPalPaymentDidCancel(_ paymentViewController: PayPalPaymentViewController) { print("PayPal Payment Cancelled") // resultText = "" // successView.isHidden = true paymentViewController.dismiss(animated: true, completion: nil) } func payPalPaymentViewController(_ paymentViewController: PayPalPaymentViewController, didComplete completedPayment: PayPalPayment) { print("PayPal Payment Success!") paymentViewController.dismiss(animated: 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() // 別途サーバ側で用意しているAPIなどに投げるパラメータが必要であれば、下記のように取得可能 // let confirmation = completedPayment.confirmation // let authorizationId = (confirmation["response"]! as AnyObject).value(forKey: "authorization_id")! // let id = (confirmation["response"]! as AnyObject).value(forKey: "id")! // let createTime = (confirmation["response"]! as AnyObject).value(forKey: "create_time")! // APIへの接続処理などがあれば記載してください(ここでは割愛します) }) } func moveToPayPalPayment() { // 以下、PayPalDBに格納される項目 // Optional: include multiple items let item01 = PayPalItem(name: "Sample01", withQuantity: 1, withPrice: NSDecimalNumber(string: "11.00"), withCurrency: "USD", withSku: "TestPayment-001") let item02 = PayPalItem(name: "Sample02", withQuantity: 1, withPrice: NSDecimalNumber(string: "11.50"), withCurrency: "USD", withSku: "TestPayment-001") let item03 = PayPalItem(name: "Sample03", withQuantity: 1, withPrice: NSDecimalNumber(string: "12.00"), withCurrency: "USD", withSku: "TestPayment-001") let items = [item01, item02, item03] let subtotal = PayPalItem.totalPrice(forItems: items) // Optional: include payment details let shipping = NSDecimalNumber(string: "0.00") //送料 let tax = NSDecimalNumber(string: "0.00") //Tax let paymentDetails = PayPalPaymentDetails(subtotal: subtotal, withShipping: shipping, withTax: tax) let total = subtotal.adding(shipping).adding(tax) // Note: intentプロパティを使用してこれが"販売"の支払いであることを示す。 // これは承認と回収の組み合わせを意味する。 // プロセスが完了した時点で承認と回収を実行する場合は、PayPalPaymentIntentSale(.sale)を設定。 // 承認のみを実行し回収はサーバーで処理する場合は、PayPalPaymentIntentAuthorize(.authorize)を設定。 // 注文のみを実行し承認と回収をサーバーで処理する場合は、PayPalPaymentIntentOrder(.order)を設定。 // (PayPalPaymentIntentOrderは、PayPal支払いでのみ有効。クレジットカード決済の場合は無効。) // 決済画面に表示される項目 let payment = PayPalPayment(amount: total, currencyCode: "USD", shortDescription: "TestPayment", intent: .authorize) payment.items = items payment.paymentDetails = paymentDetails if (payment.processable) { print ("Payment processalbe") let paymentViewController = PayPalPaymentViewController(payment: payment, configuration: self.payPalConfig, delegate: self) self.present(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)") } } } |