This repository has been archived by the owner on Nov 9, 2018. It is now read-only.
forked from PrestaShopCorp/pigmbhpaymill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webHookEndpoint.php
64 lines (56 loc) · 2.08 KB
/
webHookEndpoint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* 2012-2014 PAYMILL
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
*
* @author PAYMILL <[email protected]>
* @copyright 2012-2014 PAYMILL
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
include_once(dirname(__FILE__).'/../../config/config.inc.php');
include_once(dirname(__FILE__).'/paymill/v2/lib/Services/Paymill/Transactions.php');
include_once(dirname(__FILE__).'/pigmbhpaymill.php');
$request = Tools::jsonDecode(Tools::file_get_contents('php://input'), true);
if (validateNotification($request))
{
$order_id = getOrderIdFromNotification($request['event_resource']['transaction']['description']);
$new_order_state = Configuration::get('PS_OS_REFUND');
$order = new Order($order_id);
$history = new OrderHistory();
$history->id_order = (int)$order->id;
$history->changeIdOrderState($new_order_state, (int)$order->id);
$history->add(true);
echo 'OK';
}
function validateNotification($notification)
{
$result = false;
if (isNotificationFormatValid($notification) && $notification['event_type'] === 'refund.succeeded')
{
$transaction_object = new Services_Paymill_Transactions(
Configuration::get('PIGMBH_PAYMILL_PRIVATEKEY'), 'https://api.paymill.com/v2/'
);
$id = $notification['event_resource']['transaction']['id'];
$transaction_result = $transaction_object->getOne($id);
$result = isset($transaction_result['id']) && $transaction_result['id'] === $id;
}
return $result;
}
function isNotificationFormatValid($notification)
{
return isset($notification) && isset($notification['event_type']) && isset($notification['event_resource']['transaction']['id']);
}
function getOrderIdFromNotification($transaction_description)
{
$regex_pattern = '/OrderID: (\d+)/i';
$matches = array();
if (preg_match($regex_pattern, $transaction_description, $matches))
return (int)$matches[1];
return false;
}