使用 Laravel 5.2 使用 StripeJS 单次充电
Single Charge With StripeJS using Laravel 5.2
我在网上绝对找不到任何可以帮助我将 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
文档到此结束。有什么帮助吗?我更愿意使用收银台,但我找不到任何基于 "non-subscription" 的使用文档,而且 laravel 网站也没有太大帮助。
我推荐使用收银台。在你的作曲家要求:
"laravel/cashier": "~6.0"
您还需要在 config/app.php 中添加提供商:
Laravel\Cashier\CashierServiceProvider::class
在 config/services 下添加您的 Stripe API 键,这就是设置的全部内容。要使用条纹购买单个产品,只需集成条纹购买按钮:
{!! 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() !!}
或者,您可以集成一个购物车并将整个购买过程从那里传递到 stripe,但这稍微复杂一些。我强烈推荐这个,它帮助我解决了我的商店的所有问题:https://leanpub.com/easyecommerce
从表单路由开始,您将传递到一个控制器方法,在该方法中获取请求:
public function index(Request $request)
{ ....
您可以var_dump查看条纹按钮传递的各种数据。
凯文的回答很好,无论如何,如果你仍然对你的代码有什么问题感兴趣的话
这是解决方案
在控制器中尝试此代码
首先你需要添加这个包
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 相关的错误,您可以先创建客户然后收费
我在网上绝对找不到任何可以帮助我将 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
文档到此结束。有什么帮助吗?我更愿意使用收银台,但我找不到任何基于 "non-subscription" 的使用文档,而且 laravel 网站也没有太大帮助。
我推荐使用收银台。在你的作曲家要求:
"laravel/cashier": "~6.0"
您还需要在 config/app.php 中添加提供商:
Laravel\Cashier\CashierServiceProvider::class
在 config/services 下添加您的 Stripe API 键,这就是设置的全部内容。要使用条纹购买单个产品,只需集成条纹购买按钮:
{!! 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() !!}
或者,您可以集成一个购物车并将整个购买过程从那里传递到 stripe,但这稍微复杂一些。我强烈推荐这个,它帮助我解决了我的商店的所有问题:https://leanpub.com/easyecommerce
从表单路由开始,您将传递到一个控制器方法,在该方法中获取请求:
public function index(Request $request)
{ ....
您可以var_dump查看条纹按钮传递的各种数据。
凯文的回答很好,无论如何,如果你仍然对你的代码有什么问题感兴趣的话
这是解决方案
在控制器中尝试此代码
首先你需要添加这个包
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 相关的错误,您可以先创建客户然后收费