Custom payment method Error
-
Dears,
I successfully created a new payment method, and the controller has 2 methods (redirect and transaction)
redirect method: am getting the amount and sending data away to an external URL, this URL will redirect me to the method transaction if successful,
Am getting success from the payment method, but in transaction, method am doing this:
use Illuminate\Http\Request; use Braintree\Gateway as Braintree_Gateway; use Braintree_Transaction; use Webkul\Sales\Repositories\InvoiceRepository; use Webkul\Sales\Repositories\OrderRepository; use Webkul\Checkout\Facades\Cart; use App\Exceptions\Handler; use Firebase\JWT\JWT; public function transaction(Request $request) { $secret = core()->getConfigData('sales.paymentmethods.zaincash.zaincash_secret'); $result= JWT::decode($request->token, $secret, array('HS256')); $result= (array) $result; print_r($result); if($result['status']=='success') { $order = $this->orderRepository->create(\Cart::prepareDataForOrder()); $this->order = $this->orderRepository->findOneWhere([ 'cart_id' => Cart::getCart()->id ]); $this->orderRepository->update(['status' => 'processing'], $this->order->id); Cart::deActivateCart(); session()->flash('order', $order); $this->invoiceRepository->create($this->prepareInvoiceData()); session()->flash('success', trans('dcb::app.payment-successfull')); } $response="success"; return $response; }
am getting this error : "message": "Call to a member function toArray() on null", on this line:
$order = $this->orderRepository->create(\Cart::prepareDataForOrder());
Why the Cart facade is empty here, even I got the amount in the first method (redirect) like this:
$cartAmount = \Cart::getCart()->grand_total;
Thanks
-
Hi @simo-kassab,
Cart::deActivateCart()
has been called. Please check your controller and methods. This issue seems to be the wrong code implementation.Please check your codes.
-
Even if I only put this in my method am getting an error
Controller:
public function transaction(Request $request) { echo Cart::getCart()->id; }
am getting this error:
-
This is my full controller:
<?php namespace MWPAY\ZainCash\Http\Controllers; use Illuminate\Http\Request; use Webkul\Sales\Repositories\InvoiceRepository; use Webkul\Sales\Repositories\OrderRepository; use Webkul\Checkout\Facades\Cart; use App\Exceptions\Handler; use Firebase\JWT\JWT; class ZainCashController extends Controller { /** * InvoiceRepository object * * @var object */ protected $invoiceRepository; /** * OrderRepository object * * @var array */ protected $orderRepository; public function __construct( OrderRepository $orderRepository, InvoiceRepository $invoiceRepository ) { $this->orderRepository = $orderRepository; $this->invoiceRepository = $invoiceRepository; } /** * Redirects to the Braintree. * * @return \Illuminate\Http\Response */ public function redirect() { $cartAmount = Cart::getCart()->grand_total; $service_type="MWPAY Cart"; //Order id, you can use it to help you in tagging transactions with your website IDs, if you have no order numbers in your website, leave it 1 //Variable Type is STRING, MAX: 512 chars $order_id="Bill_1234567890_"; // to be changed $production_cred=false; $merchentId = core()->getConfigData('sales.paymentmethods.zaincash.zaincash_merchant_id'); $msisdn = core()->getConfigData('sales.paymentmethods.zaincash.zaincash_msisdn'); $secret = core()->getConfigData('sales.paymentmethods.zaincash.zaincash_secret'); //after a successful or failed order, the user will redirect to this url $redirection_url='http://localhost/mwpay-web/public/zaincash/payment/preparetransaction'; $data = [ 'amount' => $cartAmount, 'serviceType' => $service_type, 'msisdn' => $msisdn, 'orderId' => $order_id, 'redirectUrl' => $redirection_url, 'iat' => time(), 'exp' => time()+60*60*4 ]; $newtoken =JWT::encode( $data, //Data to be encoded in the JWT $secret ,'HS256' ); if($production_cred){ $tUrl = 'https://api.zaincash.iq/transaction/init'; $rUrl = 'https://api.zaincash.iq/transaction/pay?id='; }else{ $tUrl = 'https://test.zaincash.iq/transaction/init'; $rUrl = 'https://test.zaincash.iq/transaction/pay?id='; } //POSTing data to ZainCash API $data_to_post = array(); $data_to_post['token'] = urlencode($newtoken); $data_to_post['merchantId'] = $merchentId; $data_to_post['lang'] = 'en'; $options = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data_to_post), ), ); $context = stream_context_create($options); $response= file_get_contents($tUrl, false, $context); print_r($response); //Parsing response $array = json_decode($response, true); $transaction_id = $array['id']; $newurl=$rUrl.$transaction_id; // header('Location: '.$newurl); return redirect()->away($newurl); } /** * Generate client token * * @return Mixed */ public function prepareTransaction(){ return view('zaincash::zaincash'); } /** * Perform the transaction * * @return response */ public function transaction(Request $request) { echo Cart::getCart()->id; } /** * Prepares order's invoice data for creation * * @return array */ protected function prepareInvoiceData() { $invoiceData = [ "order_id" => $this->order->id ]; foreach ($this->order->items as $item) { $invoiceData['invoice']['items'][$item->id] = $item->qty_to_invoice; } return $invoiceData; } }
view('zaincash::zaincash'):
<head> <meta charset="utf-8"> <meta name="csrf-token" content="{{ csrf_token() }}"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body> <div class="container" id="dropin-container"> <form action="./transaction" method="post" id="form"> @csrf() <h1> Zain Cash PAY </h1> <input type="hidden" id="token" name='token' value="{{$_GET['token']}}"><br><br> <input type="text" value="phone"/> <input type="submit" id="submit" name="submit" value="PAY"/> </form> </div> <script> var button = document.querySelector('#submit'); var returnRoute = "{{route('shop.checkout.success')}}" button.addEventListener('click', function () { $.post('{{ route('zaincash.payment.transaction') }}', function (response) { console.log(response); }); }); </script> </body>
-
Hi @simo-kassab,
Your cart is empty thats why you are not getting the data. Please check your controller somewhere your cart gets deactivated.
-
hi @devansh-webkul,
I sent you my controller, there is no cat deactivation anywhere please check it, I did the same thing for Braintree and another payment method and it's working fine.
Please help
-
Hi @simo-kassab,
From there I am not getting just check line by line by adding die and dump somewhere there is a small mistake nothing else.
-
dear @devansh-webkul ,
When I pay as a guest, it works fine and without error, but when I sign in, it's not reading the cart object
-
hi @gbi iq test ,
I didn't get it!