Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Paystack Transfers snipper, to be further documented.
<?php
error_reporting(-1);
ini_set('display_errors', 1);
define('SK', '');
/// List banks
function list_banks(){
$banks = [];
$headers = ['Authorization: Bearer '.SK];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.paystack.co/bank',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HEADER => false,
));
$resp = curl_exec($curl);
if(curl_error($curl)){
echo 'error:' . curl_error($curl);
}
curl_close($curl);
$result = json_decode($resp);
if (array_key_exists('data', $result)) {
$banks = $result->data;
}
return $banks;
}
// $resp = list_banks();
function name_check($account_number, $bank_code){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.paystack.co/bank/resolve?account_number='.$account_number.'&bank_code='.$bank_code,
CURLOPT_HEADER => false,
));
$resp = curl_exec($curl);
if(curl_error($curl)){
echo 'error:' . curl_error($curl);
}
curl_close($curl);
$result = json_decode($resp);
return $result;
}
// $resp = name_check('2056852802', '033');
function create_recipient($account_number, $account_name, $bank_code){
$headers = ['Authorization: Bearer '.SK,
'Content-Type: application/json'
];
$body = [
'type' => 'nuban',
'name' => $account_name,
'account_number' => $account_number,
'bank_code' => $bank_code,
'currency' =>'NGN'
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.paystack.co/transferrecipient',
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HEADER => false,
CURLOPT_POSTFIELDS => json_encode($body)
));
$resp = curl_exec($curl);
if(curl_error($curl)){
echo 'error:' . curl_error($curl);
}
curl_close($curl);
$result = json_decode($resp);
if (array_key_exists('data', $result)) {
echo $recipient_code = $result->data->recipient_code;
}
return $result;
}
// $resp = create_recipient('2056852804','Ezra', '033');
function make_transfer($recipient_code, $reason, $amount){
$headers = ['Authorization: Bearer '.SK,
'Content-Type: application/json'
];
$body = [
'source' => 'balance',
'reason' => $reason,
'recipient' => $recipient_code,
'amount' => $amount,
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.paystack.co/transfer',
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HEADER => false,
CURLOPT_POSTFIELDS => json_encode($body)
));
$resp = curl_exec($curl);
if(curl_error($curl)){
echo 'error:' . curl_error($curl);
}
curl_close($curl);
$result = json_decode($resp);
if (array_key_exists('data', $result)) {
echo $transfer_code = $result->data->TRF_ahcs3sptibmhj0n;
}
return $result;
}
// $resp = make_transfer('RCP_lciz2z28vyh7bo6','For fun', '300000');
function verify_transfer($transfer_code){
$headers = ['Authorization: Bearer '.SK];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.paystack.co/transfer/'.$transfer_code,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HEADER => false,
));
$resp = curl_exec($curl);
if(curl_error($curl)){
echo 'error:' . curl_error($curl);
}
curl_close($curl);
$result = json_decode($resp);
if (array_key_exists('data', $result) && array_key_exists('status', $result->data) && ($result->data->status === 'success')) {
echo "Transfer was successful";
//Perform necessary action
}else{
echo "Transfer was unsuccessful";
}
return $resp;
}
// $resp = verify_transfer('TRF_ahcs3sptibmhj0n');
echo "<pre>";
print_r($resp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.