使用 Stripe 创建订阅后如何获取费用 ID? [英] How to get charge id after creating a subscription using Stripe?

查看:27
本文介绍了使用 Stripe 创建订阅后如何获取费用 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Stripe 作为支付网关.现在有一个大问题困扰着我.

I am using Stripe as a payment gateway. Now there's a big problem bothers me.

我使用以下代码创建订阅:

I used code below to create a subscription:

<?php
require_once('lib/Stripe.php');

Stripe::setApiKey(API_KEY);

$token = $_POST['stripeToken'];

$customer = Stripe_Customer::create(array(
    "card"  => $token,
    "plan"  => $_POST['plan'],
    "email" => "fakeuser@gmail.com",
));

这很好用,但我无法从 $customer 获取 Charge ID,而且我发现 Stripe API 无法得到它.

This works fine, but I can not get Charge ID from $customer, and I found out there's no way in Stripe API to get it.

创建订阅时如何获取?我真的需要Charge ID.

How to get it when creating a subscription? I really need Charge ID.

推荐答案

这正是 Stripe 的 webhooks 的用途.创建具有初始订阅的客户后,您将收到 6 条网络钩子通知:

This is exactly what Stripe's webhooks are for. After creating a customer with an initial subscription, you'll get six webhook notifications:

  1. customer.created,带有客户数据(如果您要保存 API 返回的内容,则您已经拥有这些数据)
  2. charge.succeeded(或charge.failed),其中包含您要查找的初始充电数据强>
  3. invoice.created,即关联发票
  4. invoice.payment_succeeded(或invoice.payment_failed),同时告诉你收费的状态
  5. customer.card.created,包含新卡的详细信息
  6. customer.subscription.created,包含客户订阅的详细信息.
  1. customer.created, with the customer data (which you already have if you're saving what the API returns)
  2. charge.succeeded (or charge.failed), which contains the initial charge data you're looking for
  3. invoice.created, which is the associated invoice
  4. invoice.payment_succeeded (or invoice.payment_failed), also telling you the status of the charge
  5. customer.card.created, with the details of the new card
  6. customer.subscription.created, with the details of the customer's subscription.

Stripe 的 API 与许多 API 和许多支付解决方案一样,旨在与网络钩子一起使用.如果您不利用 webhooks,您将缺少功能,而且您可能会为没有 webhooks 可以完成的事情而努力工作.

Stripe's API, like many APIs and many payment solutions, is built to be used with webhooks. If you're not taking advantage of webhooks, you're going to be missing functionality, and you're probably working too hard for what can be done without webhooks.

Stripe 致力于为您提供数据.如果您正在编写代码来轮询 Stripe,那么您的工作就太辛苦了.

Stripe works to deliver the data to you. If you're writing code to poll Stripe, you're working way, way too hard.

这篇关于使用 Stripe 创建订阅后如何获取费用 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆