Skip to content

Instantly share code, notes, and snippets.

@Phlag
Last active March 17, 2021 15:38
Show Gist options
  • Save Phlag/0301e9980e8f766470a1ffa7f0af106e to your computer and use it in GitHub Desktop.
Save Phlag/0301e9980e8f766470a1ffa7f0af106e to your computer and use it in GitHub Desktop.
<?php
class Product
{
public function __construct()
{
add_action( 'rest_api_init', [$this, 'register_api'] );
}
public function register_api()
{
/**
* Webhook Shipping
*/
register_rest_route( 'caron/'.$this->version, '/shipping', array(
'methods' => 'POST',
'callback' => [$this, 'shipping']
));
/**
* Webhook Order
*/
register_rest_route( 'caron/'.$this->version, '/order', array(
'methods' => 'POST',
'callback' => [$this, 'order']
));
/**
* Webhook Custom payment gateway
*/
register_rest_route( 'caron/'.$this->version, '/payment-methods', array(
'methods' => 'POST',
'callback' => [$this, 'payment_methods']
));
}
public function payment_methods()
{
$this->authenticateRequest();
$json = file_get_contents('php://input');
$body = json_decode($json, true);
$publicToken = $body['publicToken'];
$mode = $body['mode'];
return [
[
'id' => 'snipcart_custom_gatway_1',
'name' => 'Carte-Cadeau',
'checkoutUrl' => get_home_url() . '/paiement-carte-cadeau'
]
];
}
public function order()
{
$this->authenticateRequest();
$json = file_get_contents('php://input');
$body = json_decode($json, true);
$output = [
'ordered' => []
];
if (is_null($body) or !isset($body['eventName'])) {
header('HTTP/1.1 400 Bad Request');
return;
}
switch ($body['eventName']) {
case 'order.completed':
// We export the full order to the client invoice system (Retail Point)
$this->exportJSON($body);
$items = $body['content']['items'];
$discounts = $body['content']['discounts'];
// For every products, we update quantity available based on our inventory management, as we get full inventory once a day
foreach ($items as $key => $item) {
$id = intval($item['id']);
$sold_today = get_field('sold_today', $id);
if ($sold_today === null) $sold_today = 0;
$out = [
'id' => $id,
'before' => $sold_today
];
$sold_today += $item['quantity'];
$out['after'] = $sold_today;
update_field('sold_today', $sold_today, $id);
$output['ordered'][] = $out;
}
// We update gift cards amount based on discount codes
$giftcard = new Giftcard;
foreach ($discounts as $key => $discount) {
$card = $giftcard->find($discount['code']);
if ($card !== false) {
$amount = $discount['amount'];
$itemsTotal = $body['content']['itemsTotal'];
$updatedAmount = $amount - $itemsTotal;
if ($updatedAmount<0) $updatedAmount=0;
$giftcard->update($card, $updatedAmount);
}
}
break;
}
return $output;
}
public function shipping()
{
$this->authenticateRequest();
$snipcart = json_decode(file_get_contents('php://input'), true);
$rates = [];
$free = false;
// Give free shipping on order's total amount requirements and province
if (isset($snipcart["content"]) && $snipcart["content"]['shippingAddressProvince']=='QC' && $snipcart["content"]['subtotal'] >= 74) {
$rates[] = [
'cost' => 0,
'description' => 'Livraison gratuite au Québec en haut de 74$'
];
$free = true;
}else if (isset($snipcart["content"]) && $snipcart["content"]['shippingAddressProvince']!='QC' && $snipcart["content"]['subtotal'] > 199) {
$rates[] = [
'cost' => 0,
'description' => 'Livraison gratuite en haut de 199$'
];
$free = true;
}
$total = 0;
foreach ($snipcart["content"]['items'] as $item) {
$total += $item['quantity'];
}
// If no free shipping available, give Purolator options based on fixed amount by the client
if (!$free && isset($snipcart["content"]) && $snipcart["content"]['shippingAddressProvince']=='QC') {
if ($total < 3) {
$rates[] = [
'cost' => 9,
'description' => 'Purolator'
];
} else if ($total < 5) {
$rates[] = [
'cost' => 12,
'description' => 'Purolator'
];
} else {
$rates[] = [
'cost' => 15,
'description' => 'Purolator'
];
}
} else if (!$free) {
$rates[] = [
'cost' => 25,
'description' => 'Purolator'
];
}
// Add local pickup to shipping options
$rates[] = [
'cost' => 0,
'description' => 'Ramassage en magasin chez Caron Chaussures - Trois-Rivières'
];
$rates[] = [
'cost' => 0,
'description' => 'Ramassage en magasin chez Caron Chaussures - Sherbrooke'
];
$rates[] = [
'cost' => 0,
'description' => 'Ramassage en magasin chez Chaussures Gaïa – Cap-de-la-Madeleine'
];
$rates[] = [
'cost' => 0,
'description' => 'Ramassage en magasin chez Chaussures Gaïa - Trois-Rivières-Ouest'
];
return [
'rates' => $rates
];
}
}
new Product;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment