|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Service\Braintree; |
|
4
|
|
|
|
|
5
|
|
|
use Braintree\Customer; |
|
6
|
|
|
use Braintree\Exception\NotFound; |
|
7
|
|
|
use Braintree\Result\Error; |
|
8
|
|
|
use Braintree\Result\Successful; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class CustomerService |
|
13
|
|
|
* @package App\Service\Braintree |
|
14
|
|
|
*/ |
|
15
|
|
|
class CustomerService extends AbstractBraintreeService |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @return array |
|
19
|
|
|
*/ |
|
20
|
|
|
public function listCustomers(): array |
|
21
|
|
|
{ |
|
22
|
|
|
$customers = []; |
|
23
|
|
|
$customersList = $this->gateway->customer()->search([ |
|
24
|
|
|
|
|
25
|
|
|
]); |
|
26
|
|
|
foreach ($customersList as $customer) { |
|
27
|
|
|
$customer = [ |
|
28
|
|
|
'id' => $customer->id, |
|
29
|
|
|
'full_name' => $customer->firstName . ' ' . $customer->lastName, |
|
30
|
|
|
'email' => $customer->email, |
|
31
|
|
|
'available_payment_methods' => count($customer->paymentMethods) |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
if ($customer['full_name'] && $customer['email']) { |
|
35
|
|
|
array_unshift($customers, $customer); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
return $customers; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $customerId |
|
43
|
|
|
* @return Customer |
|
44
|
|
|
* @throws NotFound |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getCustomer(string $customerId): Customer |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->gateway->customer()->find($customerId); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param array $customerData |
|
53
|
|
|
* @return Error|Successful|Exception |
|
54
|
|
|
*/ |
|
55
|
|
|
public function createCustomer(array $customerData) |
|
56
|
|
|
{ |
|
57
|
|
|
$requiredFieldKeys = ['firstName', 'lastName', 'email', 'company', 'website', 'phone']; |
|
58
|
|
|
foreach ($requiredFieldKeys as $key) { |
|
59
|
|
|
if (!array_key_exists($key, $customerData) || empty($customerData[$key])) { |
|
60
|
|
|
return new Exception('Unable to create customer, missing key: ' . $key); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
return $this->gateway->customer()->create($customerData); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|