-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_pm_customers_tc.php
67 lines (55 loc) · 2.34 KB
/
fix_pm_customers_tc.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
<?php
require 'vendor/autoload.php';
require 'config.php';
use GuzzleHttp\Client;
class TuCuotaService {
private $client;
private $url;
public function __construct($api_secret, $useSandbox = true) {
$this->url = $useSandbox ? 'https://sandbox.tucuota.com/api/' : 'https://api.tucuota.com/v1/';
$this->client = new Client([
'base_uri' => $this->url,
'headers' => [
'Authorization' => 'Bearer ' . $api_secret,
'Content-Type' => 'application/json',
],
]);
}
public function getPaymentMethod($customerId) {
try {
$response = $this->client->get('payments', ['query' => ['customer_id' => $customerId]]);
$data = json_decode($response->getBody()->getContents(), true);
return $data['data'][0]['payment_method']['id'] ?? null;
} catch (Exception $ex) {
echo "Error fetching payment method: {$ex->getMessage()}\n";
return null;
}
}
public function addPmsToAllUsers() {
$endpoint = "https://api.tucuota.com/v1/customers?limit=100";
while ($endpoint) {
echo "fetching " .$endpoint . PHP_EOL;
$response = $this->client->get($endpoint);
$data = json_decode($response->getBody()->getContents(), true);
foreach ($data['data'] as $customer) {
$metadata = $customer['metadata'] ?: [];
if (!key_exists('payment_method_id', $metadata)) {
$id = $customer['id'];
$paymentMethod = $this->getPaymentMethod($id);
if (!$paymentMethod) {
echo "No paymentMethod for customer $id" . PHP_EOL;
continue;
}
$metadata['payment_method_id'] = $paymentMethod;
$response = $this->client->put('customers/' . $id, [
'json' => ['metadata' => $metadata]
]);
echo "Added $paymentMethod to customer $id" . PHP_EOL;
}
}
$endpoint = $data['links']['next'];
}
}
}
$tucuotaService = new TuCuotaService($api_secret, false); // second in false for production
$customers = $tucuotaService->addPmsToAllUsers();