unicodeveloper / laravel-paystack

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

Passing Phone Number, First name and Last name with the email #59

Closed chidiesobe closed 3 years ago

chidiesobe commented 5 years ago

Hello i have been trying to pass phone number , name and email to the transaction page with no success, has anyone been able to do this. For example the amount hidden field is this {{-- required in kobo --}} When you view the array gotten from dd($paymentDetails); you see that first name , last name name and phone returns null regardless of having an input like this one below, nothing gets sent

derskeal commented 5 years ago

Does your input have a name attribute?

chidiesobe commented 5 years ago

Yes and it still does not work

Huxteen commented 5 years ago

I encounter similar issue. how did you find your way around it ?

derskeal commented 5 years ago

@mcdust Can u give us more info?

Huxteen commented 5 years ago

A way around it. I collected the fullname as a form field then i used javascript to collect the fullname value, and then append it to the metadata value (when you mousedown the submit button) before submitting the form. so the information would be available together with the metadata value when i get a success message from the paystack api.

taghwo commented 5 years ago

Any solution to this yet?

taghwo commented 5 years ago

Just use the metadata array like so first_Name => auth::user()->first_Name,... If you dd($paymentDetails); you would notice the first_Name will be there in the array metadata and you can easily access it like so in your payment controller ($paymentDetails ['data']['metadata'][first_Name]); I found out also you have to add the callback route to your callback url on your paystack dashboard (yoururl.com or .test/payment/callback) And webhook url on paystack dashboard will be (yoururl.com or .test/pay)...Hope this helps!

taghwo commented 5 years ago

['first_Name']*

Emizy commented 5 years ago

@Hursteen can you give us a sample code,have been trying it but not working

taghwo commented 5 years ago

On the payment page you can assign data into metadata like this

<input type="hidden" name="metadata" value="{{ json_encode($array = [ 'investor_First_Name' => auth::user()->first_Name, 'investor_Last_Name' => auth::user()->last_Name, 'investor_Email' => auth::user()->email, 'farm_Id' => $farmId ]) }}">

In your handlegatewaycallback do this first to see the payload returned dd($paymentDetails)

if payment is successful paystack return a rich data via the handlegatewaycallback method, to access the data you sent with the metadata in the payment form,do this

   `$paymentDetails = Paystack::getPaymentData();
    $farmId = (($paymentDetails['data'] ['metadata']['farm_Id']));
    $investorName = (($paymentDetails['data'] ['metadata']['investor_First_Name']));
    $investorName = (($paymentDetails['data'] ['metadata']['investor_Last_Name']));
    $investorEmail = (($paymentDetails['data'] ['metadata']['investor_Email']));
   `

If you want to store payment details to db do like so

if($paymentDetails['data']['status'] == 'success'){ //registing new funding $funding = new Funding; $funding->user_id = Auth::User()->id; $funding->farm_id = $farmId; $funding->save(); return redirect whatever view('')->with(success message); }else{ return redirect view('')->with(failed message) }

taghwo commented 5 years ago

Sorry that my code is not formatted properly

Francis-Njoku commented 5 years ago

@taghwo Thanks for this

Emizy commented 5 years ago

@taghwo Thanks ,I will surely try it and give feedback

Emizy commented 5 years ago

@taghwo i actually tried it,its only works when i used Auth::user()->id but its doesnt work if what i want to embedded inside the metadata is part of the form ,for example

i want to add this Data network with name="network" inside the metadata id="network" type="text" class="form-control" name="network" readonly="true"

when is used your method type="hidden" name="metadata" value="{{ json_encode($array = ['Network' => $request->input('network'))]) its gives an Undefined Variable Error.

Can you kindly give a clue on how to do it.Thanks

taghwo commented 5 years ago

You get undefined variable when loading the payment page or when you initiate the pay route?

Emizy commented 5 years ago

I got undefined variable when loading the payment page..

Francis-Njoku commented 5 years ago

@Emizy Check for bugs on your payment page

taghwo commented 5 years ago

Oh sorry my bad you should not use $request in view. Will edit my initial post. How do you get the network? User inputs it?

Emizy commented 5 years ago

User inputs it? Yes(its a select option drop down)..and the input field is on the same form with the metadata hidden input field

taghwo commented 5 years ago

To test,assign any value to the network key. No $request now and check if it returns with the metadata

Emizy commented 5 years ago

Its actually worked when I passes dummy data, but how about a scenerio where by I need the user to specify the network type for the data subscription he want to buy...

Using that scenerio ,how can I affix the data inside the metadata..

Have been trying to figure this out for some weeks now...Still getting stucks in it..

taghwo commented 5 years ago

Either you use JS or you do what you should have done in the 1st place.

Change your payment flow pattern. Add a confirmatory or checkout page. Is this better for UX? I think so.

The user can atleast have another chance to actually see they clicked the right network or they press back if they made a mistake.

Payment page should just have a normal form with action="/checkout" or you can name a route for it as you wish.

In checkout or confirmation controller you capture the selected network input and assign it to a variable.

And return view("/checkout or confirmation")->with('selectedNetwork', "$selectedNetwork).

The Checkout or confirmation page will hold the paystack form.

Since you now have the the selected network available in the checkout or confimation controller,you can easily pass $selectedNetwork as value for the network key in the matadata like i did here 'farm_Id' => $farmId

ekpono commented 5 years ago

Here is my implementation. public function __construct() { $this->paystack = new Paystack(); } public function makePaymentRequest(Request $request) { $this->paystack->createCustomer([ $request['fname'] = 'my first name', $request['lname'] = 'my last name', $request['phone'] = '0828347343435', $request['email'] = 'amb@gmaiy.com' ]); $reference = $request['reference'] = $this->paystack->genTranxRef(); $request['amount'] = request()->amount; return $this->paystack->getAuthorizationUrl() ->redirectNow() ->with(['message' => 'You have successfully made payment]); }

michael-ikpefua commented 4 years ago

If you are faced with passing custom field to metadata, Here is how I solved mine using javascript <input type="hidden" name="metadata[]" id="metadata" > <button type="submit" class="btn btn-primary" onmousedown="fetchMetaValues()">Pay Now</button> JS function fetchMetaValues() { let metadata = document.getElementById('metadata'); let fullname = document.getElementById('fullname').value; let phone_number = document.getElementById('phone_number').value; let unit_price = document.getElementById('unit_price').value; let units = document.getElementById('units').value; let fid = {'name':fullname, 'phone': phone_number, 'unit':units, 'price':unit_price}; metadata.value = JSON.stringify(fid); }

michael-ikpefua commented 4 years ago

`if ($paymentDetails['data']['status'] === 'success') { $donor_name = $paymentDetails['data'] ['metadata']['fullname']; $donor_email = $paymentDetails['data'] ['metadata']['email']; $donor_phone = $paymentDetails['data'] ['metadata']['phone_number']; $unit_pricef = $paymentDetails['data'] ['metadata']['unit_price']; $units = $paymentDetails['data'] ['metadata']['units']; $amount = $paymentDetails['data'] ['amount'];

        dd($donor_phone);
    }`
goodmuyis commented 3 years ago

Some having similar issue in 2020 or beyond... I discovered or assumed that json_encode() is conflicting with htmlspecialchars() when you try to {{ $user->last_name}} this may not be true, but it is problematic for me so here is how i solve mine

<?php
$meta = ['first_name'=>$user->first_name,'last_Name'=>$user->last_name,'email'=>$user->email,'phone'=>$user->profile->phone_no];
?>
.
.
.
<input type="hidden" name="metadata" value="{{ json_encode($meta) }}" >

So everything workout well. And Yes <?php ?> tag will work inside blade template

Abdulrazakrogo commented 3 years ago

@goodmuyis Thank you for this. It works!