Stripe 的账户余额系统

Account balance system with Stripe

在过去的两天里,我一直在努力了解 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"></option>
        <option value="50"></option>
        <option value="100">0</option>
        <option value="200">0</option>
        <option value="300">0</option>
        <option value="500">0</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>

我有这个 select 标签,用户可以在其中 select 他想添加到帐户中的金额。现在,这会打开 Stripe 的小部件,但一旦我点击付款,我就会收到该信息:"You did not set a valid publishable key"。 我直接使用可发布的密钥进行了尝试,我能够传递它,但是一旦它进入控制器,它就会抛出几乎相同的错误,比如 API key was not set.

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

中引用了它们

环境:

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

服务:

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

无论如何,即使我通过了这个"error"我仍然不确定我这样做是否正确。

控制器:

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

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

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

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

对于寻找如何通过 laravel 出纳员检索帐户余额的其他人,我发现它是这样的:

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

此 returns 以美分为单位的帐户余额。