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 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果