【问题标题】:Single Charge With StripeJS using Laravel 5.2使用 Laravel 5.2 使用 StripeJS 进行单次充电
【发布时间】:2016-06-03 19:20:26
【问题描述】:

我绝对无法在网上找到任何东西来帮助我将 Stripe 与 Laravel 5.2 集成。学习这个框架一直很有挑战性,因为版本之间有很多弃用:(

无论如何,这就是我正在使用的东西

用于捕获输入的 JS 文件

$(function() {
  var $form = $('#payment-form');
  $form.submit(function(event) {
    // Disable the submit button to prevent repeated clicks:
    $form.find('.submit').prop('disabled', true);

    // Request a token from Stripe:
    Stripe.card.createToken($form, stripeResponseHandler);

    // Prevent the form from being submitted:
    return false;
  });
});
function stripeResponseHandler(status, response) {
  // Grab the form:
  var $form = $('#payment-form');
  var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');

  if (response.error) { // Problem!

    // Show the errors on the form:
    $form.find('.payment-errors').text(response.error.message);
    $form.find('.submit').prop('disabled', false); // Re-enable submission

  } else { // Token was created!

    // Get the token ID:
    var token = response.id;
    console.log(token);
    // Insert the token ID into the form so it gets submitted to the server:
    $form.append($('<input type="hidden" name="stripeToken">').val(token));

    // Submit the form:
    $form.get(0).submit();
  }
};

表单完成后,我通过表单中的action="" 属性将用户路由到{{ route('success') }}

Route::any('/success', ['as' => 'success', 'uses' =>'ChargeController@pay']);

这是我的控制器,代码由 stripe 提供...你可以看到它不起作用

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\CreateSongRequest;
use Illuminate\Foundation\Http\FormRequest;
use Billable;
use Input;

class ChargeController extends Controller
{
    public function pay(Request $request){
        if(Input::has('stripeToken')){
            $token = Input::get('stripeToken');
            $amount = 10;
// I cannot use this part even though it is in the stripe documentation
            $customer = Stripe_Customer::create(array(
                                'card' => $token
                            ));
            $charge = Stripe_Charge::create(array(
                        'customer' => $customer->id,
                        'amount' => ($amount*100),
                        'currency' => 'usd',
                        'description' => "test"
                    ));
            echo 'success!';
        }
    }
}

我正在考虑使用StripeJS documentation 代替收银员。 目前,我正在查看此错误

Fatal error: Class 'App\Http\Controllers\Stripe_Customer' not found

这就是文档结束的地方。有什么帮助吗?我更喜欢使用收银员,但我找不到任何基于“非订阅”使用的文档,而且 laravel 网站也没有多大帮助。

【问题讨论】:

  • 你有 Stripe_Customer 类吗?
  • 那个错误是在寻找一个控制器,Stripe_Controller 是否存在于那个位置?
  • @AchrafKhouadja 该课程不存在,因为没有教程告诉我该放什么。 StripeJS 文档停在那里

标签: php laravel laravel-5 laravel-5.2 laravel-cashier


【解决方案1】:

凯文的回答很好,无论如何,如果你仍然对你的代码有什么问题感兴趣

解决办法

在控制器中试试这个代码

首先你需要添加这个包

composer require stripe/stripe-php 

那就试试

class ChargeController extends Controller
{

    public function __construct(){
     \Stripe\Stripe::setApiKey('d8e8fca2dc0f896fd7cb4cb0031ba249');
    }

    public function pay(Request $request){
        if(Input::has('stripeToken')){
            $token = Input::get('stripeToken');
            $amount = 10;

            $charge = \Stripe\Charge::create(array(
                        'customer' => $customer->id,
                        'amount' => ($amount*100),
                        'currency' => 'usd',
                        'description' => "test",
                        'card' => $token
                    ));
            echo 'charged successfuly!';
        }
    }
}

如果这引发与客户ID相关的错误,您可以先创建客户然后收费

【讨论】:

    【解决方案2】:

    我推荐使用 Cashier。在您的作曲家中要求:

    "laravel/cashier": "~6.0"
    

    您还需要在 config/app.php 中添加提供程序:

    Laravel\Cashier\CashierServiceProvider::class
    

    在 config/services 下添加您的 Stripe API 密钥,这就是设置。要将 Stripe 用于单个产品购买,只需集成 Stripe Purchase 按钮​​:

    {!! Form::open(array('url' => '/checkout')) !!}
      {!! Form::hidden('product_id', $product->id) !!}
      <script
         src="https://checkout.stripe.com/checkout.js" class="stripe-button"
         data-key="{{env('STRIPE_API_PUBLIC')}}"
         data-name="your app"
         data-billingAddress=true
         data-shippingAddress=true
         data-label="Buy ${{ $product->price }}"
         data-description="{{ $product->name }}"
         data-amount="{{ $product->priceToCents() }}">
       </script>
    {!! Form::close() !!}
    

    或者,您可以集成一个购物车并将整个购买从那里传递到条带,但这稍微复杂一些。我强烈推荐这个,它帮助我为我的商店解决了所有问题:https://leanpub.com/easyecommerce

    一旦从表单路由中,您传递到一个控制器方法,您可以在其中获取请求:

    public function index(Request $request)
    { ....
    

    您可以通过 var_dump 查看条纹按钮传递的各种数据。

    【讨论】:

    • 但是你如何在后端给卡“充值”呢?根据我对这种方法的理解,它会生成一个令牌,但你需要用它做一些事情
    • 我想通了。 /checkout 页面必须包含来自此处的 PHP 代码 stripe.com/docs/charges
    • 是的,他们最终处理,但您也可以将控制器中的所有数据作为请求获取
    • 如果你用解决方案更新你的答案,我可以给你一个绿色的复选标记:)
    • @MarkusProctor 我添加了在 laravel 端获取表单数据的方法,除此之外我不确定您还需要显示什么。这一切都被打破了。
    猜你喜欢
    • 2021-09-01
    • 2012-12-21
    • 1970-01-01
    • 2016-08-30
    • 2017-03-04
    • 2016-12-11
    • 1970-01-01
    • 2018-01-06
    • 2016-04-25
    相关资源
    最近更新 更多