Using Authorize.net sandbox Web UI, set up an webhook to call Symfony controller by URL. By default Auth.net sends POST request with data in JSON format. Verified at Requestb.in that the data is sent:
RAW BODY:
{"notificationId":"f803dsa2c9-32fa-4f44-8dsd-b9b2324lf9a7",
"eventType":"net.authorize.payment.authcapture.created",
"eventDate":"2017-09-19T09:29:46.9455538Z",
"webhookId":"f2a105zd-drf4-491v-ab31-9cdd4a8ad04a",
"payload":{"responseCode":21,
"authCode":"111",
"avsResponse":"M",
"authAmount":12.5,
"entityName":"transaction",
"id":"45"}
}
Then trying to retrieve POST data in Symfony Controller and write it in a sample file, return actually does nothing:
Controller code:
/**
* @Route("/webhook")
* Class DonateController
* @package AppBundle\Controller
*/
class WebHookController extends Controller
{
/**
* @Route("/test", name="webhooktest")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function WebHookAction(Request $request){
set_include_path('/var/www/project/web/');
$file = 'webhook_test.txt';
$content = $request->getContent();
$json = json_decode($content, true);
$wr = "Request object test 0: " . $request->request->all() . "\n\n";
$wr = $wr . "Object : " . $request->__toString() . "\n\n";
file_put_contents($file, $wr);
return $this->render('test/webhook.html.twig',
[
'wr' => $wr
]);
}
Unlucky, the $request contains only header, $request->request->all() returns only empty Array
Tried to extend from FOSRestController with body_listener instead of classic Symfony Controller without any difference.
config.xml part:
param_fetcher_listener: true
body_listener:
decoders:
json: fos_rest.decoder.json
Also tried to handle requests with symfony-json-request-transformer and SymfonyBundlesJsonRequestBundle, no result.
What's wrong here?
Resolved switching from HTTP to HTTPS in Authorize.net webhook configuration. By default HTTP requests with POST data are not recognized and processed as I expected it to do and no data is delivering. Just used https:// instead of http://url and all the magic happened.
Here is a simple code and it worked. I have tested it by POST with the JSON data and then let this function returned the request data to response. It worked!
/**
* @Route("/products", name="products", methods={"POST"})
*
* @param Request $request
* @return array|JsonResponse|null|object
*/
public function WebHookAction(Request $request)
{
$content = $request->getContent();
$json = json_decode($content, true);
return new JsonResponse($json);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With