Stripe API调用(创建支付意图)失败:x509证书由未知机构签署

huangapple go评论94阅读模式
英文:

Stripe API call (Payment Intent creation) failed: x509 certificate signed by unknown authority

问题

我正在尝试在服务器端(使用Go语言)实例化一个PaymentIntent,就像这个示例一样,但是遇到了这个错误Request failed with error: Post "https://api.stripe.com/v1/payment_intents": x509: certificate signed by unknown authority。我已经确保像下面这样分配了测试密钥,但错误仍然存在。这与SSL证书有关吗?我正在使用Docker在本地测试我的应用程序(localhost)。

我的代码:

import (
    "github.com/stripe/stripe-go"
    "github.com/stripe/stripe-go/paymentintent"
)

func CreateStripePaymentIntent(subtotal float32) (string, error) {
    // 使用金额和货币创建一个PaymentIntent
    stripe.Key = os.Getenv("STRIPE_SECRET_KEY")

    fmt.Println(stripe.Key)

    params := &stripe.PaymentIntentParams{
        Amount:   stripe.Int64(int64(subtotal)),
        Currency: stripe.String(string(stripe.CurrencyUSD)),
    }

    pi, err := paymentintent.New(params)

    if err != nil {
        return "", fmt.Errorf("pi.New: %v", err) // ========> 在调用Stripe API时出错
    }

    return pi.ClientSecret, nil
}
英文:

I am trying to instantiate a PaymentIntent on the server-side (using Go) just like this example but met with this error Request failed with error: Post "https://api.stripe.com/v1/payment_intents": x509: certificate signed by unknown authority. I have made sure to assign the test secret key like below, but the error still persists. Does it have something to do with SSL certificate? I am testing my app locally using Docker (localhost).

My code:

import (
    "github.com/stripe/stripe-go"
	"github.com/stripe/stripe-go/paymentintent"
)

func CreateStripePaymentIntent(subtotal float32) (string, error) {
	// Create a PaymentIntent with amount and currency
	stripe.Key = os.Getenv("STRIPE_SECRET_KEY")

	fmt.Println(stripe.Key)

	params := &stripe.PaymentIntentParams{
		Amount:   stripe.Int64(int64(subtotal)),
		Currency: stripe.String(string(stripe.CurrencyUSD)),
	}

	pi, err := paymentintent.New(params)

	if err != nil {
		return "", fmt.Errorf("pi.New: %v", err) // =======> ERROR HERE WHEN CALLING STRIPE API
	}


	return pi.ClientSecret, nil
}

答案1

得分: 1

由于我使用scratch Docker镜像运行服务器,所以没有SSL证书可用。只需要从第一阶段(我的构建是多阶段构建)复制证书COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

英文:

Since I run the server using scratch Docker image, there is no SSL certificate to use. Just need to copy the certs from first stage (Mine is a multi-staged build) COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

huangapple
  • 本文由 发表于 2022年2月5日 19:00:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/70997363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定