简体   繁体   English

通过Stripe for iOS充电

[英]Charging through Stripe for iOS

I'm having trouble charging a user through Stripe. 我无法通过Stripe向用户收费。 The paymentResult object I receive in the following delegate method 我在以下委托方法中收到的paymentResult对象

func paymentContext(_ paymentContext: STPPaymentContext, didCreatePaymentResult paymentResult: STPPaymentResult, completion: @escaping STPErrorBlock) {

}

is a STPCard object, but according the documentation to complete a charge with my backend I need an STPToken. 是一个STPCard对象,但根据文档来完成我的后端收费我需要一个STPToken。 I've tried using 我试过用了

STPAPIClient.shared().createToken(withCard: card) {}

to create a STPToken with the STPCard object I received, but I get an error saying the card parameter doesn't have the required variable 'number'. 用我收到的STPCard对象创建一个STPToken,但是我得到一个错误,说卡参数没有所需的变量'number'。 Does anyone know what may be going on or if there is a way to complete a charge with just the STPCard object? 有没有人知道可能会发生什么,或者是否有办法仅使用STPCard对象来完成收费? Thank you. 谢谢。

At the point where STPPaymentContext calls didCreatePaymentResult on its delegate, the token has already been created, so there's no need to try to create a second token. 在STPPaymentContext在其委托上调用didCreatePaymentResult时,该令牌已经创建,因此无需尝试创建第二个令牌。 I'd have a look at the example iOS backend, which works with the "standard" example application in the Stripe SDK: 我将看一下示例iOS后端,它与Stripe SDK中的“标准”示例应用程序一起使用:

https://github.com/stripe/example-ios-backend https://github.com/stripe/example-ios-backend

If you use stripe id from card instead of token , you need to include customer object, see How to use Stripe and Apple Pay in iOS 如果您使用来自卡而不是token stripe id ,则需要包含客户对象,请参阅如何在iOS中使用Stripe和Apple Pay

Here's how to handle that in Go 以下是如何在Go中处理它

package main

import (
    "net"
    "encoding/json"
    "fmt"
    "net/http"
    "log"
    "os"
    "github.com/stripe/stripe-go/charge"
)

func main() {
    stripe.Key = "sk_test_mM2MkqO61n7vvbVRfeYmBgWm00Si2PtWab"

    http.HandleFunc("/request_charge", handleCharge)
    if err := http.ListenAndServe(":8080", nil); err != nil {
        panic(err)
    }
}

var customerId = "cus_Eys6aeP5xR89ab"

type PaymentResult struct {
    StripeId string `json:"stripe_id"`
}

func handleCharge(w http.ResponseWriter, r *http.Request) {
    decoder := json.NewDecoder(r.Body)
    var t PaymentResult
    err := decoder.Decode(&t)
    if err != nil {
        panic(err)
    }

    params := &stripe.ChargeParams{
        Amount:      stripe.Int64(150),
        Currency:    stripe.String(string(stripe.CurrencyUSD)),
        Description: stripe.String("Charge from my Go backend"),
        Customer: stripe.String(customerId),
    }

    params.SetSource(t.StripeId)
    ch, err := charge.New(params)
    if err != nil {
        fmt.Fprintf(w, "Could not process payment: %v", err)
        fmt.Println(ch)
        w.WriteHeader(400)
    }

    w.WriteHeader(200)
}

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

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