This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pay_func.php
69 lines (59 loc) · 2.29 KB
/
pay_func.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
65
66
67
68
69
<?php
require_once 'config.php';
require_once 'soapclient.php';
require_once 'dao.php';
function paymentRequestReceived($serviceId) {
if (isPaid($serviceId)) {
error_log("Service already paid: $serviceId");
return true;
}
$orderId = findByServiceId($serviceId);
if (empty($orderId)) {
error_log("Received notify for unknown serviceId: $serviceId");
header("HTTP/1.0 404 Not Found");
return false;
}
error_log("Received payment for serviceId: $serviceId orderId: $orderId");
$body = createOrderStatusRequest($orderId);
$namespace = 'http://smartservice.qld.gov.au/payment/schemas/payment_api_1_3';
$result = send($body, $namespace);
if (strpos($result, '<status>PAID</status>') === false) {
error_log("Received notify for unpaid order with serviceId: $serviceId Result: $result");
header("HTTP/1.0 400 Bad Request");
return false;
}
$isWithinPaid = payServices($orderId, $serviceId, $namespace);
if ($isWithinPaid) {
error_log("Successful payment for serviceId: $serviceId");
return true;
}
error_log("OrderId: $orderId paid for but this serviceId: $serviceId is not within it");
return false;
}
function payServices($orderId, $serviceId, $namespace) {
$body = createOrderQueryRequest($orderId);
$result = send($body, $namespace);
$cleanedForPhp = str_replace('xmlns=', 'ns=', $result);
$xml = new SimpleXMLElement($cleanedForPhp);
$orderlines = $xml->xpath('//orderline');
$serviceIds = array();
foreach ($orderlines as $orderline) {
$attrs = $orderline->attributes();
$serviceIds[] = $attrs['id'];
}
setPaid($serviceIds);
return in_array($serviceId, $serviceIds);
}
function createOrderStatusRequest($orderId) {
$tokens = array("@ORDER_ID@");
$values = array("$orderId");
$template ='<OrderStatusRequest><generatedOrderId>@ORDER_ID@</generatedOrderId></OrderStatusRequest>';
return str_replace($tokens, $values, $template);
}
function createOrderQueryRequest($orderId) {
$tokens = array("@ORDER_ID@");
$values = array("$orderId");
$template ='<OrderQueryRequest><generatedOrderId>@ORDER_ID@</generatedOrderId></OrderQueryRequest>';
return str_replace($tokens, $values, $template);
}
?>