使用Stripe Checkout和Omnipay创建订阅

时间:2017-07-18 22:32:40

标签: stripe-payments omnipay

我的代码创建并加载Stripe Checkout,客户输入名称和卡片详细信息,然后返回到收到令牌并用于创建Stripe客户的PHP脚本,然后发送购买请求以对其卡片收费。< / p>

如果可能,我最好如何使用同一张卡和omnipay库添加订阅?我是否使用令牌或从购买对象中获取源ID并使用它,并在购买后创建订阅?

在一个类中,在下面的代码中调用createSubscription会给出错误&#34;该客户没有附加的付款来源&#34;。

public function createSubscription($token,$customer_id,$plan,$source_id)
{
        try {
            $response = $this->gateway->createSubscription(
                                array(
                                    'customerReference' => $customer_id,
                                    'plan'              => $plan,
                                    'source'            => $token
                                )
            )->send();
            if ($response->isSuccessful()) {
                $data = $response->getData();
return $data;
            }
        } catch (Exception $e) {
            $message = "Error creating Stripe subscription: " . $e->getMessage();
            return;
        }
    }

1 个答案:

答案 0 :(得分:0)

我已经成功创建了使用令牌附加卡片详细信息的客户,然后使用“计划”创建订阅&#39;和&#39; customerReference&#39;参数,并最终使用customerReference为卡充电(为了清楚起见,省略了成功检查和错误检查):

 $response = $gateway->createCustomer(array(
     'description'       => 'Test Customer',
     'email'             => $_POST['stripeEmail'],
     'source'  => $token
 ))->send();

 $response = $gateway->createSubscription(array(
     "customerReference" => $customer_id,
     'plan' => 'plan_name',
 ))->send();

$transaction = $gateway->purchase(array(
                  'amount'   => '5.00',
                  'currency' => 'gbp',
                  'receipt_email' => 'test@test.com',
                  "description" => 'Test Order',
                  'customerReference' => $customer_id,
));