简体   繁体   English

在 SwiftUI 添加无需付款的 Stripe 信用卡

[英]Add Stripe Credit Card without Payment in SwiftUI

I am struggling to find a solution that isn't UIKit, or one that requires you make a purchase.我正在努力寻找不是 UIKit 或需要您购买的解决方案。

My project is trying to integrate Stripe in SwiftUI, using node.js Firebase Functions for backend handling.我的项目试图在 SwiftUI 中集成 Stripe,使用 node.js Firebase 函数进行后端处理。

I built a STPPaymentCardTextField in UIViewRepresentable.我在 UIViewRepresentable 中构建了一个 STPPaymentCardTextField。 Which allows me to obtain credit card details for the customer via State.这让我可以通过 State 获取客户的信用卡详细信息。

@State var params: STPPaymentMethodCardParams = STPPaymentMethodCardParams()

SwiftUI TextField SwiftUI 文本字段

StripePaymentCardTextField(cardParams: $params, isValid: $isValid)

Then we can build paymentMethodParms like..然后我们可以构建 paymentMethodParms 就像..

 let paymentMethodParams = STPPaymentMethodParams(card: params, billingDetails: billingDetails, metadata: nil)

Now I could easily pass the credit card details to my backend and add the card manually using现在我可以轻松地将信用卡详细信息传递到我的后端并使用手动添加卡

Function for adding payment method Function 添加支付方式

const createPaymentMethods = functions.https.onCall(async (data, context) => {
    const paymentMethod = await stripe.paymentMethods.create({
        customer: '{{CUSTOMER_ID}}',
        payment_method: '{{PAYMENT_METHOD_ID}}',
        }, {
            stripeAccount: '{{CONNECTED_STRIPE_ACCOUNT_ID}}',
        });
    
}

but I am understanding this is bad practice.但我知道这是不好的做法。 I found this post which might be a "duplicate", but is the closest relevant answer.我发现这篇文章可能是“重复的”,但却是最接近的相关答案。 Adding customer's Credit card info in stripe . 在 stripe 中添加客户的信用卡信息 That user however is using reactjs and wanted to store credit card details in a database, where I only want to pass it to Stripe.然而,该用户正在使用 reactjs 并希望将信用卡详细信息存储在数据库中,我只想将其传递给 Stripe。

Is there not a way I can send the credit card data to Stripe, get a paymentMethod ID back, and pass that to my backend or something?有没有办法可以将信用卡数据发送到 Stripe,取回 paymentMethod ID,然后将其传递到我的后端或其他东西? I have already solved subscriptions, and purchase charging, I just can't get a credit card setup without going through the payment setup process.我已经解决了订阅和购买收费问题,我只是无法在不经过付款设置流程的情况下进行信用卡设置。 I want the user to add a credit card manually on creating a new account and/or in settings.我希望用户在创建新帐户和/或设置时手动添加信用卡。

Can you call stripe-ios's createPaymentMethod() function [0]?能不能调用stripe-ios的createPaymentMethod() function[0]? That is a client-side function your SwiftUI app would call to tokenize the card details from paymentMethodParams into a PaymentMethod object.这是客户端 function 您的 SwiftUI 应用程序将调用以将来自paymentMethodParams的卡详细信息标记为 PaymentMethod object。 You can then pass that ID server-side to attach to a Customer.然后,您可以将该 ID 服务器端传递给客户。

[0] https://stripe.dev/stripe-ios/docs/Classes/STPAPIClient.html#/c:@CM@Stripe@objc(cs)STPAPIClient(im)createPaymentMethodWithPayment:completion : [0] https://stripe.dev/stripe-ios/docs/Classes/STPAPIClient.html#/c:@CM@Stripe@objc(cs)STPAPIClient(im)createPaymentMethodWithPayment:completion :

To expand on @hmunoz's answer and the way I got this to work is as follows.扩展@hmunoz 的答案和我让它工作的方式如下。

  1. Create your user a Stripe account.为您的用户创建一个 Stripe 帐户。 https://stripe.com/docs/api/customers/create https://stripe.com/docs/api/customers/create
  2. Log the customers stripe id in your user's Firestore database for easy reference.在用户的 Firestore 数据库中记录客户条带 ID,以便于参考。
  3. Create an "Add card" view to your frontend and call to the add payment method function. https://stripe.com/docs/api/cards/create在您的前端创建一个“添加卡”视图并调用添加付款方式function。https://stripe.com/docs/api/cards/create
  4. Upon checkout as long as the customer's stripe ID is passed in the payment intent it should populate with their saved payment methods.结帐时,只要在付款意图中传递了客户的条带 ID,它就应该使用他们保存的付款方式进行填充。

In summary it is best to keep track of your user that is signed in so that you can do any CRUD needed throughout.总之,最好跟踪已登录的用户,以便您可以在整个过程中执行所需的任何 CRUD。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM