Php 使用条带签出允许客户购买产品

Php 使用条带签出允许客户购买产品,php,stripe-payments,Php,Stripe Payments,在我看来,使用Stripe应该是一种非常简单的方法,我还没有找到答案。使用Stripe Checkout,我如何允许一个人为我已经在仪表板的“产品”部分创建的产品付款?我找到的所有文档都显示了如何检索产品数据等,这很好,但实际上并没有解释如何允许客户通过结帐购买产品。我使用的是PHP,但我非常乐意看到任何语言中的任何示例都能遵循这一轨迹。如果您试图使用checkout.js或stripe元素来实现这一点,这是不可能的。您需要通过以下方式处理此服务器: 首先获取一个令牌,该令牌表示客户使用的卡 订

在我看来,使用Stripe应该是一种非常简单的方法,我还没有找到答案。使用Stripe Checkout,我如何允许一个人为我已经在仪表板的“产品”部分创建的产品付款?我找到的所有文档都显示了如何检索产品数据等,这很好,但实际上并没有解释如何允许客户通过结帐购买产品。我使用的是PHP,但我非常乐意看到任何语言中的任何示例都能遵循这一轨迹。

如果您试图使用checkout.js或stripe元素来实现这一点,这是不可能的。您需要通过以下方式处理此服务器:

首先获取一个令牌,该令牌表示客户使用的卡 订阅

脚本:

    $('.btn-save-sub').click(function () {
         //if your customer has chosen a plan, for example 
          var plan = $("#plan_id").val();
          var stripe = Stripe(//your public key here );
          var elements = stripe.elements();

          /**create and mount cc and cc exp elements**/
          var card = elements.create('card'); //complete card element, can be customized
          card.mount('#card-element');

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

           var form = document.getElementById('subscription_add_new_source');

           stripe.createToken(card).then(function(result) {
                if (result.error) {
                    var errorElement = document.getElementById('card-errors');
                    errorElement.textContent = result.error.message;
                }else{
                    //post result.token.id  and plan_id to your server, this token represents the card you will be using 
                }
        });
    });
现在,如果您决定允许客户选择一个计划,服务器端就有一个令牌和一个计划id。现在,我们将使用stripe的

拥有客户后,您将首先创建一个卡对象,并将其指定为默认源:

//create new card
$new_card = $stripe_customer->sources->create(array('sources'=>$posted_token));

//assign newly created card as customer's default source
//subscriptions can only charge default sources 
$stripe_customer->default_source = $new_card->id; 

//finally, create a subscription with the plan_id 
$subscription = \Stripe\Subscription::create(
        array(
            'customer' => $stripe_customer->id,
            'items' => array(
                array(
                    'plan' => $posted_plan_id,       
                )
            ),
            'trial_end' =>$end // represents the first day a  customer will be charged for this plan, pass a timestamp 
        )
    );

你只是想向他们收取你产品的价值?产品只是真实地描述计划,没有单独的定价。如果你没有创建订阅,那么你需要走另一条路。不,这是最简单的部分。我希望他们实际购买的产品,有一个与之相关的每月订阅费用。Stripe分配了一个产品ID,我只是想弄清楚,如果API不允许数据产品属性,人们如何购买它。它只需要一美元。你想为客户订阅一个特定的计划吗?是的。我有3个计划列在1个产品下。有一个选项,你可以用它来显示文本,如订阅,而不是支付x美元,如果使用结帐。但是,是的,你是对的,你必须创建客户并使用你从Checkout/Element获得的令牌在服务器端为他们订阅计划,回答得好。
//create new card
$new_card = $stripe_customer->sources->create(array('sources'=>$posted_token));

//assign newly created card as customer's default source
//subscriptions can only charge default sources 
$stripe_customer->default_source = $new_card->id; 

//finally, create a subscription with the plan_id 
$subscription = \Stripe\Subscription::create(
        array(
            'customer' => $stripe_customer->id,
            'items' => array(
                array(
                    'plan' => $posted_plan_id,       
                )
            ),
            'trial_end' =>$end // represents the first day a  customer will be charged for this plan, pass a timestamp 
        )
    );