|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Squareetlabs\AuthorizeNet\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Squareetlabs\AuthorizeNet\AuthorizeNet; |
|
6
|
|
|
use Squareetlabs\AuthorizeNet\Models\UserPaymentProfile; |
|
7
|
|
|
use net\authorize\api\contract\v1 as AnetAPI; |
|
8
|
|
|
use net\authorize\api\controller as AnetControllers; |
|
9
|
|
|
|
|
10
|
|
|
class PaymentProfileService extends AuthorizeNet |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Create a payment profile for the user. |
|
14
|
|
|
* |
|
15
|
|
|
* @param array<string, string> $opaqueData |
|
16
|
|
|
* @param array<string, mixed> $source |
|
17
|
|
|
* @return AnetAPI\CreateCustomerPaymentProfileResponse |
|
18
|
|
|
*/ |
|
19
|
|
|
public function create(array $opaqueData, array $source): AnetAPI\CreateCustomerPaymentProfileResponse |
|
20
|
|
|
{ |
|
21
|
|
|
$merchantKeys = $this->getMerchantAuthentication(); |
|
22
|
|
|
|
|
23
|
|
|
$opaqueDataType = new AnetAPI\OpaqueDataType(); |
|
24
|
|
|
$opaqueDataType->setDataDescriptor($opaqueData['dataDescriptor']); |
|
25
|
|
|
$opaqueDataType->setDataValue($opaqueData['dataValue']); |
|
26
|
|
|
|
|
27
|
|
|
$paymentType = new AnetAPI\PaymentType(); |
|
28
|
|
|
$paymentType->setOpaqueData($opaqueDataType); |
|
29
|
|
|
|
|
30
|
|
|
$customerPaymentProfileType = new AnetAPI\CustomerPaymentProfileType(); |
|
31
|
|
|
$customerPaymentProfileType->setPayment($paymentType); |
|
32
|
|
|
|
|
33
|
|
|
$paymentProfileRequest = new AnetAPI\CreateCustomerPaymentProfileRequest(); |
|
34
|
|
|
$paymentProfileRequest->setMerchantAuthentication($merchantKeys); |
|
35
|
|
|
$paymentProfileRequest->setCustomerProfileId($this->user->anet()->getCustomerProfileId()); |
|
|
|
|
|
|
36
|
|
|
$paymentProfileRequest->setPaymentProfile($customerPaymentProfileType); |
|
37
|
|
|
|
|
38
|
|
|
$controller = new AnetControllers\CreateCustomerPaymentProfileController($paymentProfileRequest); |
|
39
|
|
|
$response = $this->execute($controller); |
|
40
|
|
|
|
|
41
|
|
|
if ($response->getCustomerPaymentProfileId() !== null) { |
|
42
|
|
|
$this->storeInDatabase($response, $source); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $response; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Store payment profile in the database. |
|
50
|
|
|
* |
|
51
|
|
|
* @param AnetAPI\CreateCustomerPaymentProfileResponse $response |
|
52
|
|
|
* @param array<string, mixed> $source |
|
53
|
|
|
* @return UserPaymentProfile |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function storeInDatabase(AnetAPI\CreateCustomerPaymentProfileResponse $response, array $source): UserPaymentProfile |
|
56
|
|
|
{ |
|
57
|
|
|
return UserPaymentProfile::create([ |
|
|
|
|
|
|
58
|
|
|
'user_id' => $this->user->id, |
|
59
|
|
|
'payment_profile_id' => $response->getCustomerPaymentProfileId(), |
|
60
|
|
|
'last_4' => $source['last_4'] ?? '', |
|
61
|
|
|
'brand' => $source['brand'] ?? '', |
|
62
|
|
|
'type' => $source['type'] ?? 'card', |
|
63
|
|
|
]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|