質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Stripe

Stripeとは、米国のオンライン決済システム提供企業、及び同社が提供する決裁システムを指します。Webサイトやモバイルアプリにコードを組み込むことでクレジットカードなどの決済サービスが簡潔に追加できます。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

1回答

1401閲覧

UIButtonがうまく機能しません

akaakoz

総合スコア183

Stripe

Stripeとは、米国のオンライン決済システム提供企業、及び同社が提供する決裁システムを指します。Webサイトやモバイルアプリにコードを組み込むことでクレジットカードなどの決済サービスが簡潔に追加できます。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2016/11/27 09:47

###前提・実現したいこと
現在、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です。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzzball

2016/11/28 00:10

ボタンを押したときに呼ばれる関数はbeginPayment()でしょうか?printを入れるか、デバッガでステップ実行して処理の流れを把握して下さい。
guest

回答1

0

この条件がtrueにならないからでしょう。

swift

1if Stripe.canSubmitPaymentRequest(paymentRequest) {

AppDelegateでの処理が抜けてたりしませんか?
不具合箇所が特定できたらStripeのサポートに投げちゃうほうが良いです。

投稿2016/11/27 20:24

fromageblanc

総合スコア2724

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

akaakoz

2016/11/28 08:35

ご返答ありがとうございます。 おっしゃる通り if Stripe.canSubmitPaymentRequest(paymentRequest) { にprintを入れましたが、うまく反映されておりませんでした。 Stripeのサポートに連絡し、問題と解決方法が分かりましたらこちらに再度記載させて頂きます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問