【问题标题】:Stripe billing with same price as form与表单价格相同的条带式计费
【发布时间】:2016-06-27 05:00:48
【问题描述】:

所以我希望向客户(非用户)收取与条形表单中显示的金额相同的金额

<form action="/charge" method="POST">
                 {!! csrf_field() !!}
                  <script
                    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                    data-key="pk_test_key"
                    data-amount="{{ $note->price*100 }}"
                    data-name="Hello World"
                    data-description="{{$note->title}}"
                    data-image="/img/documentation/checkout/marketplace.png"
                    data-locale="auto"
                    data-currency="aud">
                  </script>
                </form>

在表格中显示的价格是相关钞票型号中列出的价格 * 100。我希望客户实际支付这个值,它会根据钞票而变化。

这是我的服务器帐单

public function charge()
{
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey("sk_test_key");

    // Get the credit card details submitted by the form
    $token = $_POST['stripeToken'];

    // Create the charge on Stripe's servers - this will charge the user's card
    try {
      $charge = \Stripe\Charge::create(array(
        "amount" => 100, // amount in cents, again
        "currency" => "aud",
        "source" => $token,
        "description" => "Example charge"
        ));
    } catch(\Stripe\Error\Card $e) {
      // The card has been declined
    }

    return 'payment succesful';
}

我需要将“金额”设置为等于票据价值。

否则,付款将通过我的收银员测试帐户进行,并且大多数情况下其他一切都需要正常工作。

但客户不是注册用户。

【问题讨论】:

    标签: php laravel stripe-payments laravel-cashier


    【解决方案1】:

    数量必须是您在服务器端计算出来的,否则人们可以修改 DOM 并传递他们想要的任何数量。

    相反,将id 存储在服务器端的缓存中,并将note 作为依赖项传递给charge 函数,然后比较2。

    /**
     * Assuming name of "view" page
     */
    public function checkout(Note $note) 
    {
        /**
         * Always forget this on each page load, so we can switch seamlessly between Notes.
         */
        Illuminate\Support\Facades\Cache::forget('nid')->put('nid', $note->id);
    }
    

    不要忘记修改 charge 路由以包含对我们的 Note 依赖项的新引用

    route::post('charge/{note}', 'Controller@charge')->name('charge');
    

    确保更新您的表单以同时传递note

    <form action="{{route('charge', $note)}}" method="POST">
    

    旁注我喜欢命名路线,但这不是必需的。

    然后在处理请求时,比较$nid$note-&gt;id

    public function charge(Illuminate\Http\Request $request, Note $note)
    {
        $nid = Illuminate\Support\Facades\Cache::get('nid');
    
        // id's match, no sneaky stuff. safe to use $note->price now
        if ($nid === $note->id) {
            //now we can process our payment.
            // Set your secret key: remember to change this to your live secret key in production
            // See your keys here https://dashboard.stripe.com/account/apikeys
            \Stripe\Stripe::setApiKey("sk_test_key");
    
            // Get the credit card details submitted by the form
            $token = $request->get('stripeToken');
    
            // Create the charge on Stripe's servers - this will charge the user's card
            try {
                $charge = \Stripe\Charge::create(array(
                    "amount" => number_format($note->price, 0, '', ''), // amount in cents, again
                    "currency" => "aud",
                    "source" => $token,
                    "description" => "Example charge"
                ));
            } catch(\Stripe\Error\Card $e) {
                // The card has been declined
            }    
        }
    }
    

    【讨论】:

    • @DanielPahor 你可以,但我避免使用session 并利用redis 作为我的缓存。我更喜欢控制和安全性。但是,请持保留态度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 2021-06-04
    相关资源
    最近更新 更多