unicodeveloper / laravel-paystack

:credit_card: :package: :moneybag: Laravel 6, 7, 8, 9, 10 and 11 Package for Paystack
https://paystack.co
MIT License
597 stars 310 forks source link

How To Handle Array From Laravel Payment Gateway #91

Closed Greatlegend12 closed 4 years ago

Greatlegend12 commented 4 years ago

IMG_20200101_171324

I got the array of information in the above screenshot after integration and making payments. Trying to add them to the database and update and and user information, I'm finding it hard. Please help me out.

odutolaabisoye commented 4 years ago

Try this

public function handleGatewayCallback()
{
    $paymentDetails = Paystack::getPaymentData();
  $user = User::find(Auth::id());
  $user->column_name = date("Y-m-d H:i:s",strtotime($paymentDetails['data']['paidAt']));
$user->another_column_name= $paymentDetails['data']['plan_object']['name'];
 $user->save();

 return redirect ( route ('welcome'));

}

If you have any question let me know.

Greatlegend12 commented 4 years ago

I used it, I got this error Screenshot (184)

Greatlegend12 commented 4 years ago

see full detail call back Screenshot (185) Screenshot (186)

odutolaabisoye commented 4 years ago

Can I see your code This is the way it work, I assume you're saving into the user table and you have registered your method in your route So to get the data, I pass the payment data into a new variable $paymentDetails = Paystack::getPaymentData();

I get the user info, $user = User::find(Auth::id());

This save the data into the column, there are 3 level First level is data, to get data id $paymentDetails['data']['id'] ; $paymentDetails['data']['authorization'] ['last4'] $paymentDetails['data']['forst_name']

You can save it like this $user->another_column_name= $paymentDetails['data']['authorization'] ['last4'] $user->save();

Paste your code here and your route, if you have any error.

Greatlegend12 commented 4 years ago

These are my codes, i created a table I called payments. Cant save, I'm getting error 500 as in the screenshot

Screenshot (206) Screenshot (207) Screenshot (208) Screenshot (209)

odutolaabisoye commented 4 years ago

Have you registered your callback URL on paystack https://dashboard.paystack.com/#/settings/developer https://websitename.com/payment/callback

Please note that your code will save into user database.

Greatlegend12 commented 4 years ago

Yes, have registered my callback on paystack

odutolaabisoye commented 4 years ago

Please change to return redirect ( route ('home'));

Greatlegend12 commented 4 years ago

I want to save the details in 'payments' table, not the user table

odutolaabisoye commented 4 years ago

You need to create a relationship between user table and payment table. Then import your model. You can now use orm or query to create a new record at each instances.

Greatlegend12 commented 4 years ago

How do I create the relationship, please help

odutolaabisoye commented 4 years ago

In user model, do this public function payments(){ return $this->hasMany(Payment::class); }

In Payment model, do this public function user() { return $this->belongsTo(User::class); }

I don't think you need a relationship here Just do this

        Payment::create([
            'user_id' => Auth::id()
            other details
        ]);