-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpayment.php
132 lines (115 loc) · 3.56 KB
/
payment.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
<?php
//Please modify the following variables
$amount = '250'; // E.g. "250" for 2.50 EUR!
$currency = 'EUR'; // ISO 4217
$description = $_SERVER["HTTP_REFERER"];
$privateApiKey = 'TESTPRIVATEKEY';
if (isset($_POST['paymillToken'])) {
$token = $_POST['paymillToken'];
$client = request(
'clients/',
array(),
$privateApiKey
);
$payment = request(
'payments/',
array(
'token' => $token,
'client' => $client['id']
),
$privateApiKey
);
$transaction = request(
'transactions/',
array(
'amount' => $amount,
'currency' => $currency,
'client' => $client['id'],
'payment' => $payment['id'],
'description' => $description
),
$privateApiKey
);
$isStatusClosed = isset($transaction['status']) && $transaction['status'] == 'closed';
$isResponseCodeSuccess = isset($transaction['response_code']) && $transaction['response_code'] == 20000;
if ($isStatusClosed && $isResponseCodeSuccess) {
echo '<strong>Transaction successful!</strong>';
} else {
echo '<strong>Transaction not successful!</strong> <br />';
}
echo "<pre>";
var_dump($transaction);
echo "</pre>";
}
/**
* Perform HTTP request to REST endpoint
*
* @param string $action
* @param array $params
* @param string $privateApiKey
*
* @return array
*/
function requestApi($action = '', $params = array(), $privateApiKey)
{
$curlOpts = array(
CURLOPT_URL => "https://api.paymill.com/v2/" . $action,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_USERAGENT => 'paymill-paybutton-example/1.2.0',
CURLOPT_SSL_VERIFYPEER => true,
);
$curlOpts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');
$curlOpts[CURLOPT_USERPWD] = $privateApiKey . ':';
$curl = curl_init();
curl_setopt_array($curl, $curlOpts);
$responseBody = curl_exec($curl);
$responseInfo = curl_getinfo($curl);
if ($responseBody === false) {
$responseBody = array('error' => curl_error($curl));
}
curl_close($curl);
if ('application/json' === $responseInfo['content_type']) {
$responseBody = json_decode($responseBody, true);
}
return array(
'header' => array(
'status' => $responseInfo['http_code'],
'reason' => null,
),
'body' => $responseBody
);
}
/**
* Perform API and handle exceptions
*
* @param $action
* @param array $params
* @param string $privateApiKey
*
* @return mixed
*/
function request($action, $params = array(), $privateApiKey)
{
if (!is_array($params)) {
$params = array();
}
$responseArray = requestApi($action, $params, $privateApiKey);
$httpStatusCode = $responseArray['header']['status'];
if ($httpStatusCode != 200) {
$errorMessage = 'Client returned HTTP status code ' . $httpStatusCode;
if (isset($responseArray['body']['error'])) {
$errorMessage = $responseArray['body']['error'];
}
$responseCode = '';
if (isset($responseArray['body']['data']['response_code'])) {
$responseCode = $responseArray['body']['data']['response_code'];
}
return array("data" => array(
"error" => $errorMessage,
"response_code" => $responseCode,
"http_status_code" => $httpStatusCode
));
}
return $responseArray['body']['data'];
}