Skip to content

Instantly share code, notes, and snippets.

@orditeck
Last active January 23, 2017 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save orditeck/037b8e74123fb6f5233f to your computer and use it in GitHub Desktop.
Save orditeck/037b8e74123fb6f5233f to your computer and use it in GitHub Desktop.
Webhook Snipcart
<?php
namespace POS\Webhooks\Http\Controllers;
use POS\Webhooks\Validators\Snipcart;
class SnipcartController extends WebHookController
{
public function postOrderCompleted()
{
$body = json_decode(file_get_contents('php://input'), true);
// Validate and clean the request.
// 1. Save the response from snipcart to the database if we need to take a look in the future
// 2. Throw a Bad Request (400) if anything is wrong with the request.
// 3. Set the right type for each variable, like (int) for every items quantity.
$snipcart = Snipcart::validateRequest($body);
// Create (or update) the customer
$customer = Snipcart::createOrUpdateCustomer($snipcart->customer);
// Create the order
$order = Snipcart::createOrder($snipcart->order, $snipcart->items);
// Create the transaction
Snipcart::createTransaction($order);
// Return a valid status code
return Response::json(array('error' => false), 200);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment