带条纹的帐户余额系统

时间:2017-04-01 08:55:37

标签: laravel laravel-5 stripe-payments laravel-5.4

在过去的两天里,我一直在努力了解Stripe的工作方式。我正在尝试构建的是一个简单的系统,可以让用户在网站上为他的帐户添加资金。 我按照我在互联网上找到的使用Laravel Cashier的教程,但是我已经阅读了laravel文档,如果我需要执行单项收费,我应该直接使用Stripe。问题是,关于如何使用laravel实现这一点的教程并不多。

这是我到目前为止所拥有的:

查看:

    <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中的小部件,但是一旦我点击付费,我就会收到该信息:“您没有设置有效的可发布密钥”。 我直接使用可发布的密钥尝试了这个,但我能够通过它,但是一旦它进入控制器,就会抛出几乎相同的错误,例如没有设置API密钥。

我在env文件中设置了密钥,我也在services.php ..

中引用它们

ENV:

STRIPE_KEY=pk_test_....
STRIPE_SECRET=sk_test_...

SERVICES:

'stripe' => [
    'model' => App\User::class,
    'key' => env('STRIPE_KEY'),
    'secret' => env('STRIPE_SECRET'),
],

无论如何,即使我通过了这个“错误”,我仍然不确定我是否正确行事。

控制器:

<?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');
     }
}

我在users表中有一个用于保存用户余额的字段。

正如我所提到的,我遵循了我在互联网上找到的教程。我不确定这应该如何运作。有什么建议吗?

2 个答案:

答案 0 :(得分:0)

您将按照本教程进行操作。我上周将它集成到我的购物车功能中。它很容易整合......有乐趣:)
  http://justlaravel.com/integrate-stripe-payment-gateway-laravel/

答案 1 :(得分:0)

对于其他寻求如何使用laravel收银员取回帐户余额的人,我发现它是这样的:

$user = App\User::first();
echo $user->asStripeCustomer()->account_balance;

这将以美分返回帐户余额。