From 084186c5c8603fd6b2c26364a047bd91e61139cb Mon Sep 17 00:00:00 2001 From: Petr Kopac Date: Fri, 10 Mar 2017 08:51:48 +0100 Subject: [PATCH] namespace Import -> ChartMogul, backwards compatible (#17) * namespace Import -> ChartMogul, backwards compatible --- README.md | 2 +- src/Customer.php | 37 +++++++- src/CustomerInvoices.php | 47 ++++++++++ src/Import/Customer.php | 77 +--------------- src/Import/CustomerInvoices.php | 45 +-------- src/Import/DataSource.php | 10 ++ src/Import/Invoice.php | 60 +----------- src/Import/LineItems/AbstractLineItem.php | 16 +--- src/Import/LineItems/OneTime.php | 6 +- src/Import/LineItems/Subscription.php | 15 +-- src/Import/Plan.php | 10 ++ src/Import/Subscription.php | 87 +----------------- .../Transactions/AbstractTransaction.php | 20 +--- src/Import/Transactions/Payment.php | 14 +-- src/Import/Transactions/Refund.php | 13 +-- src/Invoice.php | 66 ++++++++++++++ src/LineItems/AbstractLineItem.php | 23 +++++ src/LineItems/OneTime.php | 13 +++ src/LineItems/Subscription.php | 20 ++++ src/Subscription.php | 91 +++++++++++++++++++ src/Transactions/AbstractTransaction.php | 25 +++++ src/Transactions/Payment.php | 15 +++ src/Transactions/Refund.php | 14 +++ 23 files changed, 400 insertions(+), 326 deletions(-) create mode 100644 src/CustomerInvoices.php create mode 100644 src/Import/DataSource.php create mode 100644 src/Import/Plan.php create mode 100644 src/Invoice.php create mode 100644 src/LineItems/AbstractLineItem.php create mode 100644 src/LineItems/OneTime.php create mode 100644 src/LineItems/Subscription.php create mode 100644 src/Subscription.php create mode 100644 src/Transactions/AbstractTransaction.php create mode 100644 src/Transactions/Payment.php create mode 100644 src/Transactions/Refund.php diff --git a/README.md b/README.md index 4cd4875..cf04e4b 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ or in `composer.json`: ```json { "require": { - "chartmogul/chartmogul-php": "1.0.0" + "chartmogul/chartmogul-php": "1.1.2" } } ``` diff --git a/src/Customer.php b/src/Customer.php index 13ddaa2..57870ab 100644 --- a/src/Customer.php +++ b/src/Customer.php @@ -103,7 +103,10 @@ public function customAttributes() */ public static function findByExternalId($externalId) { - return static::all(['external_id' => $externalId])->entries->first(); + if (gettype($externalId) == 'string') { + $externalId = ['external_id' => $externalId]; + } + return static::all($externalId)->entries->first(); } /** @@ -132,7 +135,7 @@ public static function search($email, ClientInterface $client = null) */ public static function merge($from, $into, ClientInterface $client = null) { - $response = (new static([], $client)) + (new static([], $client)) ->getClient() ->setResourcekey(static::class) ->send('/v1/customers/merges', 'POST', [ @@ -229,4 +232,34 @@ public function updateCustomAttributes($custom) $this->attributes['custom'] = $result['custom']; return $result['custom']; } + + /** + * Find a Customer Subscriptions + * @param array $options + * @return \Doctrine\Common\Collections\ArrayCollection | Customer + * @deprecated Use Import\Subscription. + */ + public function subscriptions(array $options = []) + { + if (!isset($this->subscriptions)) { + $options['customer_uuid'] = $this->uuid; + $this->subscriptions = Subscription::all($options); + } + return $this->subscriptions; + } + + /** + * Find customer's invoices + * @param array $options + * @return \Doctrine\Common\Collections\ArrayCollection | Customer + * @deprecated Use Import\CustomerInvoices. + */ + public function invoices(array $options = []) + { + if (!isset($this->invoices)) { + $options['customer_uuid'] = $this->uuid; + $this->invoices = CustomerInvoices::all($options)->invoices; + } + return $this->invoices; + } } diff --git a/src/CustomerInvoices.php b/src/CustomerInvoices.php new file mode 100644 index 0000000..0a525c9 --- /dev/null +++ b/src/CustomerInvoices.php @@ -0,0 +1,47 @@ +invoices = new ArrayCollection($this->invoices); + foreach ($this->invoices as $key => $item) { + $this->setInvoice($key, $item); + } + } + + protected function setInvoice($index, $invoice) + { + + if ($invoice instanceof \ChartMogul\Import\Invoice) { + $this->invoices[$index] =$invoice; + } elseif (is_array($invoice)) { + $this->invoices[$index] = new \ChartMogul\Import\Invoice($invoice); + } + } +} diff --git a/src/Import/Customer.php b/src/Import/Customer.php index 906be6b..b105037 100644 --- a/src/Import/Customer.php +++ b/src/Import/Customer.php @@ -2,84 +2,9 @@ namespace ChartMogul\Import; -use ChartMogul\Resource\AbstractResource; - /** * @deprecated Use ChartMogul\Customer, Import\CustomerInvoices or Import\Subscription. - * @property-read string $uuid */ -class Customer extends AbstractResource -{ - - use \ChartMogul\Service\CreateTrait; - use \ChartMogul\Service\AllTrait; - use \ChartMogul\Service\DestroyTrait; - - /** - * @ignore - */ - const RESOURCE_PATH = '/v1/import/customers'; - /** - * @ignore - */ - const RESOURCE_NAME = 'Customer'; - /** - * @ignore - */ - const ROOT_KEY = 'customers'; - - protected $uuid; - - public $external_id; - public $name; - public $email; - public $company; - public $country; - public $state; - public $city; - public $zip; - public $data_source_uuid; - public $lead_created_at; - public $free_trial_started_at; - - /** - * Find a Customer by External ID - * @param string $externalId - * @return Customer - * @deprecated Use ChartMogul\Customer - */ - public static function findByExternalId(array $options = []) - { - return static::all($options)->first(); - } - - /** - * Find a Customer Subscriptions - * @param array $options - * @return \Doctrine\Common\Collections\ArrayCollection | Customer - * @deprecated Use Import\Subscription. - */ - public function subscriptions(array $options = []) - { - if (!isset($this->subscriptions)) { - $options['customer_uuid'] = $this->uuid; - $this->subscriptions = Subscription::all($options); - } - return $this->subscriptions; - } +class Customer extends ChartMogul\Customer { - /** - * Find a Customer Invoices - * @param array $options - * @return \Doctrine\Common\Collections\ArrayCollection | Customer - * @deprecated Use Import\CustomerInvoices. - */ - public function invoices(array $options = []) - { - if (!isset($this->invoices)) { - $options['customer_uuid'] = $this->uuid; - $this->invoices = CustomerInvoices::all($options)->invoices; - } - return $this->invoices; - } } diff --git a/src/Import/CustomerInvoices.php b/src/Import/CustomerInvoices.php index 110afd6..f9e491d 100644 --- a/src/Import/CustomerInvoices.php +++ b/src/Import/CustomerInvoices.php @@ -2,46 +2,9 @@ namespace ChartMogul\Import; -use ChartMogul\Resource\AbstractResource; -use ChartMogul\Http\ClientInterface; -use Doctrine\Common\Collections\ArrayCollection; +/** + * @deprecated Use \ChartMogul\CustomerInvoices + */ +class CustomerInvoices extends \ChartMogul\CustomerInvoices { -class CustomerInvoices extends AbstractResource -{ - - use \ChartMogul\Service\CreateTrait; - use \ChartMogul\Service\AllTrait; - - /** - * @ignore - */ - const RESOURCE_PATH = '/v1/import/customers/:customer_uuid/invoices'; - /** - * @ignore - */ - const RESOURCE_NAME = 'Invoices'; - - public $invoices = []; - - public $customer_uuid; - - public function __construct(array $attr = [], ClientInterface $client = null) - { - parent::__construct($attr, $client); - - $this->invoices = new ArrayCollection($this->invoices); - foreach ($this->invoices as $key => $item) { - $this->setInvoice($key, $item); - } - } - - protected function setInvoice($index, $invoice) - { - - if ($invoice instanceof \ChartMogul\Import\Invoice) { - $this->invoices[$index] =$invoice; - } elseif (is_array($invoice)) { - $this->invoices[$index] = new \ChartMogul\Import\Invoice($invoice); - } - } } diff --git a/src/Import/DataSource.php b/src/Import/DataSource.php new file mode 100644 index 0000000..cb2928b --- /dev/null +++ b/src/Import/DataSource.php @@ -0,0 +1,10 @@ +line_items = new ArrayCollection($this->line_items); - foreach ($this->line_items as $key => $item) { - $this->setLineItem($key, $item); - } - - $this->transactions = new ArrayCollection($this->transactions); - foreach ($this->transactions as $key => $item) { - $this->setTransaction($key, $item); - } - } - - protected function setLineItem($index, $line) - { - - if ($line instanceof AbstractLineItem) { - $this->line_items[$index] = $line; - } elseif (is_array($line) && isset($line['type']) && $line['type'] === 'one_time') { - $this->line_items[$index] = new OneTime($line); - } elseif (is_array($line) && isset($line['type']) && $line['type'] === 'subscription') { - $this->line_items[$index] = new SubsItem($line); - } - } - - protected function setTransaction($index, $tr) - { +class Invoice extends \ChartMogul\Invoice { - if ($tr instanceof AbstractTransaction) { - $this->transactions[$index] = $tr; - } elseif (is_array($tr) && isset($tr['type']) && $tr['type'] === 'payment') { - $this->transactions[$index] = new Payment($tr); - } elseif (is_array($tr) && isset($tr['type']) && $tr['type'] === 'refund') { - $this->transactions[$index] = new Refund($tr); - } - } } diff --git a/src/Import/LineItems/AbstractLineItem.php b/src/Import/LineItems/AbstractLineItem.php index c4fcb8e..37d6cb3 100644 --- a/src/Import/LineItems/AbstractLineItem.php +++ b/src/Import/LineItems/AbstractLineItem.php @@ -4,20 +4,8 @@ /** * @codeCoverageIgnore -* @property-read string $uuid +* @deprecated */ -abstract class AbstractLineItem extends \ChartMogul\Resource\AbstractModel +abstract class AbstractLineItem extends \ChartMogul\AbstractLineItem { - - protected $uuid; - - public $type; - public $amount_in_cents; - public $quantity; - public $discount_amount_in_cents; - public $discount_code; - public $tax_amount_in_cents; - public $external_id; - - public $invoice_uuid; } diff --git a/src/Import/LineItems/OneTime.php b/src/Import/LineItems/OneTime.php index e3dcab8..5d5fa4b 100644 --- a/src/Import/LineItems/OneTime.php +++ b/src/Import/LineItems/OneTime.php @@ -4,10 +4,8 @@ /** * @codeCoverageIgnore +* @deprecated Use ChartMogul\OneTime */ -class OneTime extends AbstractLineItem -{ +class OneTime extends ChartMogul\OneTime { - public $type = 'one_time'; - public $description; } diff --git a/src/Import/LineItems/Subscription.php b/src/Import/LineItems/Subscription.php index d3eed3d..c529f45 100644 --- a/src/Import/LineItems/Subscription.php +++ b/src/Import/LineItems/Subscription.php @@ -4,17 +4,8 @@ /** * @codeCoverageIgnore +* @deprecated Use ChartMogul\Subscription */ -class Subscription extends AbstractLineItem -{ - - public $type = 'subscription'; - public $subscription_external_id; - public $service_period_start; - public $service_period_end; - public $cancelled_at; - public $prorated; - - protected $subscription_uuid; - public $plan_uuid; +class Subscription extends Subscription{ + } diff --git a/src/Import/Plan.php b/src/Import/Plan.php new file mode 100644 index 0000000..5839afa --- /dev/null +++ b/src/Import/Plan.php @@ -0,0 +1,10 @@ +getClient()->send( - '/v1/import/subscriptions/'.$this->uuid, - 'PATCH', - $payload - ); - $this->cancellation_dates = $response['cancellation_dates']; - return $this; - } - - /** - * Cancels a subscription that was generated from an imported invoice. - * @param string $cancelledAt The time at which the subscription was cancelled. - * @return Subscription - */ - public function cancel($cancelledAt) - { - return $this->cancellation([ - 'cancelled_at' => $cancelledAt - ]); - } - - /** - * Changes dates of cancellation for a subscription. - * @param array $cancellationDates The array of times (strings) at which the subscription was cancelled. - * @return Subscription - */ - public function setCancellationDates($cancellationDates) - { - return $this->cancellation([ - 'cancellation_dates' => $cancellationDates - ]); - } +class Subscription extends \ChartMogul\Subscription { - /** - * @param array $data - * @param ClientInterface|null $client - * @return ArrayCollection|self - */ - public static function fromArray(array $data, \ChartMogul\Http\ClientInterface $client = null) - { - $result = parent::fromArray($data, $client); - if (isset($data["customer_uuid"])) { - $result->customer_uuid = $data["customer_uuid"]; - } - return $result; - } -} +} \ No newline at end of file diff --git a/src/Import/Transactions/AbstractTransaction.php b/src/Import/Transactions/AbstractTransaction.php index f8dca8c..df4cd56 100644 --- a/src/Import/Transactions/AbstractTransaction.php +++ b/src/Import/Transactions/AbstractTransaction.php @@ -2,24 +2,10 @@ namespace ChartMogul\Import\Transactions; -use ChartMogul\Resource\AbstractResource; - /** * @property-read string $uuid + * @deprecated Use ChartMogul\AbstractTransaction */ -abstract class AbstractTransaction extends AbstractResource -{ - - use \ChartMogul\Service\CreateTrait; - - const RESOURCE_PATH = '/v1/import/invoices/:invoice_uuid/transactions'; - - protected $uuid; - - public $type; - public $date; - public $result; - public $external_id; - - public $invoice_uuid; +abstract class AbstractTransaction extends ChartMogul\AbstractTransaction { + } diff --git a/src/Import/Transactions/Payment.php b/src/Import/Transactions/Payment.php index bbc8747..985b412 100644 --- a/src/Import/Transactions/Payment.php +++ b/src/Import/Transactions/Payment.php @@ -3,13 +3,9 @@ namespace ChartMogul\Import\Transactions; /** -* @codeCoverageIgnore -*/ -class Payment extends AbstractTransaction -{ - - const RESOURCE_NAME= 'Payment Transaction'; - - - public $type = 'payment'; + * @codeCoverageIgnore + * @deprecated Use ChartMogul\Payment + */ +class Payment extends ChartMogul\Payment { + } diff --git a/src/Import/Transactions/Refund.php b/src/Import/Transactions/Refund.php index 260e091..a6d6e93 100644 --- a/src/Import/Transactions/Refund.php +++ b/src/Import/Transactions/Refund.php @@ -3,12 +3,9 @@ namespace ChartMogul\Import\Transactions; /** -* @codeCoverageIgnore -*/ -class Refund extends AbstractTransaction -{ - - const RESOURCE_NAME= 'Refund Transaction'; - - public $type = 'refund'; + * @codeCoverageIgnore + * @deprecated Use ChartMogul\Refund + */ +class Refund extends ChartMogul\Refund { + } diff --git a/src/Invoice.php b/src/Invoice.php new file mode 100644 index 0000000..298d5ac --- /dev/null +++ b/src/Invoice.php @@ -0,0 +1,66 @@ +line_items = new ArrayCollection($this->line_items); + foreach ($this->line_items as $key => $item) { + $this->setLineItem($key, $item); + } + + $this->transactions = new ArrayCollection($this->transactions); + foreach ($this->transactions as $key => $item) { + $this->setTransaction($key, $item); + } + } + + protected function setLineItem($index, $line) + { + + if ($line instanceof AbstractLineItem) { + $this->line_items[$index] = $line; + } elseif (is_array($line) && isset($line['type']) && $line['type'] === 'one_time') { + $this->line_items[$index] = new OneTime($line); + } elseif (is_array($line) && isset($line['type']) && $line['type'] === 'subscription') { + $this->line_items[$index] = new SubsItem($line); + } + } + + protected function setTransaction($index, $tr) + { + + if ($tr instanceof AbstractTransaction) { + $this->transactions[$index] = $tr; + } elseif (is_array($tr) && isset($tr['type']) && $tr['type'] === 'payment') { + $this->transactions[$index] = new Payment($tr); + } elseif (is_array($tr) && isset($tr['type']) && $tr['type'] === 'refund') { + $this->transactions[$index] = new Refund($tr); + } + } +} diff --git a/src/LineItems/AbstractLineItem.php b/src/LineItems/AbstractLineItem.php new file mode 100644 index 0000000..685d6ff --- /dev/null +++ b/src/LineItems/AbstractLineItem.php @@ -0,0 +1,23 @@ +getClient()->send( + '/v1/import/subscriptions/'.$this->uuid, + 'PATCH', + $payload + ); + $this->cancellation_dates = $response['cancellation_dates']; + return $this; + } + + /** + * Cancels a subscription that was generated from an imported invoice. + * @param string $cancelledAt The time at which the subscription was cancelled. + * @return Subscription + */ + public function cancel($cancelledAt) + { + return $this->cancellation([ + 'cancelled_at' => $cancelledAt + ]); + } + + /** + * Changes dates of cancellation for a subscription. + * @param array $cancellationDates The array of times (strings) at which the subscription was cancelled. + * @return Subscription + */ + public function setCancellationDates($cancellationDates) + { + return $this->cancellation([ + 'cancellation_dates' => $cancellationDates + ]); + } + + /** + * @param array $data + * @param ClientInterface|null $client + * @return ArrayCollection|self + */ + public static function fromArray(array $data, \ChartMogul\Http\ClientInterface $client = null) + { + $result = parent::fromArray($data, $client); + if (isset($data["customer_uuid"])) { + $result->customer_uuid = $data["customer_uuid"]; + } + return $result; + } +} diff --git a/src/Transactions/AbstractTransaction.php b/src/Transactions/AbstractTransaction.php new file mode 100644 index 0000000..4f1674b --- /dev/null +++ b/src/Transactions/AbstractTransaction.php @@ -0,0 +1,25 @@ +