|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Squareetlabs\AuthorizeNet\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Squareetlabs\AuthorizeNet\AuthorizeNet; |
|
6
|
|
|
use Squareetlabs\AuthorizeNet\Models\UserGatewayProfile; |
|
7
|
|
|
use Illuminate\Support\Facades\Log; |
|
8
|
|
|
use net\authorize\api\contract\v1 as AnetAPI; |
|
9
|
|
|
use net\authorize\api\controller as AnetController; |
|
10
|
|
|
|
|
11
|
|
|
class CustomerProfileService extends AuthorizeNet |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Create a customer profile on authorize.net. |
|
15
|
|
|
* |
|
16
|
|
|
* @return AnetAPI\CreateCustomerProfileResponse |
|
17
|
|
|
* @throws \Exception |
|
18
|
|
|
*/ |
|
19
|
|
|
public function create(): AnetAPI\CreateCustomerProfileResponse |
|
20
|
|
|
{ |
|
21
|
|
|
$customerProfileDraft = $this->draftCustomerProfile(); |
|
22
|
|
|
$request = $this->draftRequest($customerProfileDraft); |
|
23
|
|
|
$controller = new AnetController\CreateCustomerProfileController($request); |
|
24
|
|
|
|
|
25
|
|
|
$response = $this->execute($controller); |
|
26
|
|
|
$response = $this->handleCreateCustomerResponse($response); |
|
27
|
|
|
|
|
28
|
|
|
if (method_exists($response, 'getCustomerProfileId') && $response->getCustomerProfileId() !== null) { |
|
29
|
|
|
$this->persistInDatabase($response->getCustomerProfileId()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $response; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Handle the response from creating a customer profile. |
|
37
|
|
|
* |
|
38
|
|
|
* @param AnetAPI\CreateCustomerProfileResponse $response |
|
39
|
|
|
* @return AnetAPI\CreateCustomerProfileResponse |
|
40
|
|
|
* @throws \Exception |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function handleCreateCustomerResponse(AnetAPI\CreateCustomerProfileResponse $response): AnetAPI\CreateCustomerProfileResponse |
|
43
|
|
|
{ |
|
44
|
|
|
if ($response->getCustomerProfileId() === null) { |
|
|
|
|
|
|
45
|
|
|
$message = $response->getMessages()->getMessage()[0]->getText() ?? 'Unknown error'; |
|
46
|
|
|
|
|
47
|
|
|
if (app()->environment() === 'local') { |
|
48
|
|
|
dd($message); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
Log::debug('Failed to create customer profile: ' . $message); |
|
52
|
|
|
throw new \Exception('Failed to create customer profile.'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $response; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Persist customer profile ID in the database. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $customerProfileId |
|
62
|
|
|
* @return UserGatewayProfile |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function persistInDatabase(string $customerProfileId): UserGatewayProfile |
|
65
|
|
|
{ |
|
66
|
|
|
return UserGatewayProfile::create([ |
|
|
|
|
|
|
67
|
|
|
'profile_id' => $customerProfileId, |
|
68
|
|
|
'user_id' => $this->user->id, |
|
69
|
|
|
]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Create a draft customer profile object. |
|
74
|
|
|
* |
|
75
|
|
|
* @return AnetAPI\CustomerProfileType |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function draftCustomerProfile(): AnetAPI\CustomerProfileType |
|
78
|
|
|
{ |
|
79
|
|
|
$customerProfile = new AnetAPI\CustomerProfileType(); |
|
80
|
|
|
$customerProfile->setDescription('Customer Profile'); |
|
81
|
|
|
$customerProfile->setMerchantCustomerId((string) $this->user->id); |
|
82
|
|
|
$customerProfile->setEmail($this->user->email ?? ''); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
return $customerProfile; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Create a draft request object. |
|
89
|
|
|
* |
|
90
|
|
|
* @param AnetAPI\CustomerProfileType $customerProfile |
|
91
|
|
|
* @return AnetAPI\CreateCustomerProfileRequest |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function draftRequest(AnetAPI\CustomerProfileType $customerProfile): AnetAPI\CreateCustomerProfileRequest |
|
94
|
|
|
{ |
|
95
|
|
|
$request = new AnetAPI\CreateCustomerProfileRequest(); |
|
96
|
|
|
$request->setMerchantAuthentication($this->getMerchantAuthentication()); |
|
97
|
|
|
$request->setRefId($this->getRefId()); |
|
98
|
|
|
$request->setProfile($customerProfile); |
|
99
|
|
|
|
|
100
|
|
|
return $request; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|