billplz php

mail@pastecode.io avatar
unknown
php
2 years ago
1.6 kB
4
Indexable
Never
<?php 


//function

function billplz_call_api($data, $api_key){

	$api_url = 'https://www.billplz-sandbox.com/api';
	$api_version = 'v3';
	$sep = '/';
	$ints = '?';
	
	$url = $api_url . $sep . $api_version . $sep . "bills";

	$curl = curl_init();
	curl_setopt($curl, CURLOPT_POST, 1);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_HEADER, 0);
	curl_setopt($curl, CURLOPT_USERPWD, $api_key . ":");
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

	$result = curl_exec($curl);
	curl_close($curl);

	if(!$result){
		die("Connection Failure");
	}
	
	return json_decode($result);
	
}


//set required parameter/data
$data = [];
$data['api_key'] = $billplz_api;
$data['collection_id'] = $billplz_key;
$data['description'] = "Bayaran sumbangan untuk ".$cluster->name;
$data['email'] = $user->email;
$data['name'] = $user->name;
$data['amount'] = $request['amount'] * 100;
$data['callback_url'] = route("sumbangan.callback", $payment->id);
$data['redirect_url'] = route("app.customer.sumbangan.complete", $payment->id);

//call the function
$result = billplz_call_api($data, $data['api_key']);

//handle the result
if($result && $result->id){
    
    $payment->payment_id = $result->id;
    $payment->payment_url = $result->url;
    $payment->payment_redirect = $result->redirect_url;
    $payment->payment_callback = $result->callback_url;
    $payment->description = $result->description;
    $payment->save();
    
    //redirect to payment page
    return redirect($result->url);

}

?>