如何处理来自 Laravel 支付网关的数组
How To Handle Array From Laravel Payment Gateway
我刚刚开始学习 laravel 通过构建一个带有支付平台的网站。
付款成功后,我会返回一个数组 dd($paymentDetails);
我想将引用存储到用户数据库中,但遗憾的是我不知道该怎么做。
这是我的控制器
public function handleGatewayCallback(Request $request)
{
$paymentDetails = Paystack::getPaymentData();
//dd($paymentDetails);
if ($request) {
$result = json_decode($request, true);
}
if (array_key_exists('data', $paymentDetails) && array_key_exists('status', $paymentDetails['data']) && ($paymentDetails['data']['status'] === 'success')) {
echo "Transaction was successful";
//Perform necessary action
}else{
echo "Transaction was unsuccessful";
}
// Now you have the payment details,
// you can store the authorization_code in your DB to allow for recurrent subscriptions
// you can then redirect or do whatever you want
}
如果有人向我指出对初学者有帮助的阅读材料或教程,我将不胜感激。
您不应将引用直接存储在 users
table 上,而是创建一个新的 table,将关系设置为 users
并存储引用。这样一个用户 -> HasMany -> 付款:
php artisan make:migration Payments --create=payments
// in your CreatePaymentsTable migration
$table->increments('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('reference');
...
// add more columns as needed
...
$table->foreign('user_id')->references('id')->on('users');
创建 Payment
模型,然后在您的 User
模型中添加关系:
public function payments()
{
return $this->hasMany(Payment::class);
}
现在您可以将引用存储在您的控制器中,如下所示:
auth()->user()->payments()->create(['reference' => data_get($paymentDetails, 'data.reference')]);
我刚刚开始学习 laravel 通过构建一个带有支付平台的网站。
付款成功后,我会返回一个数组 dd($paymentDetails);
我想将引用存储到用户数据库中,但遗憾的是我不知道该怎么做。 这是我的控制器
public function handleGatewayCallback(Request $request)
{
$paymentDetails = Paystack::getPaymentData();
//dd($paymentDetails);
if ($request) {
$result = json_decode($request, true);
}
if (array_key_exists('data', $paymentDetails) && array_key_exists('status', $paymentDetails['data']) && ($paymentDetails['data']['status'] === 'success')) {
echo "Transaction was successful";
//Perform necessary action
}else{
echo "Transaction was unsuccessful";
}
// Now you have the payment details,
// you can store the authorization_code in your DB to allow for recurrent subscriptions
// you can then redirect or do whatever you want
}
如果有人向我指出对初学者有帮助的阅读材料或教程,我将不胜感激。
您不应将引用直接存储在 users
table 上,而是创建一个新的 table,将关系设置为 users
并存储引用。这样一个用户 -> HasMany -> 付款:
php artisan make:migration Payments --create=payments
// in your CreatePaymentsTable migration
$table->increments('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('reference');
...
// add more columns as needed
...
$table->foreign('user_id')->references('id')->on('users');
创建 Payment
模型,然后在您的 User
模型中添加关系:
public function payments()
{
return $this->hasMany(Payment::class);
}
现在您可以将引用存储在您的控制器中,如下所示:
auth()->user()->payments()->create(['reference' => data_get($paymentDetails, 'data.reference')]);