如何从Laravel Payment Gateway处理数组

时间:2018-07-14 19:36:45

标签: laravel payment-gateway

我刚刚开始通过构建带有支付平台的网站来学习laravel。 付款成功后,我dd($paymentDetails);收到一个返回的数组 imgur link to image of the returned array

我想将引用存储到用户数据库中,但可惜我不知道该怎么做。 这是我的控制器

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
}

如果我对初学者友好的阅读材料或教程会有所帮助,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

您不应将引用直接存储在users表上,而应创建一个新表来将关系设置为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')]);