在 Stripe PaymentOptionViewController 中未调用添加新卡 [英] Add new card is not being called in Stripe PaymentOptionViewController

查看:23
本文介绍了在 Stripe PaymentOptionViewController 中未调用添加新卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 SwiftUI 中实现 Stripe Add Payment Method.因此用户可以添加付款方式或从列表中选择.经过多天的搜索,我能够实现一个有效的 PaymentOptionsView.但是,当单击添加新卡时,它不会显示 STPAddCardViewController 以输入新的付款方式

I am trying to implement Stripe Add Payment Method in SwiftUI. So a user can add a payment method or select from listed. After many days of searching I was able to implement a working PaymentOptionsView. However, when add new card is clicked it does not display the STPAddCardViewController to enter new payment methods

这是显示付款选项的代码

Here is the code that display the payment option

import Foundation
import SwiftUI
import Stripe

struct PaymentOptionsView: UIViewControllerRepresentable {
  func makeCoordinator() -> Coordinator {
    Coordinator(self)
 }

class Coordinator: NSObject, STPPaymentOptionsViewControllerDelegate {
    var control: PaymentOptionsView

    init(_ control: PaymentOptionsView) {
        self.control = control
    }

    // Implement required delegate methods here:
    func paymentOptionsViewControllerDidCancel(_ paymentOptionsViewController: STPPaymentOptionsViewController) {

    }

    func paymentOptionsViewControllerDidFinish(_ paymentOptionsViewController: STPPaymentOptionsViewController) {

    }

    func paymentOptionsViewController(_ paymentOptionsViewController: STPPaymentOptionsViewController, didFailToLoadWithError error: Error) {

    }
}

func makeUIViewController(context: UIViewControllerRepresentableContext<PaymentOptionsView>) -> STPPaymentOptionsViewController {
      let config = STPPaymentConfiguration()
      //          config.requiredBillingAddressFields = .none
      config.appleMerchantIdentifier = "dummy-merchant-id"

    return STPPaymentOptionsViewController(configuration: config, theme: STPTheme(), apiAdapter: STPCustomerContext(keyProvider: MyAPIClient()), delegate: context.coordinator)
  }


 func updateUIViewController(_ uiViewController: STPPaymentOptionsViewController, context: UIViewControllerRepresentableContext<PaymentOptionsView>) { }
 }

这里是 APIClient

and here is APIClient

class MyAPIClient: NSObject, STPCustomerEphemeralKeyProvider {


func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {

    let customerId = "cus_LoK4MNElrbzeVg"
     let url = "https://us-central1-test-mmmm.cloudfunctions.net/app/createEphemeralKey"
    
     
     AF.request(url, method: .post, parameters: [
         "api_version": "2020-08-27",
         "customerId": customerId,
         ])
         .validate(statusCode: 200..<300)
         .responseJSON { responseJSON in
             switch responseJSON.result {
             case .success(let json):
                 completion(json as? [String: AnyObject], nil)
             case .failure(let error):
                 print("Error creating customer Key (retrieving ephemeral key) with Alamofire. See: MyAPIClient - func createCustomerKey")
                 completion(nil, error)
             }
     }
  }
}

我做错了什么,如何实现添加新卡的方法?

What am I doing wrong and How do I implement the add new card method?

推荐答案

这段代码实际上运行良好.我最初是通过 .sheet(isPresented: 因此它不允许在按下添加卡按钮时转换到新视图.当 PaymentOptionsView 显示为

This code actually works fine. I was initially presenting PaymentOptionsView through .sheet(isPresented: so it does not allowing transition to a new view when the add card button is pressed. When the PaymentOptionsView is presented as

  .background( // add a hidden `NavigationLink` in the background
                          NavigationLink(destination:  PaymentOptionsView(), isActive: $showPaymentSheet) {EmptyView()}
                          .hidden()
                      )

一切正常.当按下添加新卡片按钮时,视图完美过渡到 STPAddCardViewController.

everythings works. when the add new card button is pressed, the view transition to STPAddCardViewController perfectly.

这篇关于在 Stripe PaymentOptionViewController 中未调用添加新卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆