Issues (16)

src/Services/CustomerProfileService.php (3 issues)

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) {
0 ignored issues
show
The condition $response->getCustomerProfileId() === null is always false.
Loading history...
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([
0 ignored issues
show
Bug Best Practice introduced by
The expression return Squareetlabs\Auth...d' => $this->user->id)) could return the type Illuminate\Database\Eloq...gHasThroughRelationship which is incompatible with the type-hinted return Squareetlabs\AuthorizeNe...dels\UserGatewayProfile. Consider adding an additional type-check to rule them out.
Loading history...
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 ?? '');
0 ignored issues
show
It seems like $this->user->email ?? '' can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation; however, parameter $email of net\authorize\api\contra...ileBaseType::setEmail() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
        $customerProfile->setEmail(/** @scrutinizer ignore-type */ $this->user->email ?? '');
Loading history...
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