SubscriptionService   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 81
c 1
b 0
f 1
dl 0
loc 135
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatus() 0 9 1
A get() 0 12 1
A create() 0 47 1
A getList() 0 22 1
A cancel() 0 9 1
A update() 0 23 1
1
<?php
2
3
namespace Squareetlabs\AuthorizeNet\Services;
4
5
use Squareetlabs\AuthorizeNet\AuthorizeNet;
6
use DateTime;
7
use net\authorize\api\contract\v1 as AnetAPI;
8
use net\authorize\api\controller as AnetController;
9
10
class SubscriptionService extends AuthorizeNet
11
{
12
13
    public function create(array $data)
14
    {
15
        // Subscription Type Info
16
        $subscription = new AnetAPI\ARBSubscriptionType();
17
        $subscription->setName($data['name']);
18
19
        $interval = new AnetAPI\PaymentScheduleType\IntervalAType();
20
        $interval->setLength($data['intervalLength']);
21
        $interval->setUnit($data['intervalLengthUnit'] ?? 'days');
22
23
        $paymentSchedule = new AnetAPI\PaymentScheduleType();
24
        $paymentSchedule->setInterval($interval);
25
        $paymentSchedule->setStartDate(new DateTime($data['startDate']));
26
        $paymentSchedule->setTotalOccurrences($data['totalOccurrences']);
27
        $paymentSchedule->setTrialOccurrences($data['trialOccurrences']);
28
29
30
        $creditCard = new AnetAPI\CreditCardType();
31
        $creditCard->setCardNumber($data['cardNumber']);
32
        $creditCard->setExpirationDate($data['cardExpiry']);
33
34
        $payment = new AnetAPI\PaymentType();
35
        $payment->setCreditCard($creditCard);
36
37
        $subscription->setPaymentSchedule($paymentSchedule);
38
        $subscription->setAmount($data['amountInDollars']);
39
        $subscription->setTrialAmount($data['trialAmountInDollars']);
40
41
        $subscription->setPayment($payment);
42
43
        $order = new AnetAPI\OrderType();
44
        $order->setInvoiceNumber($data['invoiceNumber']);
45
        $order->setDescription($data['subscriptionDescription']);
46
        $subscription->setOrder($order);
47
48
        $billTo = new AnetAPI\NameAndAddressType();
49
        $billTo->setFirstName($data['customerFirstName']);
50
        $billTo->setLastName($data['customerLastName']);
51
52
        $subscription->setBillTo($billTo);
53
54
        $request = new AnetAPI\ARBCreateSubscriptionRequest();
55
        $request->setmerchantAuthentication($this->getMerchantAuthentication());
56
        $request->setRefId($this->getRefId());
57
        $request->setSubscription($subscription);
58
        $controller = new AnetController\ARBCreateSubscriptionController($request);
59
        return $this->execute($controller);
60
    }
61
62
    public function getList(array $options = [])
63
    {
64
        $sorting = new AnetAPI\ARBGetSubscriptionListSortingType();
65
        $sorting->setOrderBy($options['orderBy'] ?? 'id');
66
        $sorting->setOrderDescending($options['orderDescending'] ?? false);
67
68
        $paging = new AnetAPI\PagingType();
69
        $paging->setLimit($options['limit'] ?? 1000);
70
        $paging->setOffset($options['offset'] ?? 1);
71
72
        $request = new AnetAPI\ARBGetSubscriptionListRequest();
73
        $request->setMerchantAuthentication($this->getMerchantAuthentication());
74
        $request->setRefId($this->getRefId());
75
        $request->setSearchType($options['searchType'] ?? "subscriptionActive");
76
77
        $request->setSorting($sorting);
78
        $request->setPaging($paging);
79
80
81
        $controller = new AnetController\ARBGetSubscriptionListController($request);
82
83
        return $this->execute($controller);
84
    }
85
86
    public function getStatus(string $subscriptionId)
87
    {
88
        $request = new AnetAPI\ARBGetSubscriptionStatusRequest();
89
        $request->setMerchantAuthentication($this->getMerchantAuthentication());
90
        $request->setRefId($this->getRefId());
91
        $request->setSubscriptionId($subscriptionId);
92
93
        $controller = new AnetController\ARBGetSubscriptionStatusController($request);
94
        return $this->execute($controller);
95
    }
96
97
    public function get(string $subscriptionId)
98
    {
99
        // Creating the API Request with required parameters
100
        $request = new AnetAPI\ARBGetSubscriptionRequest();
101
        $request->setMerchantAuthentication($this->getMerchantAuthentication());
102
        $request->setRefId($this->getRefId());
103
        $request->setSubscriptionId($subscriptionId);
104
        $request->setIncludeTransactions(true);
105
106
        // Controller
107
        $controller = new AnetController\ARBGetSubscriptionController($request);
108
        return $this->execute($controller);
109
    }
110
111
    public function update(string $subscriptionId, array $data)
112
    {
113
114
        $subscription = new AnetAPI\ARBSubscriptionType();
115
116
        $creditCard = new AnetAPI\CreditCardType();
117
        $creditCard->setCardNumber($data['cardNumber']);
118
        $creditCard->setExpirationDate($data['cardExpiry']);
119
120
        $payment = new AnetAPI\PaymentType();
121
        $payment->setCreditCard($creditCard);
122
123
        $subscription->setPayment($payment);
124
125
126
        $request = new AnetAPI\ARBUpdateSubscriptionRequest();
127
        $request->setMerchantAuthentication($this->getMerchantAuthentication());
128
        $request->setRefId($this->getRefId());
129
        $request->setSubscriptionId($subscriptionId);
130
        $request->setSubscription($subscription);
131
132
        $controller = new AnetController\ARBUpdateSubscriptionController($request);
133
        return $this->execute($controller);
134
    }
135
136
    public function cancel(string $subscriptionId)
137
    {
138
        $request = new AnetAPI\ARBCancelSubscriptionRequest();
139
        $request->setMerchantAuthentication($this->getMerchantAuthentication());
140
        $request->setRefId($this->getRefId());
141
        $request->setSubscriptionId($subscriptionId);
142
143
        $controller = new AnetController\ARBCancelSubscriptionController($request);
144
        return $this->execute($controller);
145
    }
146
}
147