require_once $whmcsdir . '/includes/gatewayfunctions.php'; require_once $whmcsdir . '/includes/invoicefunctions.php'; require_once $whmcsdir . '/modules/gateways/gocardless.php'; # get gateway params using WHMCS getGatewayVariables method $gateway = getGatewayVariables('gocardless'); # sanity check to ensure module is active if (!$gateway['type']) { die("Module Not Activated"); } # set relevant API information for GoCardless module gocardless_set_account_details($gateway); # get the raw contents of the callback and decode JSON $webhook = file_get_contents('php://input'); $webhook_array = json_decode($webhook, true); # validate the webhook by verifying the integrity of the payload with GoCardless if (GoCardless::validate_webhook($webhook_array['payload']) !== true) { # we could not validate the web hook header('HTTP/1.1 400 Bad Request'); exit(__LINE__ . ': Payload could not be verified'); } # store various elements of the webhook array into params $val = $webhook_array['payload']; # base what we are doing depending on the resource type switch ($val['resource_type']) { case 'pre_authorization': # handle preauths (possible actions - cancelled, expired) switch ($val['action']) { # handle cancelled or expired preauths case 'cancelled': case 'expired': # delete related preauths
/** * Validate the payload of a webhook * * @param array $params The payload of the webhook * * @return boolean True if webhook signature is valid */ public static function validate_webhook($params) { return GoCardless::$client->validate_webhook($params); }
$pre_auth_url = GoCardless::new_pre_authorization_url($payment_details); echo ' · <a href="' . $pre_auth_url . '">New pre-authorized payment</a>'; // New bill $payment_details = array('amount' => '30.00', 'name' => 'Donation', 'user' => array('first_name' => 'Tom', 'last_name' => 'Blomfield', 'email' => '*****@*****.**')); $bill_url = GoCardless::new_bill_url($payment_details); echo ' · <a href="' . $bill_url . '">New bill</a></p>'; echo 'NB. The \'new bill\' link is also a demo of pre-populated user data'; echo '<h2>API calls</h2>'; echo 'GoCardless_Merchant::find(\'258584\')'; echo '<blockquote><pre>'; $merchant = GoCardless_Merchant::find('258584'); print_r($merchant); echo '</pre></blockquote>'; echo 'GoCardless_Merchant::find(\'258584\')->pre_authorizations()'; echo '<blockquote><pre>'; $preauths = GoCardless_Merchant::find('258584')->pre_authorizations(); print_r($preauths); echo '</pre></blockquote>'; echo 'GoCardless_PreAuthorization::find(\'992869\')->create_bill($bill_details)'; echo '<blockquote><pre>'; $pre_auth = GoCardless_PreAuthorization::find('013M018V0K'); $bill_details = array('amount' => '15.00'); $bill = $pre_auth->create_bill($bill_details); print_r($bill); echo '</pre></blockquote>'; echo 'validate webhook:'; echo '<blockquote><pre>'; $webhook_json = '{"payload":{"bills":[{"id":"880807"},{"status":"pending"},{"source_type":"subscription"},{"source_id":"21"},{"uri":"https:\\/\\/sandbox.gocardless.com\\/api\\/v1\\/bills\\/880807"}],"action":"created","resource_type":"bill","signature":"f25a611fb9afbc272ab369ead52109edd8a88cbb29a3a00903ffbce0ec6be5cb"}}'; $webhook = json_decode($webhook_json, true); var_dump(GoCardless::validate_webhook($webhook['payload'])); echo '</pre></blockquote>';
/** * Accept the raw json response and validate it * @param $webhook * @return bool */ public function validateWebhook($webhook) { $webhook_array = json_decode($webhook, true); return \GoCardless::validate_webhook($webhook_array['payload']); }
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/init.php'; $webhook = file_get_contents('php://input'); $webhook_array = json_decode($webhook, true); $webhook_valid = GoCardless::validate_webhook($webhook_array['payload']); if ($webhook_valid == TRUE) { header('HTTP/1.1 200 OK'); } else { header('HTTP/1.1 403 Invalid signature'); }
public function processCallback() { $webhook = file_get_contents('php://input'); $webhook_array = json_decode($webhook, true); if (\GoCardless::validate_webhook($webhook_array['payload']) == true) { header('HTTP/1.1 200 OK'); foreach ($webhook_array['payload']['bills'] as $bill) { $orders = $this->orderFactory->getByTransactionReference($bill['id']); if (count($orders) == 1) { $order = $orders->pop(); if (floatval($order->getTotalCost()) === floatval($bill['amount'])) { $this->updateOrder($order, $bill['status']); } } } } }