Snippets

Monil Gandhi Add Webhook

Created by Monil Gandhi
<?php
use Braango\braangomodel as model;
use Braango\braangomodel\RequestHeader as rhdr;
require_once (__DIR__ . '/../../vendor/autoload.php');

/**
 *
 * @author braango
 *        
 *         Sample code showing how to get all webhooks
 *        
 *        
 *        
 */

// TEST auth token. Please contact
// sales@braango.com to have one
// created for you
Braango\Configuration::getDefaultConfiguration()->setApiKey('auth_token', 'ISNWF0P30WM0CMK');

$api_instance = new Braango\braangoapi\WebhooksApi();

// string | id of _sub_dealer_
$subdealerid = "subdealers2002";

// string | id of _personnel_
$salespersonid = "aed72631-c968-4362-a9a4-ebe5bef8310b";

// Braango\braangomodel\WebhookInput |
$webhookInput = new \Braango\braangomodel\WebhookInput();

/*
 * { "api_key": "ISNGvAzwuy4X7vAqrtV", "id": "any value",
 * "account_type": "partner" }
 */
$hrd = new rhdr();

// dealer_api_key returned
// when partner_dealer was created
$hrd->setApiKey("ISNMdzuNiKG7jhl9d9v");

// Set the account type to partner for
// virtual dealer and partner hosted
// accounts
$hrd->setAccountType("partner");

// ID that will be reflected back
$hrd->setId("create-webhook");

$webhookInput->setHeader($hrd);

$webhookInputBody = new model\WebhookInputBody();

// This is partner generated authid that partner
// uses to track the webhook
// Braango uses that to index the entry
// This needs to be unique for given
// personnel
$webhookInputBody->setAuthId("webhook-auth-id");

// Partner generated auth key for webhook
// Braango will use this in its
// request when posting message
$webhookInputBody->setAuthKey("partner-webhook-auth-key");

$webhookInputBody->setEnable(TRUE);

// Url where the webhook needs to be posted
$webhookInputBody->setPostUrl("https://test.partner.webhook/someresource");

$webhookInput->setBody($webhookInputBody);
// End create input for the webhook

try {
    $result = $api_instance->createWebhook($subdealerid, $salespersonid, $webhookInput);
    
    if ($result != null) {
        // Extract out response hdr and bdy
        $rspHdr = $result->getHeader();
        
        if ($requestHdr = ! null) {
            $rspId = $rspHdr->getId();
            $braangoRequestId = $rspHdr->getIsnRequestId();
            print_r("Response ID = " . $rspId . ", API Request Id = " . $braangoRequestId . "\n");
        }
        
        $rspBdy = $result->getBody();
        if ($rspBdy != null) {
            $status = $rspBdy->getStatus();
            
            // Typically this value should be always SUCCESS else exception will be thrown
            // Sometimes, this can be WARNING to indicate if any resource conflicted or not
            // In case of SUCCESS or WARNING, resources are always created (excepting warned sub-resources)
            print_r("Status = " . $status . "\n");
            $webhookInputBodyData = $rspBdy->getData();
            print "Webhooks : ";
            print_r($webhookInputBodyData->getRestEndPoints());
        }
    }
} catch (Exception $e) {
    echo 'Exception when calling WebhooksApi->createWebhook: ', $e->getMessage(), PHP_EOL;
}
?>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.