“没有这样的令牌”错误 400 向 Stripe 发出付款请求

问题描述 投票:0回答:1

网站 API STRIPE 告诉我 LOGS 中有错误: 输入测试卡号码并单击“确认”按钮时出现 400 错误的具体原因是什么? 此消息包含我的错误的所有详细信息。 我已附上付款代码。处理token等

POST /v1/charges
Status
400 ERR
ID
req_GKbBUprcIuSQ0X
Time
5/25/24, 1:09:58 PM
IP address
151.249.163.206
API version
2024-04-10
Source
Stripe/v1 PythonBindings/2.37.2
Idempotency
Key — badc22f3-4989-46e8-aec6-35347aa48d10

resource_missing - source
No such token: 'tok_1PKHY1IRI3YGNQKDocTdMkPu'

Was this useful?

Yes

No
{
  "source": "tok_1PKHY1IRI3YGNQKDocTdMkPu",
  "amount": "4400",
  "currency": "usd"
}
Response body
{
  "error": {
    "code": "resource_missing",
    "doc_url": "https://stripe.com/docs/error-codes/resource-missing",
    "message": "No such token: 'tok_1PKHY1IRI3YGNQKDocTdMkPu'",
    "param": "source",
    "request_log_url": "https://dashboard.stripe.com/test/logs/req_GKbBUprcIuSQ0X?t=1716631798",
    "type": "invalid_request_error"
  }
}
Request POST body
{
  "source": "tok_1PKHY1IRI3YGNQKDocTdMkPu",
  "amount": "4400",
  "currency": "usd"
}

我的API令牌

STRIPE_SECRET_KEY=sk_test_51PKFiB03rDhpU9syC7RuPemfhPC6Z5IZvxaKWRR5MajKEE3tvE58VrbZjxp3y56VoGrC2QLkARr95QVuDpmmu8uv00TP46EZQM STRIPE_PUBLISHABLE_KEY=pk_test_51PKFiB03rDhpU9syvWsl9Mvlt0XLdWaDI9a8fdolFlN9SXcUSxPhd9rtolZSUKIbBikfuWoeYZkPrdxFvf1T3Pmw00DGaQtOPA

观点.PY

class PaymentView(LoginRequiredMixin, View):
    def get(self, *args, **kwargs):
        order = Order.objects.filter(user=self.request.user, ordered=False).first()
        return render(self.request, 'checkout/payment.html', {'order': order})

    def post(self, *args, **kwargs):
        order = Order.objects.filter(user=self.request.user, ordered=False).first()
        token = self.request.POST.get('stripeToken')
        try:
            charge = stripe.Charge.create(
              amount=round(float(order.get_total_amount() * 100)),
              currency="usd",
              source=token
            )
        except stripe.error.CardError:
            messages.error(self.request, 'Payment could not be made')
            return redirect('products:home-page')
        except Exception:
            messages.error(self.request, 'Internal server error')
            return redirect('products:home-page')

        payment = Payment(
            user=self.request.user,
            stripe_id=charge.id,
            amount=order.get_total_amount()
        )
        payment.save()
        order.order_id = get_random_string(length=20)
        order.payment = payment
        order.ordered = True
        order.save()

        messages.info(self.request, 'Payment was successfully issued')
        return redirect('checkout:checkout-success')

JS 文件

const stripe = Stripe('pk_test_51PKFiB03rDhpU9syvWsl9Mvlt0XLdWaDI9a8fdolFlN9SXcUSxPhd9rtolZSUKIbBikfuWoeYZkPrdxFvf1T3Pmw00DGaQtOPA');
const elements = stripe.elements();

// Custom styling can be passed to options when creating an Element.
const style = {
  base: {
    // Add your base input styles here. For example:
    fontSize: '16px',
    color: "#32325d",
  },
};

// Create an instance of the card Element.
const card = elements.create('card', {style});

// Add an instance of the card Element into the `card-element` <div>.
card.mount('#card-element');

card.addEventListener('change', ({error}) => {
  const displayError = document.getElementById('card-errors');
  if (error) {
    displayError.textContent = error.message;
  } else {
    displayError.textContent = '';
  }
});

// Create a token or display an error when the form is submitted.
const form = document.getElementById('payment-form');
form.addEventListener('submit', async (event) => {
  event.preventDefault();

  const {token, error} = await stripe.createToken(card);

  if (error) {
    // Inform the customer that there was an error.
    const errorElement = document.getElementById('card-errors');
    errorElement.textContent = error.message;
  } else {
    // Send the token to your server.
    stripeTokenHandler(token);
  }
});

const stripeTokenHandler = (token) => {
  // Insert the token ID into the form so it gets submitted to the server
  const form = document.getElementById('payment-form');
  const hiddenInput = document.createElement('input');
  hiddenInput.setAttribute('type', 'hidden');
  hiddenInput.setAttribute('name', 'stripeToken');
  hiddenInput.setAttribute('value', token.id);
  form.appendChild(hiddenInput);

  // Submit the form
  form.submit();
}

此消息包含我的错误的所有详细信息。 我已附上付款代码。处理令牌等 帮助

python django stripe-payments credit-card http-status-code-400
1个回答
0
投票

正如错误消息所述,它无法在您的 Stripe 帐户上找到

tok_1PKHY1IRI3YGNQKDocTdMkPu
。您确定该令牌是使用您在 JS 文件中使用的相同可发布密钥创建的吗?我建议您检查/记录用于创建令牌的可发布密钥以及令牌 ID。

就像评论中提到的那样,无论是测试密钥还是实时模式密钥,您都不应该共享您的秘密 API 密钥。您应该立即滚动测试模式密钥:https://stripe.com/docs/keys#rolling-keys

此外,令牌和费用是遗留 API,您应该改用 PaymentMethods 和 PaymentIntents。请参阅 https://docs.stripe.com/ payments/ payment-intents/migration/charges 了解 Charges 和 PaymentIntents 之间的差异。

您还可以参考 https://docs.stripe.com/ payments/quickstart 获取可供使用的示例。

© www.soinside.com 2019 - 2024. All rights reserved.