-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFailureCallbackHandler.php
144 lines (121 loc) · 3.74 KB
/
FailureCallbackHandler.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
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
137
138
139
140
141
142
143
144
<?php
require '../vendor/autoload.php';
use Platbox\Enum\PlatboxErrorCodeEnum;
use Platbox\Exception\PlatboxBadSignatureException;
use Platbox\Exception\PlatboxException;
use Platbox\Services\Callback\CallbackRequest;
use Platbox\Services\Callback\PlatboxCallbackHandler;
use Platbox\Services\Payment\MerchantData;
/**
* Example 1
*
* In case of bad input sign you should not accept payment
*/
$inputSign = "bad_sign";
/**
* Example of input request body.
* You can use file_get_contents('php://input')
*/
$rawData = "{
\"action\": \"test\",
\"platbox_tx_id\": \"42\",
\"platbox_tx_created_at\": \"2017-05-27T02:03:00Z\",
\"product\": \"legend_of_zelda\",
\"payment\" : {
\"amount\": 10000,
\"currency\": \"RUB\",
\"exponent\": 2
},
\"account\": {
\"id\": \"player31337\",
\"location\": 4,
\"additional\": \"Jane Doe\"
},
\"order\": \"314542341\",
\"merchant_extra\": {
\"proc_code\": 564
},
\"payer\": {
\"id\": \"**********\"
}
}";
$merchantData = new MerchantData();
$merchantData->setSecretKey("3f66f166eeb1a590b88d1f19097875ab");
$callbackRequest = new CallbackRequest($rawData, $inputSign);
$callbackHandler = new PlatboxCallbackHandler($merchantData, $callbackRequest);
try {
$callbackHandler->validateSign();
} catch (PlatboxBadSignatureException $e) {
/**
* In case of failure handling
*/
$errorResponse = $callbackHandler->getErrorCallback()->getResponse();
$errorResponse->setCode(PlatboxErrorCodeEnum::BAD_SIGNATURE);
$errorResponse->setDescription("Bad sign");
/**
* Json response
*/
$result = $callbackHandler->toJson($errorResponse);
/**
* Signature of response
*/
$generatedSign = $callbackHandler->generateSign($errorResponse);
print "Exception: ".$e->getMessage().PHP_EOL;
print "Sign to platbox: ".$generatedSign.PHP_EOL;
print "Body to platbox: ".$result.PHP_EOL;
}
/**
* Example 2
*
* You can verify input action.
*/
try {
$action = $callbackHandler->getAction();
} catch (PlatboxException $e) {
/**
* In case of failure handling
*/
$errorResponse = $callbackHandler->getErrorCallback()->getResponse();
$errorResponse->setCode(PlatboxErrorCodeEnum::BAD_REQUEST_PARAMS);
$errorResponse->setDescription("Bad action");
/**
* Json response
*/
$result = $callbackHandler->toJson($errorResponse);
/**
* Signature of response
*/
$generatedSign = $callbackHandler->generateSign($errorResponse);
print "Exception: ".$e->getMessage().PHP_EOL;
print "Sign to platbox: ".$generatedSign.PHP_EOL;
print "Body to platbox: ".$result.PHP_EOL;
}
/**
* Example 3
*
* You can verify input amount or another parameters.
*/
try {
$inputAmount = $callbackHandler->getCheckCallback()->getRequest()->getPayment()->getAmount();
//Validate input amount and amount of your order
if(true) {
throw new PlatboxException("Bad input amount");
}
} catch (PlatboxException $e) {
/**
* In case of failure handling
*/
$errorResponse = $callbackHandler->getErrorCallback()->getResponse();
$errorResponse->setCode(PlatboxErrorCodeEnum::INVALID_AMOUNT);
/**
* Json response
*/
$result = $callbackHandler->toJson($errorResponse);
/**
* Signature of response
*/
$generatedSign = $callbackHandler->generateSign($errorResponse);
print "Exception: ".$e->getMessage().PHP_EOL;
print "Sign to platbox: ".$generatedSign.PHP_EOL;
print "Body to platbox: ".$result.PHP_EOL;
}