Skip to content

Instantly share code, notes, and snippets.

@gamaup
Created August 23, 2016 04:29
Show Gist options
  • Save gamaup/94dd0d95ca356f07daccba4208e3ce65 to your computer and use it in GitHub Desktop.
Save gamaup/94dd0d95ca356f07daccba4208e3ce65 to your computer and use it in GitHub Desktop.
a Tiga (WP plugin) controller to handle notifications sent by veritrans
<?php
class VeritransController {
public function index() {
global $woocommerce;
// get veritrans keys depending on environment (sandbox/production)
$vt = get_option('woocommerce_veritrans_settings');
if (empty($vt)) {
return Response::json(array("success"=>false,"error"=>"veritrans setting not found"),200,array());;
}
if ($vt['select_veritrans_environment'] == 'production') {
$serverkey = $vt['server_key_v2_production'];
} else if ($vt['select_veritrans_environment'] == 'sandbox') {
$serverkey = $vt['server_key_v2_sandbox'];
} else {
return Response::json(array("success"=>false,"error"=>"veritrans environment not set"),200,array());;
}
// parameters check
if (!Request::has('order_id') || !Request::has('status_code') || !Request::has('gross_amount') || !Request::has('signature_key')) {
return Response::json(array("success"=>false,"error"=>"invalid parameters"),200,array());;
}
$req = Request::all();
$order_id = (int)$req["order_id"];
$order_status = $req["status_code"];
$order_date = $req["transaction_time"];
$order_amount = $req["gross_amount"];
$signature_key = $req["signature_key"];
//generate signature key
$generated_signature = openssl_digest($order_id.$order_status.$order_amount.$serverkey, 'sha512');
if ($generated_signature != $signature_key) {
return Response::json(array("success"=>false,"error"=>"signature key not valid"),200,array());;
}
$order = new WC_Order($order_id);
$user_id = $order->get_user_id();
if ($order_status == "200") { //if payment success
$order->update_status( 'completed' );
}
return Response::json(array(
"success"=>true,
"order_id"=> $order_id,
"user_id"=> $user_id
),200,array());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment