如何在提交另一份表格之前不通过Stripe收取信用卡(Laravel 4项目)

时间:2015-03-26 21:18:16

标签: php jquery laravel laravel-4 stripe-payments

我正在尝试构建一个包含Stripe付款按钮的表单。它会看起来像这样(想象完成按钮褪色而没有填写所有相关信息):

Overall form

现在我要做的是让用户填写上面的信息,然后点击Pay $ 1按钮。我希望用户填写他们的cc信息(并且可以轻松检查),但我现在不希望他们像条纹表单那样付费。我希望这只是保存他们的cc信息然后,一旦完成,就可以点击"完成"上面表单上的按钮提交付款和其他信息:

Stripe form

有没有办法做到这一点?

我使用Abodeo Laravel-Stripe包来处理付款:github.com/Abodeo/laravel-stripe

以下是一些示例代码:

查看(没有所有输入,只是为了提出想法):

{{ Form::open(array('route' => 'fans.store')) }}

{{ Form::label('name', 'Name:') }}
{{ Form::text('name', null, array(
        'class'     => 'input',
        ));}}

{{ Form::label('email', 'Email:') }}
{{ Form::text('email', null, array(
        'class'     => 'input',
        ));}}

//STRIPE BUTTON
<div class="button_row">
{{ Form::button('Pay $1', array( 
        'id' => 'customButton',
        'class' => 'button',
        )); }}
</div>

//Submit
<div class="button_row">
{{Form::submit('Finish', ['class' => 'button'])}}
</div>

{{Form::close()}}

JS处理条纹:

<script>
  var handler = StripeCheckout.configure({
    key: '*************************',
    image: '/assets/images/1024icon.png',
    token: function(token) {
      // Use the token to create the charge with a server-side script.
      // You can access the token ID with `token.id`
    }
  });

  $('#customButton').on('click', function(e) {
    // Open Checkout with further options
    handler.open({
      name: '******',
      description: '**************',
      amount: 100
    });
    e.preventDefault();
  });

  // Close Checkout on page navigation
  $(window).on('popstate', function() {
    handler.close();
  });
</script>

服务器端:

我目前在我的控制器中有两个函数可以处理另一个表单输入,并分别处理Strip输入。我希望这能在一个函数中处理所有这些(单击完成时)。

public function store()
        {

            $v = Fan::validate(Input::all());

            $full_name = Input::get('name');
            $name_pieces = explode(" ", $full_name);
            $first_name = $name_pieces[0]; 
            $last_name = $name_pieces[1];

            if ( $v->passes() ) {        

            $fan = new Fan;
            $fan->first_name = $first_name;
            $fan->last_name = $last_name;
            $fan->email = Input::get('email');
            $fan->save();


            return Redirect::to('/thisview);
            }
        }

        public function stripe_pay() {
            // Use the config for the stripe secret key
            Stripe::setApiKey(Config::get('stripe.stripe.secret'));

            // Get the credit card details submitted by the form
            $token = Input::get('stripeToken');

            // Create the charge on Stripe's servers - this will charge the user's card
            try {
                $charge = Stripe_Charge::create(array(
                  "amount" => 100, // amount in cents
                  "currency" => "usd",
                  "card"  => $token,
                  "description" => 'Charge for my product')
                );

            } catch(Stripe_CardError $e) {
                $e_json = $e->getJsonBody();
                $error = $e_json['error'];
                // The card has been declined
                // redirect back to checkout page
                return Redirect::to('/artists/'.$artist_id)
                    ->withInput()->with('stripe_errors',$error['message']);
            }
            // Maybe add an entry to your DB that the charge was successful, or at least Log the charge or errors
            // Stripe charge was successfull, continue by redirecting to a page with a thank you message
            return Redirect::to('thisview/success');

        }

1 个答案:

答案 0 :(得分:1)

首先 - 您不仅仅使用Laravel收银包的任何理由:http://laravel.com/docs/5.0/billing

无论如何 - 您需要做的就是更改store()功能 - 只需将您获得的条带令牌$token = Input::get('stripeToken');保存到某个会话中。

然后您可以在提交表单后尝试收取该令牌。没有必要立即尝试充电。