douzhuan4406 2014-12-30 16:12
浏览 59
已采纳

Stripe AJAX PHP和JavaScript

The below is my code I'm using for my Stripe Subscription Payments, because the site I'm implementing this into is AngularJS I want to keep the site from Refreshing so I'm opting for this AJAX option.

I have commented out a piece of the PHP which is

$charge = Stripe_Charge::create(array(
    'customer' => $customer->id,
    'amount' => $amount,
    'currency' => 'gbp'
));

If I exclude this, the payment goes through once and I'm unsure how this application is able to process the charge if there is no call for it in the PHP file.

If I include the above snippet then the charge goes through twice.

My config.php only has require_once('Stripe.php'); along with my API keys.

So I'm hoping someone could explain why the charge goes through without the charge code piece in there if my code is actually okay for me to continue with.

HTML

<button id="proBtn">Subscribe</button>

JavaScript

    Stripe.setPublishableKey('HIDDEN');

    $('#proBtn').click(function(){
        var token = function(res){
            var $input = $('<input type="hidden" name="stripeToken" />').val(res.id);
            var tokenId = $input.val();
            var email = res.email;

            setTimeout(function(){
                $.ajax({
                    url:'/assets/lib/charge.php',
                    cache: false,
                    data:{ stripeEmail : email, stripeToken:tokenId, stripePlan: 'pro' },
                    type:'POST'
                })
                .done(function(data){
                    // If Payment Success
                    console.log(data);
                    $('#proBtn').html('Thank You').addClass('disabled');
                })
                .error(function(){
                    $('#proBtn').html('Error, Unable to Process Payment').addClass('disabled');
                });
            },500);
            //$('form:first-child').append($input).submit();
        };

        StripeCheckout.open({
            key:         'HIDDEN', // Your Key
            address:     false,
            amount:      500,
            currency:    'gbp',
            name:        'Pro Account',
            description: '',
            panelLabel:  'Checkout',
            allowRememberMe: false,
            token:       token
        });
        return false;
    });

charge.php

<?php
  require_once($_SERVER['DOCUMENT_ROOT'].'/assets/lib/config.php');

  $token  = $_POST['stripeToken'];
  $email  = $_POST['stripeEmail'];

  $plan  = $_POST['stripePlan'];

  if ( $plan == 'pro' ) {
      $amount = 500;
      $amountFormat = number_format( $amount / 100, 2) ;
      $plan = 'pro';
  } 

  if ( $plan == 'team' ) {
      $amount = 2000;
      $amountFormat = number_format( $amount / 100, 2) ;
      $plan = 'team';
  } 

  Stripe_Plan::retrieve("pro");
  Stripe_Plan::retrieve("team");

  $customer = Stripe_Customer::create(array(
      'email' => $email,
      'card'  => $token,
      'plan' => $plan
  ));

try {
    /*
    $charge = Stripe_Charge::create(array(
        'customer' => $customer->id,
        'amount' => $amount,
        'currency' => 'gbp'
    ));*/
    echo 'success';
} catch(Stripe_CardError $e) {
    echo "The card has been declined";
    echo $token;
}

  print_r($token);
  echo '<br/>';
  print_r($email);
  echo '<br/>';
  echo $customer->id;

  echo '<h1>Successfully charged '.$amountFormat.'!</h1>';
?>
  • 写回答

1条回答 默认 最新

  • dongri1989 2014-12-30 16:25
    关注

    When you create a customer in Stripe it automatically charges them if you pass in a plan. You're basically saying create this customer and sign them up for this plan. That's why you are charging them twice.

    Here's a link to the Customer API:

    https://stripe.com/docs/api#create_customer

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私