forked from blazeyo/commerce_platnosci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommerce_platnosci.pages.inc
136 lines (121 loc) · 4.4 KB
/
commerce_platnosci.pages.inc
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* Menu callback.
* It gets called by platnosci.pl to let us know that some ordder's payment status was updated.
*/
function commerce_platnosci_payment_update() {
// Load payment rule settings.
$methods->payment_methods = array();
rules_invoke_all('commerce_payment_methods', $methods);
$settings = $methods->payment_methods['commerce_platnosci|commerce_payment_commerce_platnosci']['settings'];
if (!empty($settings)) {
// Validate packet signature.
$sig_check = md5($settings['pos_id'] . $_POST['session_id'] . $_POST['ts'] . $settings['key2']);
if ($sig_check == $_POST['sig']) {
// Fetch transaction status and update our local copy.
$result = commerce_platnosci_fetch_status($_POST['session_id'], $settings);
commerce_platnosci_transaction_update($result, $settings);
}
}
// Tell gateway that we received the message.
print 'OK';
exit();
}
/**
* This function sends an update request to platnosci.pl after we have been
* notified that something has changed. It resurns parsed response from paygw.
*
* @param int $order_id
* @param array $settings
* @return array
*/
function commerce_platnosci_fetch_status($session_id, $settings) {
$url = 'https://www.platnosci.pl/paygw/UTF/Payment/get/txt';
$ts = time();
// Calculate request signature
$sig = md5($settings['pos_id'] . $session_id . $ts . $settings['key1']);
// Translate post variables into urlencoded query string. I don't know why but
// passing an array does not work in my environment.
$postfields = array(
'pos_id' => $settings['pos_id'],
'session_id' => $session_id,
'ts' => $ts,
'sig' => $sig,
);
$output = '';
foreach ($postfields as $key => $value) {
$output .= urlencode($key) . '=' . urlencode($value) . '&';
}
rtrim($output, '&');
// Set up curl options and send request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $output);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$result = curl_exec($ch);
curl_close($ch);
// Return gateway response parsed to array
return commerce_platnosci_parse_response($result);
}
/**
* We translate geteway response from string to structured array.
*
* @param string $response
* @return array
*/
function commerce_platnosci_parse_response($response) {
$response_data = array();
foreach (explode("\n", trim($response)) as $line) {
$row = explode(":", $line);
if (count($row) > 1) {
$response_data[trim($row[0])] = trim($row[1]);
}
}
return $response_data;
}
/**
* Creates or updates our transaction based on platnosci.pl response.
*
* @param array $response
* @param stdClass $order
* @param array $settings
*/
function commerce_platnosci_transaction_update($response, $settings) {
// Verify packet signature
$sig = md5(
$response['trans_pos_id'] . $response['trans_session_id'] .
$response['trans_order_id'] . $response['trans_status'] .
$response['trans_amount'] . $response['trans_desc'] .
$response['trans_ts'] . $settings['key2']);
if ($sig == $response['trans_sig']) {
$order = commerce_order_load($response['trans_order_id']);
$transaction = commerce_payment_transaction_load((int) $response['trans_desc2']);
$trans_status = (int) $response['trans_status'];
switch ($trans_status) {
case 99: // return 'płatność odbrana - zakończona';
$status = COMMERCE_PAYMENT_STATUS_SUCCESS;
$message = 'Complete';
break;
case 1: // return 'Nowa';
case 4: // return 'Rozpoczęta';
case 5: // return 'Oczekuje na odbiór';
$status = COMMERCE_PAYMENT_STATUS_PENDING;
$message = 'Processing';
break;
case 2: // return 'Anulowana';
case 3: // return 'Odrzucona';
case 7: // return 'płatność odrzucona, otrzymano środki od klienta po wcześniejszym anulowaniu transakcji, lub nie było możliwości zwrotu środków w sposób automatyczny';
default: // return 'błędny status - prosimy o kontakt z platnosci.pl';
$status = COMMERCE_PAYMENT_STATUS_FAILURE;
$message = 'Failed';
break;
}
$transaction->status = $status;
$transaction->message = $message;
$transaction->amount = $response['trans_amount'];
commerce_payment_transaction_save($transaction);
commerce_order_save($order);
}
}