applepay 在真机上不可用
applepay is not working in real device
我在 ios 中使用 swift 实现了带条带的苹果支付。因此,当测试程序时,它可以在模拟器上运行良好,但在真实设备上运行不佳。要使用真实设备测试 Apple Pay,它不会显示付款 sheet 来处理交易。我的疑问是在真实设备上使用 apple pay 进行测试,我们可以将测试卡添加到钱包或拥有沙盒帐户吗?使用stripe gate方式需要在钱包中添加测试卡吗?你能给我说清楚吗?
这是我的代码:
import UIKit
import PassKit
import Stripe
class ViewController: UIViewController {
let request = PKPaymentRequest()
var paymentStatus = PKPaymentAuthorizationStatus.failure
var paymentController: PKPaymentAuthorizationController?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func applepay(_ sender: Any) {
let merchantIdentifier = "merchant.Appplepay"
let paymentNetworks = [PKPaymentNetwork.masterCard, PKPaymentNetwork.visa,PKPaymentNetwork.amex,PKPaymentNetwork.discover,PKPaymentNetwork.interac]
if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: paymentNetworks)
{
request.merchantIdentifier = "merchant.Appplepay"
request.countryCode = "US"
request.currencyCode = "USD"
request.supportedNetworks = paymentNetworks
request.merchantCapabilities = .capability3DS
request.requiredShippingAddressFields = [.phone, .email,.postalAddress,.all]
request.paymentSummaryItems = [PKPaymentSummaryItem(label: "Fancy Hat", amount: 50.00),
// The final line should represent your company;
// it'll be prepended with the word "Pay" (i.e. "Pay iHats, Inc ")
PKPaymentSummaryItem(label: "iHats, Inc", amount: 50.00),]
} else {
// Traditional checkout flow
}
func applePaySupported() -> Bool {
return PKPaymentAuthorizationViewController.canMakePayments() && PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: [.amex, .visa, .masterCard])
}
// Setup payment authorization view controller
if Stripe.canSubmitPaymentRequest(request) {
// Setup payment authorization view controller
let paymentAuthorizationViewController = PKPaymentAuthorizationViewController(paymentRequest: request)
paymentAuthorizationViewController?.delegate = self
// Present payment authorization view controller
self.present((paymentAuthorizationViewController)!, animated: true,completion: nil)
}
else {
// There is a problem with your Apple Pay configuration
print("Failed to present PaymentSheet");
}
}
}
extension ViewController : PKPaymentAuthorizationViewControllerDelegate {
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping (PKPaymentAuthorizationStatus) -> Void) {
if payment.shippingContact?.emailAddress == nil || payment.shippingContact?.phoneNumber == nil {
paymentStatus = .invalidShippingContact
} else {
STPAPIClient.shared().createToken(with: payment) { (token: STPToken?, error)-> Void in
print("Stripe token is \(String(describing: token!))")
//self.paymentStatus = .success
completion(.success)
}
}
}
func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
回答有关在真实设备上进行测试的问题:您只能将真实卡添加到钱包以用于 Apple Pay,但是如果您使用测试条 api 密钥,that is detected by stripe and your real card will not be charged
不确定为什么付款 sheet 没有在没有看到一些代码的情况下显示在真实设备上,但您需要确保设备已设置为 Apple Pay,主要是它有卡添加到钱包(你支持)
并非所有国家/地区 support Apple Pay 因此并非总是有将卡添加到钱包的选项,这意味着 Apple Pay sheet 不会显示 [=10] =] 返回错误。
我在 ios 中使用 swift 实现了带条带的苹果支付。因此,当测试程序时,它可以在模拟器上运行良好,但在真实设备上运行不佳。要使用真实设备测试 Apple Pay,它不会显示付款 sheet 来处理交易。我的疑问是在真实设备上使用 apple pay 进行测试,我们可以将测试卡添加到钱包或拥有沙盒帐户吗?使用stripe gate方式需要在钱包中添加测试卡吗?你能给我说清楚吗?
这是我的代码:
import UIKit
import PassKit
import Stripe
class ViewController: UIViewController {
let request = PKPaymentRequest()
var paymentStatus = PKPaymentAuthorizationStatus.failure
var paymentController: PKPaymentAuthorizationController?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func applepay(_ sender: Any) {
let merchantIdentifier = "merchant.Appplepay"
let paymentNetworks = [PKPaymentNetwork.masterCard, PKPaymentNetwork.visa,PKPaymentNetwork.amex,PKPaymentNetwork.discover,PKPaymentNetwork.interac]
if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: paymentNetworks)
{
request.merchantIdentifier = "merchant.Appplepay"
request.countryCode = "US"
request.currencyCode = "USD"
request.supportedNetworks = paymentNetworks
request.merchantCapabilities = .capability3DS
request.requiredShippingAddressFields = [.phone, .email,.postalAddress,.all]
request.paymentSummaryItems = [PKPaymentSummaryItem(label: "Fancy Hat", amount: 50.00),
// The final line should represent your company;
// it'll be prepended with the word "Pay" (i.e. "Pay iHats, Inc ")
PKPaymentSummaryItem(label: "iHats, Inc", amount: 50.00),]
} else {
// Traditional checkout flow
}
func applePaySupported() -> Bool {
return PKPaymentAuthorizationViewController.canMakePayments() && PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: [.amex, .visa, .masterCard])
}
// Setup payment authorization view controller
if Stripe.canSubmitPaymentRequest(request) {
// Setup payment authorization view controller
let paymentAuthorizationViewController = PKPaymentAuthorizationViewController(paymentRequest: request)
paymentAuthorizationViewController?.delegate = self
// Present payment authorization view controller
self.present((paymentAuthorizationViewController)!, animated: true,completion: nil)
}
else {
// There is a problem with your Apple Pay configuration
print("Failed to present PaymentSheet");
}
}
}
extension ViewController : PKPaymentAuthorizationViewControllerDelegate {
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping (PKPaymentAuthorizationStatus) -> Void) {
if payment.shippingContact?.emailAddress == nil || payment.shippingContact?.phoneNumber == nil {
paymentStatus = .invalidShippingContact
} else {
STPAPIClient.shared().createToken(with: payment) { (token: STPToken?, error)-> Void in
print("Stripe token is \(String(describing: token!))")
//self.paymentStatus = .success
completion(.success)
}
}
}
func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
回答有关在真实设备上进行测试的问题:您只能将真实卡添加到钱包以用于 Apple Pay,但是如果您使用测试条 api 密钥,that is detected by stripe and your real card will not be charged
不确定为什么付款 sheet 没有在没有看到一些代码的情况下显示在真实设备上,但您需要确保设备已设置为 Apple Pay,主要是它有卡添加到钱包(你支持)
并非所有国家/地区 support Apple Pay 因此并非总是有将卡添加到钱包的选项,这意味着 Apple Pay sheet 不会显示 [=10] =] 返回错误。