Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. public function execute()
  2. {
  3. $phone = $this->getRequest()->getParam('phone');
  4. // $phone = '254720108418';
  5.  
  6. $token = $this->_mpesahelper->generateToken();
  7. $live = $this->_mpesahelper->getGeneralConfig('live_or_dev');
  8. $url = ($live == 'Yes' ? 'https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest' : 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest');
  9.  
  10. //$url = $this->_mpesahelper->getGeneralConfig('mpesa_request_url');
  11. $curl = curl_init();
  12. curl_setopt($curl, CURLOPT_URL, $url);
  13. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization:Bearer '.$token)); //setting custom header
  14.  
  15. $passkey = $this->_mpesahelper->getGeneralConfig('passkey');
  16. $paybill = $this->_mpesahelper->getGeneralConfig('my_paybill');
  17.  
  18.  
  19. date_default_timezone_set('Africa/Nairobi');
  20. $date = new \DateTime('now');
  21. $date->setTimezone(new \DateTimeZone('UTC'));
  22. $str_server_now = $date->format('YmdHis');
  23. date_default_timezone_set('UTC');
  24. $timestamp = $str_server_now;
  25.  
  26. $amount = $this->cart->getQuote()->getGrandTotal();
  27. $account_id = $this->cart->getQuote()->getId();
  28. $customerId = $this->cart->getQuote()->getCustomer()->getId();
  29.  
  30.  
  31. $curl_post_data = array(
  32. //Fill in the request parameters with valid values
  33. 'BusinessShortCode' => $paybill,
  34. 'Password' => base64_encode($paybill.$passkey.$timestamp),
  35. 'Timestamp' => $timestamp,
  36. 'TransactionType' => 'CustomerPayBillOnline',
  37. 'Amount' => number_format($amount,0, '.', ''),
  38. 'PartyA' => $this->_mpesahelper->formatPhone($phone),
  39. 'PartyB' => $paybill,
  40. 'PhoneNumber' => $this->_mpesahelper->formatPhone($phone),
  41. 'CallBackURL' => $this->_url->getUrl('safaricommpesa/mpesa/stkpushlistener'),
  42. 'AccountReference' => $account_id,
  43. 'TransactionDesc' => 'Magento Order'
  44. );
  45.  
  46. $data_string = json_encode($curl_post_data);
  47.  
  48. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  49. curl_setopt($curl, CURLOPT_POST, true);
  50. curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
  51.  
  52. $curl_response = curl_exec($curl);
  53.  
  54. $json = json_decode($curl_response,true);
  55. if(isset($json['errorCode']))
  56. {
  57. echo json_encode(['success'=>false,'message'=>$json['errorMessage']]);
  58. }
  59. elseif(isset($json['ResponseCode'])){
  60. //return json_encode(['success'=>true,'message'=>$json['ResponseDescription']]);
  61. $this->_stkpush->setData(['account_id'=>$account_id,'merchant_request_id'=>$json['MerchantRequestID'],'checkout_request_id'=>$json['CheckoutRequestID'],'phone'=>$phone,'customer_id'=>$customerId])->save();
  62. echo json_encode(['success'=>true,'message'=>$json['CustomerMessage'],'m_id'=>$json['MerchantRequestID'],'c_id' =>$json['CheckoutRequestID']]);
  63. }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement