-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.php
66 lines (56 loc) · 1.97 KB
/
run.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
<?php
require_once './vendor/autoload.php';
require_once './lib/functions.php';
if(!file_exists(".env")){
echo "ERROR: Create your .env file";
exit();
}
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__); //Location of .env file
$dotenv->safeLoad();
$access_token = $_ENV['GC_ACCESS_TOKEN']; //Name of .env variable
$client = new \GoCardlessPro\Client(array(
'access_token' => $access_token,
'environment' => \GoCardlessPro\Environment::SANDBOX //SANDBOX or PRODUCTION
));
$raw = file_get_contents("data.json");
$data = json_decode($raw, true);
if(!empty($data)){
foreach($data as $record){
sleep($_ENV['INTERVAL']);
try {
$result = $client->subscriptions()->create([
"params" => [
"amount" => $record['amount'],
"currency" => $record['currency'],
"interval_unit" => $record['interval_unit'],
"name" => $record['name'],
"start_date" => $record['start_date'],
"end_date" => $record['end_date'],
"links" => [
"mandate" => $record['mandate']
]
]
]);
if($result->id){
completedArray($record['mandate'],$result->id,"SUCCESS",null);
} else {
completedArray($record['mandate'],null,"ERROR","UNKNOWN");
}
} catch (\GoCardlessPro\Core\Exception\ApiException $e) {
completedArray($record['mandate'],null,"ERROR", $e->getMessage());
} catch (\GoCardlessPro\Core\Exception\MalformedResponseException $e) {
completedArray($record['mandate'],null,"ERROR", $e->getMessage());
} catch (\GoCardlessPro\Core\Exception\ApiConnectionException $e) {
completedArray($record['mandate'],null,"ERROR", $e->getMessage());
}
}
$fp = fopen('completed.json', 'w');
fwrite($fp, json_encode($completed, JSON_PRETTY_PRINT));
fclose($fp);
header('Content-Type: application/json');
echo json_encode($completed);
} else {
echo "ERROR: No Data Available";
exit();
}
?>