IOS strip charging

I'm having trouble loading a user through Stripe. The paymentResult object that I get in the following delegate method

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

}

      

is an STPCard object, but according to the documentation, in order to complete the download from my backend I need an STPToken. I have tried using

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

      

to create an STPToken with an STPCard object I got, but I get an error when the card parameter doesn't have the required variable 'number'. Does anyone know what might happen, or if there is a way to complete the charge with just the STPCard object? Thank.

+4


source to share


2 answers


At the moment when STPPaymentContext calls didCreatePaymentResult on its item, the token has already been created, so there is no need to try to create a second token. I would look at an example of an iOS backend that works with the default sample app in the Stripe SDK:



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

0


source


If you're using stripe id

from card instead token

, you need to specify a customer object, see How to use Stripe and Apple Pay on iOS.

Here's how to deal with it in 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)
}

      

0


source







All Articles