Stripe创建具有订阅级别优惠券的客户


Stripe create customer with coupon at subscription level

我正在使用Stripe的php库创建一个订阅包含优惠券的客户。我不想在客户级别添加优惠券,我想在订阅级别添加。下面的代码是我目前正在使用的,根据他们的支持,这应该可以工作,但事实并非如此。我希望有人能帮忙。

$params = array(
    'card'=>'SOME_TOKEN_FROM_JS_LIBRARY',
    'plan'=>'valid_plan_id',
    'coupon'=>'valid_coupon_id'
);
$c = Stripe_Customer::create($params);
if(!empty($params['coupon'])){
    $c->updateSubscription(array(
        'plan'=>$params['plan'],
        'coupon'=>$params['coupon']
    ));
}

这成功地创建了客户,并使用所应用的优惠券对卡进行收费。

最终使用了下面的代码,它完成了我希望它做的事情。此外,下面代码的目标是,如果由于任何原因收费失败,则删除客户,这样我就不会留下不属于任何地方的客户。

$_c = Stripe_Customer::create($params);
try{
    $_c->subscriptions->create(array('plan'=>$plan, 'coupon'=>$coupon));
}catch(Exception $e){
    $_c->delete();
    throw new Exception($e->getMessage());  
}
$c = Stripe_Customer::retrieve($_c->id); //to get the customer object with the latest data