<?php
$test_paystack = "sk_test_05ad917a33c357b0c978dca1f6d4ec2255630709";
$live_paystack = "sk_live_f7669e5d6cf5d8355bd64fb883928237149bc026";

function createCustomer($email,$fname, $lname, $phone){
    global $live_paystack,$test_paystack;
    $result = array();
    $url = 'https://api.paystack.co/customer';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $live_paystack]);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array("email"=>$email, "first_name"=>$fname,"last_name"=>$lname,"phone"=>$phone)));
    $request = curl_exec($ch);
    curl_close($ch);
    if ($request) {
    $result = json_decode($request, true);
    if($result){
      if(array_key_exists("data", $result) && $result['data'] && $result["status"]){
            return $result["data"]["customer_code"];
      }else{
        return "";
      }
    }else{
      return "";
    }
  }else{
    return "";
  }
}

function validateCustomer($customer,$fname, $lname, $bvn){
    global $live_paystack,$test_paystack;
    $result = array();
    $url = "https://api.paystack.co/customer/$customer/identification";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $live_paystack]);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array("type"=>"bvn", "first_name"=>$fname,"last_name"=>$lname,"value"=>$bvn,"country"=>"NG")));
    $request = curl_exec($ch);
    curl_close($ch);
    if ($request) {
    $result = json_decode($request, true);
    print_r($result);
    if($result){
      if( (in_array("status", array_keys($result)) && $result["status"]) ||  (in_array("message", array_keys($result)) && strtolower($result["message"]) == "customer already identified")){
            return true;
      }
    }
  }
  return false;
}

function validateTransfer($ref){
    global $live_paystack,$test_paystack;
    $result = array();
    $url = "https://api.paystack.co/transfer/$ref";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $live_paystack]);
    $request = curl_exec($ch);
    curl_close($ch);
    if ($request) {
    $result = json_decode($request, true);
    if($result){
      if( (in_array("status", array_keys($result)) && $result["status"]) ||  (in_array("data", array_keys($result)) && ( strtolower($result["data"]["status"]) != "failed" || strtolower($result["data"]["status"]) != "abandoned") )){
            return true;
      }
    }
  }
  return false;
}

function createAccount($cus,$fname,$lname,$phone){
    global $live_paystack,$test_paystack;
    $result = array();
    $url = 'https://api.paystack.co/dedicated_account';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $live_paystack]);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array("customer"=>$cus,"preferred_bank"=>"wema-bank","first_name"=>$fname,"last_name"=>$lname,"phone"=>$phone)));
    $request = curl_exec($ch);
    echo $request."<br />";
    curl_close($ch);
    if ($request) {
    $result = json_decode($request, true);
    if($result){
      if(array_key_exists("data", $result) && $result['data'] && $result["status"]){
            return array("number"=>$result["data"]["account_number"],"bank"=>$result["data"]["bank"]["name"]);
      }else{
        return "";
      }
    }else{
      return "";
    }
  }else{
    return "";
  }
}

function charge_returning($auth,$amount,$email){
    global $live_paystack,$test_paystack;
    $result = array();
    $url = 'https://api.paystack.co/transaction/charge_authorization';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $live_paystack]);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,"authorization_code=$auth&amount=$amount&email=$email");
    $request = curl_exec($ch);
    
    curl_close($ch);
    if ($request) {
    $result = json_decode($request, true);
    if($result && array_key_exists("status", $result)){
      if(in_array("data", array_keys($result))){
        if(in_array("status", array_keys($result))){
            if($result['data']['status'] == "success"){
                return 1;
            }else{
                return 0;
            }
        }else{
          return 0;
        }
      }else{
        return 0;
      }
    }else{
      return 0;
    }
  }else{
    return 0;
  }
}

function request_transfer($amount,$code,$note,$reference){
    global $live_paystack,$test_paystack;
    $result = array();
    $url = 'https://api.paystack.co/transfer';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $live_paystack]);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,"source=balance&amount=".(intval($amount)* 100)."&recipient=$code&reference=$reference&reason=".($note ?? "Payout of NGN$amount as requested from FundsBridge."));
    $request = curl_exec($ch);
    
    curl_close($ch);
    if ($request) {
    $result = json_decode($request, true);
    print_r($result);
    if($result && array_key_exists("status", $result)){
      if(in_array("data", array_keys($result))){
        if(in_array("status", array_keys($result))){
            if($result['data']['status'] == "success" || $result['data']['status'] == "pending" || $result['data']['status'] == "otp"){
                return $result['data']['transfer_code'];
            }else{
                return 0;
            }
        }else{
          return 0;
        }
      }else{
        return 0;
      }
    }else{
      return 0;
    }
  }else{
    return 0;
  }
}
?>