###前提・実現したいこと
現在、Stripe(決済サービス)とApple Pay繋げる作業をしております。バックエンドサーバーはherokuのアカウントを使用しています。
実現したい事は以下の写真のように"Buy withApple Pay"のボタンをタップすると、下から新しいWindowが出て、ユーザーが決済出来るようにしたいです。(書いてある"Delicious Kale"はストーリーボードでドラッグしているだけなので関係ないです)
###発生している問題
以下のコードを書いたのですがUIButtonがうまく機能しません。プロジェクトはBuildするのですが、ボタンをタップしても上の写真のように下から新しいwindowが出ません。考えられるエラーをお聞きしたく投稿させて頂きました。
###該当のソースコード
import UIKit import Stripe enum STPBackendChargeResult { case success, failure } typealias STPTokenSubmissionHandler = (STPBackendChargeResult?, NSError?) -> Void class ViewController: UIViewController, PKPaymentAuthorizationViewControllerDelegate { // Replace these values with your application's keys // Find this at https://dashboard.stripe.com/account/apikeys let stripePublishableKey = "ストライプのテストアカウントID" // To set this up, see https://github.com/stripe/example-ios-backend let backendChargeURLString = "herokuのバックエンドURL" // To set this up, see https://stripe.com/docs/mobile/apple-pay let appleMerchantId = "アップルMerchantID" let shirtPrice : UInt = 1000 // this is in cents override func viewDidLoad() { super.viewDidLoad() let button = PKPaymentButton(type: .buy, style: .black) button.addTarget(self, action: #selector(ViewController.beginPayment(_:)), for: .touchUpInside) let bw = button.frame.size.width let bh = button.frame.size.height let vw = view.frame.size.width let vh = view.frame.size.height button.frame = CGRect(origin: CGPoint(x: vw/2 - bw/2, y: vh/2 - bh/2), size: button.frame.size) view.addSubview(button) } func beginPayment(_: UIButton) { if (stripePublishableKey == "") { let alert = UIAlertController( title: "You need to set your Stripe publishable key.", message: "You can find your publishable key at https://dashboard.stripe.com/account/apikeys .", preferredStyle: UIAlertControllerStyle.alert ) let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil) alert.addAction(action) present(alert, animated: true, completion: nil) return } if (appleMerchantId != "") { let paymentRequest = Stripe.paymentRequest(withMerchantIdentifier: appleMerchantId) //{ if Stripe.canSubmitPaymentRequest(paymentRequest) { paymentRequest.paymentSummaryItems = [PKPaymentSummaryItem(label: "Cool shirt", amount: NSDecimalNumber(string: "10.00")), PKPaymentSummaryItem(label: "Stripe shirt shop", amount: NSDecimalNumber(string: "10.00"))] let paymentAuthVC = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest) paymentAuthVC.delegate = self present(paymentAuthVC, animated: true, completion: nil) return } //} } else { print("You should set an appleMerchantId.") } } func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping ((PKPaymentAuthorizationStatus) -> Void)) { let apiClient = STPAPIClient(publishableKey: stripePublishableKey) apiClient.createToken(with: payment, completion: { (token, error) -> Void in if error == nil { if let token = token { self.createBackendChargeWithToken(token, completion: { (result, error) -> Void in if result == STPBackendChargeResult.success { completion(PKPaymentAuthorizationStatus.success) } else { completion(PKPaymentAuthorizationStatus.failure) } }) } } else { completion(PKPaymentAuthorizationStatus.failure) } }) } func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) { dismiss(animated: true, completion: nil) } func createBackendChargeWithToken(_ token: STPToken, completion: @escaping STPTokenSubmissionHandler) { if backendChargeURLString != "" { if let url = URL(string: backendChargeURLString + "/charge") { let session = URLSession(configuration: URLSessionConfiguration.default) let request = NSMutableURLRequest(url: url) request.httpMethod = "POST" let postBody = "stripeToken=\(token.tokenId)&amount=\(shirtPrice)" let postData = postBody.data(using: String.Encoding.utf8, allowLossyConversion: false) session.uploadTask(with: request as URLRequest, from: postData, completionHandler: { data, response, error in let successfulResponse = (response as? HTTPURLResponse)?.statusCode == 200 if successfulResponse && error == nil { completion(.success, nil) } else { if error != nil { completion(.failure, error as NSError?) } else { completion(.failure, NSError(domain: StripeDomain, code: 50, userInfo: [NSLocalizedDescriptionKey: "There was an error communicating with your payment backend."])) } } }).resume() return } } completion(STPBackendChargeResult.failure, NSError(domain: StripeDomain, code: 50, userInfo: [NSLocalizedDescriptionKey: "You created a token! Its value is \(token.tokenId). Now configure your backend to accept this token and complete a charge."])) } }
###補足情報(言語/FW/ツール等のバージョンなど)
Swift3です。