Billplz Payment Gateway Integration with Laravel
In this blog, we will talk about the Billplz payment gateway integration with Laravel. Now, it is very important to know what is Billplz payment gateway? Billplz is an affordable Malaysian Payment Gateway. In the Laravel development framework, we can easily use Billplz payment Gateway without using any SDK or package.
Please follow the below steps for the integration. So, let’s start.
Step 1:
Create a class named Billplz where we use this function's we connect with the Billplz server and get responses from the server.
https://www.billplz.com/api/v2'; private $data = array(); private $api_key =''; private $ch; private $sep = '/'; private $end_bills = 'bills'; private $end_collections = 'collections'; function __construct( $data = array() ){ if (is_array($data) && (count($data) > 0)) { if (isset($data['api_key'])) $this->api_key = $data['api_key']; if (isset($data['host'])) $this->host = $data['host']; } } function create_collection(){ $this->ch = curl_init($this->host . $this->sep . $this->end_collections); if (isset($this->data['logo'])) { if (file_exists($this->data['logo'])) { $this->data['logo'] = '@'. $this->data['logo']; } else { $this->error = "logo file not found"; return false; } } return $this->_run(); } function create_bill(){ $this->ch = curl_init($this->host . $this->sep . $this->end_bills); return $this->_run(); } function get_bill( $bill_id ){ $this->ch = curl_init($this->host . $this->sep . $this->end_bills . $this->sep . $bill_id); return $this->_run(); } function delete_bill( $bill_id ){ $this->ch = curl_init($this->host . $this->sep . $this->end_bills . $this->sep . $bill_id); curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "DELETE"); return $this->_run(); } function set_data($data, $data2 = null) { if (is_array($data)) { foreach($data as $key => $value){ $this->data[$key] = $value; } } else if ($data2 !== null) { $this->data[$data] = $data2; } } function _run(){ if ($this->api_key == '') { $this->error = 'API key was not set'; return false; } curl_setopt($this->ch, CURLOPT_HEADER, 1); curl_setopt($this->ch, CURLOPT_USERPWD, $this->api_key . ":"); curl_setopt($this->ch, CURLOPT_TIMEOUT, 30); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE); if (count($this->data) > 0) { curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->data ); } $r = curl_exec($this->ch); curl_close($this->ch); return $r; } } ?>
Step 2:
Declare in the route where we define our function post and get a response.
Route::post('/post_book_boat', [ 'as' => 'frontend_boat_book', 'uses' => 'BookingController@postAddBooking', ]); Route::get('paywithbillplz/{billplzid}', array('as' => 'paywithbillplz','uses' => 'BookingController@getPaymentStatusBillplz',));
Step 3:
In controller we send our relevant data through postAddBooking() function. Also get bill details through getPaymentStatusBillplz() function.
api_key='XXXXXXXXXXX'; public function postAddBooking(Request $request) { $bplz = new Billplz(array('api_key' =>$this->api_key)); $bplz->set_data(array( 'collection_id' => $this->collection_id, 'email' => $request->customer_email, 'mobile' => $request->phone_initial.$request->customer_phone, 'name' => $request->customer_name, 'due_at' => date("Y-m-d"), 'amount' => $customerpayment*100, 'fixed_amount' => true, 'redirect_url' => 'https://'.$this->operator_domain.'/paywithbillplz/'.base64_encode($bookingId), 'callback_url' => 'https://bhstourtravel.travelfunfair.com' )); $result = $bplz->create_bill(); } public function getPaymentStatusBillplz($booking_id) { $booking_id = base64_decode($booking_id); $booking = BookingDetails::find($booking_id); if(!empty($booking)){ if($this->operator_id==$booking->operator_id){ if(!empty($booking->payment_details_info)){ $bill_id=json_decode($booking->payment_details_info,true)[0]['reffno_backend']; //*****Get Bill Details*****// $bplz = new Billplz(array('api_key' =>$this->api_key)); $result = $bplz->get_bill( $bill_id ); $result = explode("\n", $result); //23 for sand box and 22 for live $billplz_result=json_decode($result[22],true); } } } } } ?>
All the above code is tested by me and it is perfectly working. Hope you liked the blog about Laravel payment gateway. Please share your views in the comment section below. See you on my next blog.
Comments