Closed chidiesobe closed 3 years ago
Does your input have a name
attribute?
Yes and it still does not work
I encounter similar issue. how did you find your way around it ?
@mcdust Can u give us more info?
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.
Any solution to this yet?
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!
['first_Name']*
@Hursteen can you give us a sample code,have been trying it but not working
<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)
`$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($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) }
Sorry that my code is not formatted properly
@taghwo Thanks for this
@taghwo Thanks ,I will surely try it and give feedback
@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
You get undefined variable when loading the payment page or when you initiate the pay route?
I got undefined variable when loading the payment page..
@Emizy Check for bugs on your payment page
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?
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
To test,assign any value to the network key. No $request now and check if it returns with the metadata
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..
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
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]); }
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); }
`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);
}`
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
@goodmuyis Thank you for this. It works!
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