首页 > 解决方案 > 尝试使用 Braintree 在 Laravel 上提交付款

问题描述

我遇到了这个问题,我有 3 个无线电输入,每个都有一个值(来自数据库的赞助价格),但是当我尝试测试交易时,它给了我这个错误:在此处输入图像描述

更新:现在之前的错误消失了,但是在提交表单时它返回了这个-> “消息出现错误:金额是无效格式。无法确定付款方式。”

这是视图,admin.musicians.sponsor

@extends('layouts.app')

@section('content')
    <div class="container">
        <h1>Sponsorizza il tuo profilo!</h1>
        <p>Scegli il piano più adatto a te.</p>

        <div id="dropin-container">
            <form method="post" id="payment-form" action="{{url('/checkout')}}">
                @csrf
                @method('POST')
                <section>
                   
                    @foreach ($sponsorships as $sponsorship)
                        <div>
                            <input type="radio" name="sponsorships[]" id="sponsorships-{{ $sponsorship->id }}" value="{{ $sponsorship->price }}">
                            <label for="sponsorship-{{ $sponsorship->id }}">
                                <strong>{{ $sponsorship->name }}:</strong> {{ $sponsorship->description }}
                            </label>
                        </div>
                    @endforeach
    
                    <div class="bt-drop-in-wrapper">
                        <div id="bt-dropin"></div>
                    </div>
                </section>
    
                <input id="nonce" name="payment_method_nonce" type="hidden" />
                <button class="button btn btn-primary" type="submit"><span>Test Transaction</span></button>
            </form>
        </div>
        <button id="submit-button" class="btn btn-success">Request payment method</button>

        <div class="d-flex justify-content-between align-items-center">
            <a class="btn btn-secondary" href="{{ route('admin.welcome') }}">
                <i class="far fa-hand-point-left text-white"></i>
            </a>
        </div>
    </div>
<script src="https://js.braintreegateway.com/web/dropin/1.31.2/js/dropin.min.js"></script>
<script>
    var form = document.querySelector('#payment-form');
    var client_token = "{{ $token }}";
    braintree.dropin.create({
        authorization: client_token,
        selector: '#bt-dropin',
        // paypal: {
        // flow: 'vault'
        // },
/*               option : {
            verifyCard : true
        } */
    }, function (createErr, instance) {
        if (createErr) {
        console.log('Create Error', createErr);
        return;
        }
        form.addEventListener('submit', function (event) {
        event.preventDefault();
        instance.requestPaymentMethod(function (err, payload) {
            if (err) {
            console.log('Request Payment Method Error', err);
            return;
            }
            // Add the nonce to the form and submit
            document.querySelector('#nonce').value = payload.nonce;
            form.submit();
        });
        });
    });
</script>
@endsection

这些是 config/services.php 文件中 Braintree 的设置

'braintree' => [
        'environment' => env('BT_ENVIRONMENT', 'sandbox'),
        'merchantId' => env('BT_MERCHANT_ID'),
        'publicKey' => env('BT_PUBLIC_KEY'),
        'privateKey' => env('BT_PRIVATE_KEY'),
    ],

这是 MusicianController 的 showSponsorPage 函数

 public function showSponsorPage() {
        
        $gateway = new Braintree\Gateway([
            'environment' => config('services.braintree.environment'),
            'merchantId' => env('BT_MERCHANT_ID'),
            'publicKey' => env('BT_PUBLIC_KEY'),
            'privateKey' => env('BT_PRIVATE_KEY')
        ]);
    
        $token = $gateway->ClientToken()->generate();

        $user = Auth::user();
        $sponsorships = Sponsorship::all();
        return view('admin.musicians.sponsor', compact('user', 'token', 'sponsorships'));
    }

这些都是涉及的路线

Route::middleware('auth')
    ->namespace('Admin')
    ->name('admin.')
    ->prefix('admin')
    ->group(function() {

        Route::get('/', 'HomeController@index')->name('welcome');
        Route::resource('musicians', 'MusicianController');

        Route::get('musicians/{id}/sponsor', 'MusicianController@showSponsorPage')->name('musicians.sponsor');

        // Route::get('/payment/make', 'SponsorshipController@make')->name('payment.make');
        // Route::get('/payment', 'SponsorshipController@payment')->name('musicians.payment');
        Route::post('/checkout', function (Request $request) {
            $gateway = new Braintree\Gateway([
                'environment' => config('services.braintree.environment'),
                'merchantId' => env('BT_MERCHANT_ID'),
                'publicKey' => env('BT_PUBLIC_KEY'),
                'privateKey' => env('BT_PRIVATE_KEY')
            ]);
        
            $amount = $request->amount;
            $nonce = $request->payment_method_nonce;
        
            $result = $gateway->transaction()->sale([
                'amount' => $amount,
                'paymentMethodNonce' => $nonce,
                'customer' => [
                    'firstName' => 'Tony',
                    'lastName' => 'Stark',
                    'email' => 'tony@avengers.com',
                ],
                'options' => [
                    'submitForSettlement' => true
                ]
            ]);
        
        
            if ($result->success) {
                $transaction = $result->transaction;
                // header("Location: transaction.php?id=" . $transaction->id);
        
                return back()->with('success_message', 'Transaction successful. The ID is:'. $transaction->id);
            } else {
                $errorString = "";
        
                foreach ($result->errors->deepAll() as $error) {
                    $errorString .= 'Error: ' . $error->code . ": " . $error->message . "\n";
                }
        
                // $_SESSION["errors"] = $errorString;
                // header("Location: index.php");
                return back()->withErrors('An error occurred with the message: '.$result->message);
            }
        });
    }); 

路线在此处输入图像描述

标签: phplaravellaravel-7braintreebraintree-sandbox

解决方案


推荐阅读