带有条纹的账户余额系统
问题描述:
在过去的两天里,我一直在试图理解Stripe是如何工作的。我想要构建的是一个简单的系统,它允许用户向网站上的帐户添加资金。 我遵循我在互联网上找到的使用Laravel Cashier的教程,但正如我在laravel文档中看到的,如果我需要执行单次收费,我应该直接使用Stripe。问题是,有没有这个应该怎么用laravel做很多教程..带有条纹的账户余额系统
这是我到目前为止有:
VIEW:
<form class="app-form" style="margin-bottom: 0px;" action="/add-funds" method="POST">
{{ csrf_field() }}
<select id="funds-options" style="width: 20%; margin-bottom: 20px;" name="add-funds-select">
<option value="30">$30</option>
<option value="50">$50</option>
<option value="100">$100</option>
<option value="200">$200</option>
<option value="300">$300</option>
<option value="500">$500</option>
</select>
<p style="margin-bottom: 0px;">
<script src="https://checkout.stripe.com/checkout.js"></script>
<button id="customButton">Purchase</button>
<script>
var handler = StripeCheckout.configure({
key: '{{ getenv('STRIPE_KEY') }}',
image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
locale: 'auto',
token: function(token) {
// You can access the token ID with `token.id`.
// Get the token ID to your server-side code for use.
}
});
document.getElementById('customButton').addEventListener('click', function(e) {
// Open Checkout with further options:
var userAmount = $("#funds-options").val();
handler.open({
name: 'Demo Site',
description: '2 widgets',
amount: userAmount*100
});
e.preventDefault();
});
// Close Checkout on page navigation:
window.addEventListener('popstate', function() {
handler.close();
});
</script>
</p>
</form>
我有这样的选择标记,其中用户可以选择他想要添加到他的账户的金额。现在,这将从Stripe打开小部件,但只要我点击付费,我就会得到这样的信息:“您没有设置有效的可发布密钥”。 我直接使用publishable键试过这个,我能够通过这个,但一旦它进入控制器,它会抛出几乎相同的错误,像API键没有设置。
我在ENV文件中设置的键,我还引用了他们在services.php ..
ENV:即使
'stripe' => [
'model' => App\User::class,
'key' => env('STRIPE_KEY'),
'secret' => env('STRIPE_SECRET'),
],
反正:
STRIPE_KEY=pk_test_....
STRIPE_SECRET=sk_test_...
服务我通过这个“错误”我仍然不确定我是否正确地做这件事。
控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
class WalletController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('user.wallet.index');
}
public function postPayWithStripe(Request $request)
{
return $this->chargeCustomer($request->input('add-funds-select'), $request->input('stripeToken'));
}
public function chargeCustomer($amount, $token)
{
\Stripe\Stripe::setApiKey(getenv('STRIPE_SECRET'));
if (!$this->isStripeCustomer())
{
$customer = $this->createStripeCustomer($token);
}
else
{
$customer = \Stripe\Customer::retrieve(Auth::user()->stripe_id);
}
return $this->createStripeCharge($amount, $customer);
}
public function createStripeCharge($amount, $customer)
{
try {
$charge = \Stripe\Charge::create(array(
"amount" => $amount,
"currency" => "brl",
"customer" => $customer->id,
"description" => "Add funds to your account"
));
} catch(\Stripe\Error\Card $e) {
return redirect()
->route('index')
->with('error', 'Your credit card was been declined. Please try again or contact us.');
}
return $this->postStoreAmount($amount);
}
public function createStripeCustomer($token)
{
\Stripe\Stripe::setApiKey(getenv('STRIPE_SECRET'));
$customer = \Stripe\Customer::create(array(
"description" => Auth::user()->email,
"source" => $token
));
Auth::user()->stripe_id = $customer->id;
Auth::user()->save();
return $customer;
}
/**
* Check if the Stripe customer exists.
*
* @return boolean
*/
public function isStripeCustomer()
{
return Auth::user() && \App\User::where('id', Auth::user()->id)->whereNotNull('stripe_id')->first();
}
public function postStoreAmount($amount)
{
$userBalance = Auth::user()->balance;
$userBalance = $userBalance + $amount;
Auth::user()->save();
session()->flash('message', 'You just added funds to your account.');
return redirect()->route('index');
}
}
我在保存用户余额的用户表中的字段。
正如我所提到的,我遵循我在互联网上找到的教程..我不知道这应该如何工作。有什么建议么?
答
您将按照本教程进行操作。上周我将它集成到了我的购物车功能中。它很容易集成...玩得开心:)
http://justlaravel.com/integrate-stripe-payment-gateway-laravel/
答
为别人寻找如何与laravel收银员找回账户余额,我发现它是这样的:
$user = App\User::first();
echo $user->asStripeCustomer()->account_balance;
这将返回帐户余额以美分计。
谢谢!我会试一试。 –
如果您遇到任何问题,请告诉我。 –
按照你的回答,我能够完成这项工作..但现在我遇到了另一个问题..如何向用户的银行卡或银行账户汇款?就像用户退出一样。 –