-
Notifications
You must be signed in to change notification settings - Fork 2
/
subscription.php
136 lines (118 loc) · 3.52 KB
/
subscription.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
<?php
//Please modify the following variables
$amount = '250'; // E.g. "250" for 2.50 EUR!
$currency = 'EUR'; // ISO 4217
$description = $_SERVER["HTTP_REFERER"];
$interval = '1 MONTH'; //Defining how often the client should be charged. Format: number DAY | WEEK | MONTH | YEAR
$offerName = 'Testoffer'; //Your name for this offer
$privateApiKey = 'TESTPRIVATEKEY';
if (isset($_POST['paymillToken'])) {
$token = $_POST['paymillToken'];
$client = request(
'clients/',
array(),
$privateApiKey
);
$payment = request(
'payments/',
array(
'token' => $token,
'client' => $client['id']
),
$privateApiKey
);
$offer = request(
'offers/',
array(
'amount' => $amount,
'currency' => $currency,
'interval' => $interval,
'name' => $offerName
),
$privateApiKey
);
$subscription = request(
'subscriptions/',
array(
'client' => $client['id'],
'payment' => $payment['id'],
'offer' => $offer['id']
),
$privateApiKey
);
if (isset($subscription['offer']['subscription_count']['active']) && ($subscription['offer']['subscription_count']['active'])) {
echo '<strong>Subscription successful!</strong>';
} else {
echo '<strong>Subscription not successful!</strong>';
}
echo "<pre>";
var_dump($subscription);
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'];
}
return array("data" => array(
"error" => $errorMessage,
"http_status_code" => $httpStatusCode
));
}
return $responseArray['body']['data'];
}