简体   繁体   中英

How is an api call made to an endpoint?

I am trying to integrate an escrow payment system in a project i am working on but kind of confused about how this goes.Newbie

I have already set up the inline payment in my page along other requirements similar to stripe.

Now it has to do with settling an escrow payment and to do that When the funds are in escrow and I would like to settle the seller for the funds i would need to call the settlement endpoint. https://ravesandboxapi.flutterwave.com/v2/gpx/transactions/escrow/settle This is a sample request

{
    "id": "348813", // this is the txid value returned in the v2/verify response.
    "secret_key": "FLWSECK-*************************-X" // your merchant secret key.
}

and this is a sample response

{
    "status": "success",
    "message": "SUCCESS",
    "data": "Transaction settled"
}

How exactly is the api call made using the above.

Take a look at Guzzle. It's a pretty simple PHP package to handle API routing.

http://docs.guzzlephp.org/en/stable/

$client = new GuzzleHttp\Client(['base_uri' => 'https://ravesandboxapi.flutterwave.com']);

$response = $client->request('GET', 
  '/v2/gpx/transactions/escrow/settle',
  ['json' => ['id' => '348813', 'secret_key' => 'KEY']
);

$body = $response->getBody();
$responseData = $body->getContents();

When using PHP I always use a function like this one I'm sharing which I find quite useful. I think it's quite self explanatory but in case you need further assistance please let me know.

function CallAPI($method, $url, $data, $id, $secret_key) {

    $headers = [
        "Content-type: application/json",
        "id: ".$id,
        "secret_key: " .$secret_key
    ];

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    switch ($method) {
        case "GET":
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
            break;
        case "POST":
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            break;
        case "PUT":
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
            break;
        case "DELETE":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            break;
    }

    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_HEADER         => false,    //return headers in addition to content
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 30,       // timeout on connect
        CURLOPT_TIMEOUT        => 30,       // timeout on response
        CURLOPT_NOBODY        =>  0,        // timeout on response
        CURLOPT_MAXREDIRS      => 9,        // stop after 10 redirects
        CURLINFO_HEADER_OUT    => true,
        CURLOPT_SSL_VERIFYPEER => true,     // Disabled SSL Cert checks
        CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
        CURLOPT_HTTPHEADER      => $headers
    ));

    $result = curl_exec($ch);
            curl_close($ch);
            return json_decode($result, true);

}

just use guzzle library. i made a simple boilerplate just for API call i used in any project

https://gist.github.com/afiqiqmal/777fc6383ffce11113dc379094ee18b4

Sample use

$result = (new ApiRequest())
     ->baseUrl("https://ravesandboxapi.flutterwave.com")
     ->setHeader([
        'id' => $id,
        'secret_key' => $secretKey
     ])
     ->requestUrl("v2/gpx/transactions/escrow/settle")
     ->fetch();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM