diff --git a/src/ARBCancelSubscriptionRequest.php b/src/ARBCancelSubscriptionRequest.php new file mode 100644 index 0000000..6c014b7 --- /dev/null +++ b/src/ARBCancelSubscriptionRequest.php @@ -0,0 +1,30 @@ +subscriptionId = $subscriptionId; + } + + protected function attachData(RequestInterface $request) + { + $request->addData('subscriptionId', $this->subscriptionId); + } +} diff --git a/src/ARBCreateSubscriptionRequest.php b/src/ARBCreateSubscriptionRequest.php new file mode 100644 index 0000000..ca7340f --- /dev/null +++ b/src/ARBCreateSubscriptionRequest.php @@ -0,0 +1,39 @@ +subscription = $subscription; + } + + /** + * @param \CommerceGuys\AuthNet\DataTypes\Subscription $subscription + * @return $this + */ + public function setSubscription(Subscription $subscription) + { + $this->subscription = $subscription; + return $this; + } + + protected function attachData(RequestInterface $request) + { + $request->addDataType($this->subscription); + } +} diff --git a/src/ARBGetSubscriptionListRequest.php b/src/ARBGetSubscriptionListRequest.php new file mode 100644 index 0000000..482a34c --- /dev/null +++ b/src/ARBGetSubscriptionListRequest.php @@ -0,0 +1,60 @@ +searchType = $searchType; + } + + /** + * @param \CommerceGuys\AuthNet\DataTypes\Sorting $sorting + * @return $this + */ + public function setSorting(Sorting $sorting) + { + $this->sorting = $sorting; + return $this; + } + + /** + * @param \CommerceGuys\AuthNet\DataTypes\Paging $paging + * @return $this + */ + public function setPaging(Paging $paging) + { + $this->paging = $paging; + return $this; + } + + protected function attachData(RequestInterface $request) + { + $request->addData('searchType', $this->searchType); + if ($this->sorting) { + $request->addDataType($this->sorting); + } + if ($this->paging) { + $request->addDataType($this->paging); + } + } +} diff --git a/src/ARBGetSubscriptionRequest.php b/src/ARBGetSubscriptionRequest.php new file mode 100644 index 0000000..ebe4093 --- /dev/null +++ b/src/ARBGetSubscriptionRequest.php @@ -0,0 +1,34 @@ +subscriptionId = $subscriptionId; + $this->includeTransactions = filter_var($includeTransactions, FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false'; + } + + protected function attachData(RequestInterface $request) + { + $request->addData('subscriptionId', $this->subscriptionId); + $request->addData('includeTransactions', $this->includeTransactions); + } +} diff --git a/src/ARBGetSubscriptionStatusRequest.php b/src/ARBGetSubscriptionStatusRequest.php new file mode 100644 index 0000000..4f37fe2 --- /dev/null +++ b/src/ARBGetSubscriptionStatusRequest.php @@ -0,0 +1,30 @@ +subscriptionId = $subscriptionId; + } + + protected function attachData(RequestInterface $request) + { + $request->addData('subscriptionId', $this->subscriptionId); + } +} diff --git a/src/ARBSubscriptionRequest.php b/src/ARBSubscriptionRequest.php new file mode 100644 index 0000000..5f10c33 --- /dev/null +++ b/src/ARBSubscriptionRequest.php @@ -0,0 +1,22 @@ +getShortName(); + } +} diff --git a/src/ARBUpdateSubscriptionRequest.php b/src/ARBUpdateSubscriptionRequest.php new file mode 100644 index 0000000..32c0054 --- /dev/null +++ b/src/ARBUpdateSubscriptionRequest.php @@ -0,0 +1,29 @@ +subscriptionId = $subscription; + } + + protected function attachData(RequestInterface $request) + { + $request->addData('subscriptionId', $this->subscriptionId); + } +} diff --git a/src/DataTypes/CustomerPaymentProfileBase.php b/src/DataTypes/CustomerPaymentProfileBase.php new file mode 100644 index 0000000..f7a8fe0 --- /dev/null +++ b/src/DataTypes/CustomerPaymentProfileBase.php @@ -0,0 +1,17 @@ +addDataType($billTo); + } +} diff --git a/src/DataTypes/CustomerProfileId.php b/src/DataTypes/CustomerProfileId.php new file mode 100644 index 0000000..9caa143 --- /dev/null +++ b/src/DataTypes/CustomerProfileId.php @@ -0,0 +1,13 @@ + 365) { + $message = 'Interval length for days must be between 7 and 365, inclusive.'; + throw new \InvalidArgumentException($message); + } + break; + + case 'months': + if ($values['length'] < 1 || $values['length'] > 12) { + $message = 'Interval length for months must be between 1 and 12, inclusive.'; + throw new \InvalidArgumentException($message); + } + break; + } + } +} diff --git a/src/DataTypes/Paging.php b/src/DataTypes/Paging.php new file mode 100644 index 0000000..e7d9936 --- /dev/null +++ b/src/DataTypes/Paging.php @@ -0,0 +1,12 @@ +properties['payment'][$paymentMethod->getType()] = $paymentMethod->toArray(); } - public function addBillTo(BillTo $billTo) - { - $this->addDataType($billTo); - } - public function addCustomerPaymentProfileId($id) { $this->properties['customerPaymentProfileId'] = $id; diff --git a/src/DataTypes/PaymentSchedule.php b/src/DataTypes/PaymentSchedule.php new file mode 100644 index 0000000..9b33f66 --- /dev/null +++ b/src/DataTypes/PaymentSchedule.php @@ -0,0 +1,18 @@ +properties['interval'] = $interval->toArray(); + } +} diff --git a/src/DataTypes/Sorting.php b/src/DataTypes/Sorting.php new file mode 100644 index 0000000..0b57326 --- /dev/null +++ b/src/DataTypes/Sorting.php @@ -0,0 +1,23 @@ +validate($values); + foreach ($values as $name => $value) { + $this->$name = $value; + if ($name == 'orderDescending') { + $this->$name = filter_var($value, FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false'; + } + } + } +} diff --git a/src/DataTypes/Subscription.php b/src/DataTypes/Subscription.php new file mode 100644 index 0000000..133263b --- /dev/null +++ b/src/DataTypes/Subscription.php @@ -0,0 +1,48 @@ +properties['paymentSchedule'] = $paymentSchedule->toArray(); + } + + public function addPayment(PaymentMethodInterface $payment) + { + $this->properties['payment'] = $payment->toArray(); + } + + public function addOrder(Order $order) + { + $this->addDataType($order); + } + + public function addBillTo(BillTo $billTo) + { + $this->addDataType($billTo); + } + + public function addShipTo(ShipTo $shipTo) + { + $this->addDataType($shipTo); + } + + public function addProfile(CustomerProfileId $profile) + { + $this->properties['profile'] = $profile->toArray(); + } +} diff --git a/tests/ARBCreateSubscriptionRequestTest.php b/tests/ARBCreateSubscriptionRequestTest.php new file mode 100644 index 0000000..edacc4b --- /dev/null +++ b/tests/ARBCreateSubscriptionRequestTest.php @@ -0,0 +1,104 @@ + 1, 'unit' => 'months']); + $paymentSchedule = new PaymentSchedule([ + 'startDate' => '2018-08-30', + 'totalOccurrences' => 9999, + ]); + $paymentSchedule->addInterval($interval); + $profile = new CustomerProfileId([ + 'customerProfileId' => '39931060', + 'customerPaymentProfileId' => '36223863', + ]); + $subscription = new Subscription(['amount' => '10.29']); + $subscription->addPaymentSchedule($paymentSchedule); + $subscription->addProfile($profile); + $request = new ARBCreateSubscriptionRequest($this->configurationXml, $this->client, $subscription); + $request->setSubscription($subscription); + $response = $request->execute(); + $this->assertObjectHasAttribute('profile', $response->contents()); + $this->assertResponse($response, 'I00001', 'Successful.', 'Ok'); + $subscriptionId = $response->contents()->subscriptionId; + // Test Get. + $get = new ARBGetSubscriptionRequest($this->configurationXml, $this->client, $subscriptionId); + $response = $get->execute(); + $this->assertResponse($response, 'I00001', 'Successful.', 'Ok'); + // Test get status. + $status = new ARBGetSubscriptionStatusRequest($this->configurationXml, $this->client, $subscriptionId); + $response = $status->execute(); + $this->assertResponse($response, 'I00001', 'Successful.', 'Ok'); + // Remove subscription before finishing. + $cancel = new ARBCancelSubscriptionRequest($this->configurationXml, $this->client, $subscriptionId); + $response = $cancel->execute(); + $this->assertResponse($response, 'I00001', 'Successful.', 'Ok'); + // Retrieve a list of subscriptions. + $sorting = new Sorting([ + 'orderBy' => 'id', + 'orderDescending' => false, + ]); + $paging = new Paging([ + 'limit' => 100, + 'offset' => 1, + ]); + $request = new ARBGetSubscriptionListRequest($this->configurationXml, $this->client, 'subscriptionActive'); + $request->setSorting($sorting); + $request->setPaging($paging); + $response = $request->execute(); + $this->assertObjectHasAttribute('totalNumInResultSet', $response->contents()); + $this->assertResponse($response, 'I00001', 'Successful.', 'Ok'); + // If something has caused a lot of built-up subscriptions, remove them. + if ($response->contents()->totalNumInResultSet > 100) { + $this->cleanupSubscriptions(); + } + } + + protected function cleanupSubscriptions() + { + $request = new ARBGetSubscriptionListRequest($this->configurationXml, $this->client, 'subscriptionActive'); + $response = $request->execute(); + $contents = $response->contents(); + while ($contents->totalNumInResultSet) { + $subscriptions = $contents->subscriptionDetails->subscriptionDetail; + if (!is_array($subscriptions)) { + $subscriptions = [$contents->subscriptionDetails->subscriptionDetail]; + } + foreach ($subscriptions as $subscription) { + $cancel = new ARBCancelSubscriptionRequest($this->configurationXml, $this->client, $subscription->id); + $response = $cancel->execute(); + $this->assertEquals('Ok', $response->getResultCode()); + } + $response = $request->execute(); + $contents = $response->contents(); + } + } +} diff --git a/tests/DataType/IntervalTest.php b/tests/DataType/IntervalTest.php new file mode 100644 index 0000000..733ba6c --- /dev/null +++ b/tests/DataType/IntervalTest.php @@ -0,0 +1,60 @@ +setExpectedException(\InvalidArgumentException::class, 'Interval must have a length.'); + $interval = new Interval(); + } + + /** + * Test validate logic. + */ + public function testMissingUnit() + { + $this->setExpectedException(\InvalidArgumentException::class, 'Interval must have a unit.'); + $interval = new Interval(['length' => '1']); + } + + /** + * Test validate logic. + */ + public function testInvalidUnit() + { + $this->setExpectedException(\InvalidArgumentException::class, 'Interval unit must be days or months.'); + $interval = new Interval(['length' => '1', 'unit' => 'foo']); + } + + /** + * Test validate logic. + */ + public function testUnitDays() + { + $this->setExpectedException(\InvalidArgumentException::class, 'Interval length for days must be between 7 and 365, inclusive.'); + $interval = new Interval(['length' => '1', 'unit' => 'days']); + } + + /** + * Test validate logic. + */ + public function testUnitMonths() + { + $this->setExpectedException(\InvalidArgumentException::class, 'Interval length for months must be between 1 and 12, inclusive.'); + $interval = new Interval(['length' => '13', 'unit' => 'months']); + } +}