php - 尝试使用 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);
}
});
});
解决方案
推荐阅读
- google-api - 关于google API中访问令牌和刷新令牌的问题
- game-engine - Godot 与 C# 命令行问题 EditorSettings::_get - 找不到属性
- android - 当我创建一个新的空活动时,布局 R.layout.layoutname 没有解析?
- oracle - 与数据库同步数据时出错原因:SQL 错误 [932] [42000]:ORA-00932:不一致的数据类型:预期 - 得到 BLOB
- javascript - 在 Vue Js 的 javascript 文件中获取更新的状态值
- robotframework - 将赛普拉斯与机器人测试框架集成?
- python - 如何将 tf.keras 训练的模型转移到具有相同预测结果的 caffe
- bots - 如何绕过嵌入消息的反引号?
- unix - 自动将 .xlsx 转换为 .csv 文件
- python - 如何使用 bash 脚本激活 python virtualenv 并在脚本完成后保留它?