doushi1929 2017-04-21 14:53
浏览 54
已采纳

将与客户的费用关联到Stripe

I am creating a simple web page where the Stripe Checkout will prompt the user for their card info, generate a token, and send it to my backend Go webserver that processes the payment for that token. The issue I'm having is that when I get the token, I try and create a customer and set the source to the token created by Stripe, but I get this response everytime:

charge failed: {"type":"invalid_request_error","message":"Customer cus_######### does not have a linked source with ID tok_#########.","code":"missing","param":"source","request_id":"req_######","status":404}

Creating the customer and associating the card with that customer works just fine. But, charging the card based on the token Stripe generated fails. Even though in the logs for that customer in my Stripe Dashboard it shows the attempted charge. So, it's trying to associate the charge with the correct customer, but failing because the token was generated without a specific customer in mind? I'm not really sure what is going on.

The code I have on the backend that accepts the token and processes everything is below:

stripe.Key = "sk_test_################"

customerParams := &stripe.CustomerParams{
    Desc:          "Customer for xyz.com",
    Email:         "test@email.com",
}

err := customerParams.SetSource("tok_####################")
if err != nil {
    return nil, err
}

cus, err := customer.New(customerParams)
if err != nil {
    return nil, err
}

// Charge the user's card
cp := &stripe.ChargeParams{
    Amount:   100,
    Currency: "usd",
    Desc:     "some description",
    Customer: cus.ID,
}

err = cp.SetSource("tok_####################")
if err != nil {
    return nil, err
}

err = charge.New(cp)

Also, if I remove the customer portion of this example, the charge goes through just fine with the above code. It just doesn't associate the charge with a customer obviously. However, as soon as I try to set the customer on the charge it fails.

  • 写回答

3条回答 默认 最新

  • duanpo6079 2017-04-24 14:57
    关注

    Turns out the issue was setting the token source on the charge params after I had already set the source of the new customer I was charging. The following worked as expected to create a new customer and associate the new charge with the customer:

    stripe.Key = "sk_test_################"
    
    customerParams := &stripe.CustomerParams{
        Desc:          "Customer for xyz.com",
        Email:         "test@email.com",
    }
    
    err := customerParams.SetSource("tok_####################")
    if err != nil {
        return nil, err
    }
    
    cus, err := customer.New(customerParams)
    if err != nil {
        return nil, err
    }
    
    // Charge the user's card
    cp := &stripe.ChargeParams{
        Amount:   100,
        Currency: "usd",
        Desc:     "some description",
        Customer: cus.ID,
    }
    
    err = charge.New(cp)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数