From 7f68d6e588eb9597fd1b406a7f01f4ae38e5fc17 Mon Sep 17 00:00:00 2001 From: Manuel HUEZ Date: Thu, 17 Nov 2016 15:10:33 -0500 Subject: [PATCH] Initial stable version --- README.md | 4 +- composer.json | 12 +- init.php | 34 ++ spec.php | 34 ++ src/Activity.php | 81 ++-- src/AuthorizationRequest.php | 125 +++--- src/Card.php | 76 ++-- src/Coupon.php | 147 ++++--- src/Customer.php | 239 ++++++------ src/CustomerAction.php | 30 +- src/Discount.php | 108 +++--- src/Event.php | 98 +++-- ...eption.php => AuthenticationException.php} | 4 +- ...{ApiException.php => GenericException.php} | 4 +- src/Exceptions/InternalException.php | 21 + src/Exceptions/NotFoundException.php | 2 +- src/Exceptions/ValidationException.php | 21 + src/Gateway.php | 54 ++- src/GatewayConfiguration.php | 46 +-- src/Invoice.php | 237 ++++++------ src/Networking/Request.php | 178 +++++++++ src/Networking/RequestProcessoutPrivate.php | 150 -------- src/Networking/Response.php | 71 ++-- src/Plan.php | 143 +++---- src/ProcessOut.php | 239 ++++++++---- src/Product.php | 162 ++++---- src/Project.php | 57 ++- src/Refund.php | 82 ++-- src/Subscription.php | 364 ++++++++++-------- src/Token.php | 126 +++--- src/Transaction.php | 231 ++++++----- src/Webhook.php | 74 ++-- 32 files changed, 1707 insertions(+), 1547 deletions(-) create mode 100644 init.php create mode 100644 spec.php rename src/Exceptions/{ApiAuthenticationException.php => AuthenticationException.php} (88%) rename src/Exceptions/{ApiException.php => GenericException.php} (89%) create mode 100644 src/Exceptions/InternalException.php create mode 100644 src/Exceptions/ValidationException.php create mode 100644 src/Networking/Request.php delete mode 100755 src/Networking/RequestProcessoutPrivate.php diff --git a/README.md b/README.md index 3620ac1..ae478fc 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ The package's installation is done using composer. Simply add these lines to you ``` json { "require": { - "ProcessOut/ProcessOut-php": "^4.5.1" + "ProcessOut/ProcessOut-php": "^5.0.0" } } ``` @@ -29,4 +29,4 @@ And run the composer update command to install/update the package. ``` sh composer update -``` +``` \ No newline at end of file diff --git a/composer.json b/composer.json index a4621e7..4605138 100644 --- a/composer.json +++ b/composer.json @@ -1,25 +1,23 @@ { - "name": "ProcessOut/ProcessOut-php", + "name": "processout/processout-php", "description": "ProcessOut API bindings.", "keywords": [ - "ProcessOut", + "processout", "api", "bindings" ], - "homepage": "https://processout.com", + "homepage": "https://www.processout.com", "license": "MIT", "authors": [ { "name": "ProcessOut", - "homepage": "https://processout.com" + "homepage": "https://www.processout.com" } ], "require": { "php": ">=5.4", "ext-curl": "*", - "ext-mbstring": "*", - - "anlutro/curl": "1.4" + "ext-mbstring": "*" }, "autoload": { "psr-4": { diff --git a/init.php b/init.php new file mode 100644 index 0000000..b4df64b --- /dev/null +++ b/init.php @@ -0,0 +1,34 @@ +newInvoice(array( + 'name' => 'Test invoice', + 'amount' => '9.99', + 'currency' => 'USD' +))->create(); + +assert(!empty($invoice->getId()), 'The invoice ID should not be empty'); + +$fetched = $client->newInvoice()->find($invoice->getId()); +assert(!empty($fetched->getId()), 'The fetched invoice ID should not be empty'); +assert($invoice->getId() == $fetched->getId(), 'The invoices ID should be equal'); + +// Fetch the customers +$customers = $client->newCustomer()->all(); + +// Create a subscription for a customer +$customer = $client->newCustomer()->create(); +assert(!empty($customer->getId()), 'The created customer ID should not be empty'); + +$subscription = $client->newSubscription(array( + 'amount' => '9.99', + 'currency' => 'USD', + 'interval' => '1d', + 'name' => 'great subscription' +))->create($customer->getId()); +assert(!empty($subscription->getId()), 'The created subscription ID should not be empty'); diff --git a/src/Activity.php b/src/Activity.php index 1540957..7100d20 100755 --- a/src/Activity.php +++ b/src/Activity.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class Activity { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the activity @@ -56,18 +54,16 @@ class Activity /** * Activity constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; + + $this->fillWithData($prefill); } @@ -115,7 +111,7 @@ public function setProject($value) $this->project = $value; else { - $obj = new Project($this->instance); + $obj = new Project($this->client); $obj->fillWithData($value); $this->project = $obj; } @@ -218,43 +214,43 @@ public function setCreatedAt($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["project"])) - $this->setProject($data["project"]); + if(! empty($data['project'])) + $this->setProject($data['project']); - if(! empty($data["title"])) - $this->setTitle($data["title"]); + if(! empty($data['title'])) + $this->setTitle($data['title']); - if(! empty($data["content"])) - $this->setContent($data["content"]); + if(! empty($data['content'])) + $this->setContent($data['content']); - if(! empty($data["level"])) - $this->setLevel($data["level"]); + if(! empty($data['level'])) + $this->setLevel($data['level']); - if(! empty($data["created_at"])) - $this->setCreatedAt($data["created_at"]); + if(! empty($data['created_at'])) + $this->setCreatedAt($data['created_at']); return $this; } + /** * Get all the project activities. * @param array $options * @return array */ - public static function all($options = array()) + public function all($options = array()) { - $cur = new Activity(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/activities"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -263,45 +259,40 @@ public static function all($options = array()) $body = $response->getBody(); foreach($body['activities'] as $v) { - $tmp = new Activity($cur->instance); + $tmp = new Activity($this->client); $tmp->fillWithData($v); $a[] = $tmp; } - - $returnValues["Activities"] = $a; - - return array_values($returnValues)[0]; + $returnValues['Activities'] = $a; + return array_values($returnValues)[0]; } - + /** * Find a specific activity and fetch its data. * @param string $activityId * @param array $options * @return $this */ - public static function find($activityId, $options = array()) + public function find($activityId, $options = array()) { - $cur = new Activity(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/activities/" . urlencode($activityId) . ""; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field activity $body = $response->getBody(); - $body = $body['activity']; - - $returnValues["find"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['activity']; + $returnValues['find'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - } diff --git a/src/AuthorizationRequest.php b/src/AuthorizationRequest.php index c562690..f89fcc5 100755 --- a/src/AuthorizationRequest.php +++ b/src/AuthorizationRequest.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class AuthorizationRequest { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the authorization @@ -98,18 +96,16 @@ class AuthorizationRequest /** * AuthorizationRequest constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; + + $this->fillWithData($prefill); } @@ -157,7 +153,7 @@ public function setProject($value) $this->project = $value; else { - $obj = new Project($this->instance); + $obj = new Project($this->client); $obj->fillWithData($value); $this->project = $obj; } @@ -186,7 +182,7 @@ public function setCustomer($value) $this->customer = $value; else { - $obj = new Customer($this->instance); + $obj = new Customer($this->client); $obj->fillWithData($value); $this->customer = $obj; } @@ -215,7 +211,7 @@ public function setToken($value) $this->token = $value; else { - $obj = new Token($this->instance); + $obj = new Token($this->client); $obj->fillWithData($value); $this->token = $obj; } @@ -428,77 +424,77 @@ public function setCreatedAt($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["project"])) - $this->setProject($data["project"]); + if(! empty($data['project'])) + $this->setProject($data['project']); - if(! empty($data["customer"])) - $this->setCustomer($data["customer"]); + if(! empty($data['customer'])) + $this->setCustomer($data['customer']); - if(! empty($data["token"])) - $this->setToken($data["token"]); + if(! empty($data['token'])) + $this->setToken($data['token']); - if(! empty($data["url"])) - $this->setUrl($data["url"]); + if(! empty($data['url'])) + $this->setUrl($data['url']); - if(! empty($data["authorized"])) - $this->setAuthorized($data["authorized"]); + if(! empty($data['authorized'])) + $this->setAuthorized($data['authorized']); - if(! empty($data["name"])) - $this->setName($data["name"]); + if(! empty($data['name'])) + $this->setName($data['name']); - if(! empty($data["currency"])) - $this->setCurrency($data["currency"]); + if(! empty($data['currency'])) + $this->setCurrency($data['currency']); - if(! empty($data["return_url"])) - $this->setReturnUrl($data["return_url"]); + if(! empty($data['return_url'])) + $this->setReturnUrl($data['return_url']); - if(! empty($data["cancel_url"])) - $this->setCancelUrl($data["cancel_url"]); + if(! empty($data['cancel_url'])) + $this->setCancelUrl($data['cancel_url']); - if(! empty($data["custom"])) - $this->setCustom($data["custom"]); + if(! empty($data['custom'])) + $this->setCustom($data['custom']); - if(! empty($data["sandbox"])) - $this->setSandbox($data["sandbox"]); + if(! empty($data['sandbox'])) + $this->setSandbox($data['sandbox']); - if(! empty($data["created_at"])) - $this->setCreatedAt($data["created_at"]); + if(! empty($data['created_at'])) + $this->setCreatedAt($data['created_at']); return $this; } + /** * Get the customer linked to the authorization request. * @param array $options * @return Customer */ - public function customer($options = array()) + public function fetchCustomer($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/authorization-requests/" . urlencode($this->getId()) . "/customers"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field customer $body = $response->getBody(); $body = $body['customer']; - $customer = new Customer($cur->instance); - $returnValues["customer"] = $customer->fillWithData($body); + $customer = new Customer($this->client); + $returnValues['customer'] = $customer->fillWithData($body); - return array_values($returnValues)[0]; + return array_values($returnValues)[0]; } - + /** * Create a new authorization request for the given customer ID. * @param string $customerId @@ -507,8 +503,7 @@ public function customer($options = array()) */ public function create($customerId, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/authorization-requests"; $data = array( @@ -520,47 +515,43 @@ public function create($customerId, $options = array()) "customer_id" => $customerId ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field authorization_request $body = $response->getBody(); - $body = $body['authorization_request']; - - $returnValues["create"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['authorization_request']; + $returnValues['create'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Find an authorization request by its ID. * @param string $authorizationRequestId * @param array $options * @return $this */ - public static function find($authorizationRequestId, $options = array()) + public function find($authorizationRequestId, $options = array()) { - $cur = new AuthorizationRequest(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/authorization-requests/" . urlencode($authorizationRequestId) . ""; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field authorization_request $body = $response->getBody(); - $body = $body['authorization_request']; - - $returnValues["find"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['authorization_request']; + $returnValues['find'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - } diff --git a/src/Card.php b/src/Card.php index 3f9c01b..7a7e3e9 100755 --- a/src/Card.php +++ b/src/Card.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class Card { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the card @@ -98,19 +96,17 @@ class Card /** * Card constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; $this->setMetadata(array('_library' => 'php')); + + $this->fillWithData($prefill); } @@ -158,7 +154,7 @@ public function setProject($value) $this->project = $value; else { - $obj = new Project($this->instance); + $obj = new Project($this->client); $obj->fillWithData($value); $this->project = $obj; } @@ -415,44 +411,44 @@ public function setCreatedAt($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["project"])) - $this->setProject($data["project"]); + if(! empty($data['project'])) + $this->setProject($data['project']); - if(! empty($data["brand"])) - $this->setBrand($data["brand"]); + if(! empty($data['brand'])) + $this->setBrand($data['brand']); - if(! empty($data["type"])) - $this->setType($data["type"]); + if(! empty($data['type'])) + $this->setType($data['type']); - if(! empty($data["bank_name"])) - $this->setBankName($data["bank_name"]); + if(! empty($data['bank_name'])) + $this->setBankName($data['bank_name']); - if(! empty($data["level"])) - $this->setLevel($data["level"]); + if(! empty($data['level'])) + $this->setLevel($data['level']); - if(! empty($data["iin"])) - $this->setIin($data["iin"]); + if(! empty($data['iin'])) + $this->setIin($data['iin']); - if(! empty($data["last_4_digits"])) - $this->setLast4Digits($data["last_4_digits"]); + if(! empty($data['last_4_digits'])) + $this->setLast4Digits($data['last_4_digits']); - if(! empty($data["exp_month"])) - $this->setExpMonth($data["exp_month"]); + if(! empty($data['exp_month'])) + $this->setExpMonth($data['exp_month']); - if(! empty($data["exp_year"])) - $this->setExpYear($data["exp_year"]); + if(! empty($data['exp_year'])) + $this->setExpYear($data['exp_year']); - if(! empty($data["metadata"])) - $this->setMetadata($data["metadata"]); + if(! empty($data['metadata'])) + $this->setMetadata($data['metadata']); - if(! empty($data["sandbox"])) - $this->setSandbox($data["sandbox"]); + if(! empty($data['sandbox'])) + $this->setSandbox($data['sandbox']); - if(! empty($data["created_at"])) - $this->setCreatedAt($data["created_at"]); + if(! empty($data['created_at'])) + $this->setCreatedAt($data['created_at']); return $this; } diff --git a/src/Coupon.php b/src/Coupon.php index 9043d5c..e077fd8 100755 --- a/src/Coupon.php +++ b/src/Coupon.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class Coupon { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the coupon @@ -98,21 +96,19 @@ class Coupon /** * Coupon constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; $this->setMaxRedemptions((int) 0); $this->setMetadata(array('_library' => 'php')); $this->setIterationCount((int) 0); + + $this->fillWithData($prefill); } @@ -160,7 +156,7 @@ public function setProject($value) $this->project = $value; else { - $obj = new Project($this->instance); + $obj = new Project($this->client); $obj->fillWithData($value); $this->project = $obj; } @@ -417,64 +413,64 @@ public function setCreatedAt($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["project"])) - $this->setProject($data["project"]); + if(! empty($data['project'])) + $this->setProject($data['project']); - if(! empty($data["name"])) - $this->setName($data["name"]); + if(! empty($data['name'])) + $this->setName($data['name']); - if(! empty($data["amount_off"])) - $this->setAmountOff($data["amount_off"]); + if(! empty($data['amount_off'])) + $this->setAmountOff($data['amount_off']); - if(! empty($data["percent_off"])) - $this->setPercentOff($data["percent_off"]); + if(! empty($data['percent_off'])) + $this->setPercentOff($data['percent_off']); - if(! empty($data["currency"])) - $this->setCurrency($data["currency"]); + if(! empty($data['currency'])) + $this->setCurrency($data['currency']); - if(! empty($data["max_redemptions"])) - $this->setMaxRedemptions($data["max_redemptions"]); + if(! empty($data['max_redemptions'])) + $this->setMaxRedemptions($data['max_redemptions']); - if(! empty($data["expires_at"])) - $this->setExpiresAt($data["expires_at"]); + if(! empty($data['expires_at'])) + $this->setExpiresAt($data['expires_at']); - if(! empty($data["metadata"])) - $this->setMetadata($data["metadata"]); + if(! empty($data['metadata'])) + $this->setMetadata($data['metadata']); - if(! empty($data["iteration_count"])) - $this->setIterationCount($data["iteration_count"]); + if(! empty($data['iteration_count'])) + $this->setIterationCount($data['iteration_count']); - if(! empty($data["redeemed_number"])) - $this->setRedeemedNumber($data["redeemed_number"]); + if(! empty($data['redeemed_number'])) + $this->setRedeemedNumber($data['redeemed_number']); - if(! empty($data["sandbox"])) - $this->setSandbox($data["sandbox"]); + if(! empty($data['sandbox'])) + $this->setSandbox($data['sandbox']); - if(! empty($data["created_at"])) - $this->setCreatedAt($data["created_at"]); + if(! empty($data['created_at'])) + $this->setCreatedAt($data['created_at']); return $this; } + /** * Get all the coupons. * @param array $options * @return array */ - public static function all($options = array()) + public function all($options = array()) { - $cur = new Coupon(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/coupons"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -483,17 +479,15 @@ public static function all($options = array()) $body = $response->getBody(); foreach($body['coupons'] as $v) { - $tmp = new Coupon($cur->instance); + $tmp = new Coupon($this->client); $tmp->fillWithData($v); $a[] = $tmp; } - - $returnValues["Coupons"] = $a; - - return array_values($returnValues)[0]; + $returnValues['Coupons'] = $a; + return array_values($returnValues)[0]; } - + /** * Create a new coupon. * @param array $options @@ -501,8 +495,7 @@ public static function all($options = array()) */ public function create($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/coupons"; $data = array( @@ -516,48 +509,45 @@ public function create($options = array()) "metadata" => $this->getMetadata() ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field coupon $body = $response->getBody(); - $body = $body['coupon']; - - $returnValues["create"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['coupon']; + $returnValues['create'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Find a coupon by its ID. * @param string $couponId * @param array $options * @return $this */ - public static function find($couponId, $options = array()) + public function find($couponId, $options = array()) { - $cur = new Coupon(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/coupons/" . urlencode($couponId) . ""; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field coupon $body = $response->getBody(); - $body = $body['coupon']; - - $returnValues["find"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['coupon']; + $returnValues['find'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Save the updated coupon attributes. * @param array $options @@ -565,27 +555,25 @@ public static function find($couponId, $options = array()) */ public function save($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/coupons/" . urlencode($this->getId()) . ""; $data = array( "metadata" => $this->getMetadata() ); - $response = new Response($request->put($path, $data, $options)); + $response = $request->put($path, $data, $options); $returnValues = array(); // Handling for field coupon $body = $response->getBody(); - $body = $body['coupon']; - - $returnValues["save"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['coupon']; + $returnValues['save'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Delete the coupon. * @param array $options @@ -593,22 +581,19 @@ public function save($options = array()) */ public function delete($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/coupons/" . urlencode($this->getId()) . ""; $data = array( ); - $response = new Response($request->delete($path, $data, $options)); + $response = $request->delete($path, $data, $options); $returnValues = array(); + $returnValues['success'] = $response->isSuccess(); - $returnValues["success"] = $response->isSuccess(); return array_values($returnValues)[0]; - } - } diff --git a/src/Customer.php b/src/Customer.php index b09fbd7..659825b 100755 --- a/src/Customer.php +++ b/src/Customer.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class Customer { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the customer @@ -122,20 +120,18 @@ class Customer /** * Customer constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; $this->setBalance("0"); $this->setMetadata(array('_library' => 'php')); + + $this->fillWithData($prefill); } @@ -183,7 +179,7 @@ public function setProject($value) $this->project = $value; else { - $obj = new Project($this->instance); + $obj = new Project($this->client); $obj->fillWithData($value); $this->project = $obj; } @@ -528,76 +524,76 @@ public function setCreatedAt($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["project"])) - $this->setProject($data["project"]); + if(! empty($data['project'])) + $this->setProject($data['project']); - if(! empty($data["email"])) - $this->setEmail($data["email"]); + if(! empty($data['email'])) + $this->setEmail($data['email']); - if(! empty($data["first_name"])) - $this->setFirstName($data["first_name"]); + if(! empty($data['first_name'])) + $this->setFirstName($data['first_name']); - if(! empty($data["last_name"])) - $this->setLastName($data["last_name"]); + if(! empty($data['last_name'])) + $this->setLastName($data['last_name']); - if(! empty($data["address1"])) - $this->setAddress1($data["address1"]); + if(! empty($data['address1'])) + $this->setAddress1($data['address1']); - if(! empty($data["address2"])) - $this->setAddress2($data["address2"]); + if(! empty($data['address2'])) + $this->setAddress2($data['address2']); - if(! empty($data["city"])) - $this->setCity($data["city"]); + if(! empty($data['city'])) + $this->setCity($data['city']); - if(! empty($data["state"])) - $this->setState($data["state"]); + if(! empty($data['state'])) + $this->setState($data['state']); - if(! empty($data["zip"])) - $this->setZip($data["zip"]); + if(! empty($data['zip'])) + $this->setZip($data['zip']); - if(! empty($data["country_code"])) - $this->setCountryCode($data["country_code"]); + if(! empty($data['country_code'])) + $this->setCountryCode($data['country_code']); - if(! empty($data["balance"])) - $this->setBalance($data["balance"]); + if(! empty($data['balance'])) + $this->setBalance($data['balance']); - if(! empty($data["currency"])) - $this->setCurrency($data["currency"]); + if(! empty($data['currency'])) + $this->setCurrency($data['currency']); - if(! empty($data["metadata"])) - $this->setMetadata($data["metadata"]); + if(! empty($data['metadata'])) + $this->setMetadata($data['metadata']); - if(! empty($data["has_pin"])) - $this->setHasPin($data["has_pin"]); + if(! empty($data['has_pin'])) + $this->setHasPin($data['has_pin']); - if(! empty($data["sandbox"])) - $this->setSandbox($data["sandbox"]); + if(! empty($data['sandbox'])) + $this->setSandbox($data['sandbox']); - if(! empty($data["created_at"])) - $this->setCreatedAt($data["created_at"]); + if(! empty($data['created_at'])) + $this->setCreatedAt($data['created_at']); return $this; } + /** * Get the subscriptions belonging to the customer. * @param array $options * @return array */ - public function subscriptions($options = array()) + public function fetchSubscriptions($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/customers/" . urlencode($this->getId()) . "/subscriptions"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -606,33 +602,30 @@ public function subscriptions($options = array()) $body = $response->getBody(); foreach($body['subscriptions'] as $v) { - $tmp = new Subscription($cur->instance); + $tmp = new Subscription($this->client); $tmp->fillWithData($v); $a[] = $tmp; } - - $returnValues["Subscriptions"] = $a; - - return array_values($returnValues)[0]; + $returnValues['Subscriptions'] = $a; + return array_values($returnValues)[0]; } - + /** * Get the customer's tokens. * @param array $options * @return array */ - public function tokens($options = array()) + public function fetchTokens($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/customers/" . urlencode($this->getId()) . "/tokens"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -641,33 +634,59 @@ public function tokens($options = array()) $body = $response->getBody(); foreach($body['tokens'] as $v) { - $tmp = new Token($cur->instance); + $tmp = new Token($this->client); $tmp->fillWithData($v); $a[] = $tmp; } + $returnValues['Tokens'] = $a; + + return array_values($returnValues)[0]; + } + + /** + * Find a customer's token by its ID. + * @param string $tokenId + * @param array $options + * @return Token + */ + public function findToken($tokenId, $options = array()) + { + $request = new Request($this->client); + $path = "/customers/" . urlencode($this->getId()) . "/tokens/" . urlencode($tokenId) . ""; + + $data = array( - $returnValues["Tokens"] = $a; + ); + + $response = $request->get($path, $data, $options); + $returnValues = array(); + + + // Handling for field token + $body = $response->getBody(); + $body = $body['token']; + $token = new Token($this->client); + $returnValues['token'] = $token->fillWithData($body); - return array_values($returnValues)[0]; + return array_values($returnValues)[0]; } - + /** * Get the transactions belonging to the customer. * @param array $options * @return array */ - public function transactions($options = array()) + public function fetchTransactions($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/customers/" . urlencode($this->getId()) . "/transactions"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -676,33 +695,30 @@ public function transactions($options = array()) $body = $response->getBody(); foreach($body['transactions'] as $v) { - $tmp = new Transaction($cur->instance); + $tmp = new Transaction($this->client); $tmp->fillWithData($v); $a[] = $tmp; } - - $returnValues["Transactions"] = $a; - - return array_values($returnValues)[0]; + $returnValues['Transactions'] = $a; + return array_values($returnValues)[0]; } - + /** * Get all the customers. * @param array $options * @return array */ - public static function all($options = array()) + public function all($options = array()) { - $cur = new Customer(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/customers"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -711,17 +727,15 @@ public static function all($options = array()) $body = $response->getBody(); foreach($body['customers'] as $v) { - $tmp = new Customer($cur->instance); + $tmp = new Customer($this->client); $tmp->fillWithData($v); $a[] = $tmp; } - - $returnValues["Customers"] = $a; - - return array_values($returnValues)[0]; + $returnValues['Customers'] = $a; + return array_values($returnValues)[0]; } - + /** * Create a new customer. * @param array $options @@ -729,8 +743,7 @@ public static function all($options = array()) */ public function create($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/customers"; $data = array( @@ -748,48 +761,45 @@ public function create($options = array()) "metadata" => $this->getMetadata() ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field customer $body = $response->getBody(); - $body = $body['customer']; - - $returnValues["create"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['customer']; + $returnValues['create'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Find a customer by its ID. * @param string $customerId * @param array $options * @return $this */ - public static function find($customerId, $options = array()) + public function find($customerId, $options = array()) { - $cur = new Customer(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/customers/" . urlencode($customerId) . ""; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field customer $body = $response->getBody(); - $body = $body['customer']; - - $returnValues["find"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['customer']; + $returnValues['find'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Save the updated customer attributes. * @param array $options @@ -797,8 +807,7 @@ public static function find($customerId, $options = array()) */ public function save($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/customers/" . urlencode($this->getId()) . ""; $data = array( @@ -815,19 +824,18 @@ public function save($options = array()) "metadata" => $this->getMetadata() ); - $response = new Response($request->put($path, $data, $options)); + $response = $request->put($path, $data, $options); $returnValues = array(); // Handling for field customer $body = $response->getBody(); - $body = $body['customer']; - - $returnValues["save"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['customer']; + $returnValues['save'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Delete the customer. * @param array $options @@ -835,22 +843,19 @@ public function save($options = array()) */ public function delete($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/customers/" . urlencode($this->getId()) . ""; $data = array( ); - $response = new Response($request->delete($path, $data, $options)); + $response = $request->delete($path, $data, $options); $returnValues = array(); + $returnValues['success'] = $response->isSuccess(); - $returnValues["success"] = $response->isSuccess(); return array_values($returnValues)[0]; - } - } diff --git a/src/CustomerAction.php b/src/CustomerAction.php index b8d8aa3..8ce4ced 100755 --- a/src/CustomerAction.php +++ b/src/CustomerAction.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class CustomerAction { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * Customer action type (such as url) @@ -32,18 +30,16 @@ class CustomerAction /** * CustomerAction constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; + + $this->fillWithData($prefill); } @@ -99,11 +95,11 @@ public function setValue($value) */ public function fillWithData($data) { - if(! empty($data["type"])) - $this->setType($data["type"]); + if(! empty($data['type'])) + $this->setType($data['type']); - if(! empty($data["value"])) - $this->setValue($data["value"]); + if(! empty($data['value'])) + $this->setValue($data['value']); return $this; } diff --git a/src/Discount.php b/src/Discount.php index 6a8578b..f6eba87 100755 --- a/src/Discount.php +++ b/src/Discount.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class Discount { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the discount @@ -74,19 +72,17 @@ class Discount /** * Discount constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; $this->setMetadata(array('_library' => 'php')); + + $this->fillWithData($prefill); } @@ -134,7 +130,7 @@ public function setProject($value) $this->project = $value; else { - $obj = new Project($this->instance); + $obj = new Project($this->client); $obj->fillWithData($value); $this->project = $obj; } @@ -163,7 +159,7 @@ public function setSubscription($value) $this->subscription = $value; else { - $obj = new Subscription($this->instance); + $obj = new Subscription($this->client); $obj->fillWithData($value); $this->subscription = $obj; } @@ -192,7 +188,7 @@ public function setCoupon($value) $this->coupon = $value; else { - $obj = new Coupon($this->instance); + $obj = new Coupon($this->client); $obj->fillWithData($value); $this->coupon = $obj; } @@ -317,36 +313,37 @@ public function setCreatedAt($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["project"])) - $this->setProject($data["project"]); + if(! empty($data['project'])) + $this->setProject($data['project']); - if(! empty($data["subscription"])) - $this->setSubscription($data["subscription"]); + if(! empty($data['subscription'])) + $this->setSubscription($data['subscription']); - if(! empty($data["coupon"])) - $this->setCoupon($data["coupon"]); + if(! empty($data['coupon'])) + $this->setCoupon($data['coupon']); - if(! empty($data["amount"])) - $this->setAmount($data["amount"]); + if(! empty($data['amount'])) + $this->setAmount($data['amount']); - if(! empty($data["expires_at"])) - $this->setExpiresAt($data["expires_at"]); + if(! empty($data['expires_at'])) + $this->setExpiresAt($data['expires_at']); - if(! empty($data["metadata"])) - $this->setMetadata($data["metadata"]); + if(! empty($data['metadata'])) + $this->setMetadata($data['metadata']); - if(! empty($data["sandbox"])) - $this->setSandbox($data["sandbox"]); + if(! empty($data['sandbox'])) + $this->setSandbox($data['sandbox']); - if(! empty($data["created_at"])) - $this->setCreatedAt($data["created_at"]); + if(! empty($data['created_at'])) + $this->setCreatedAt($data['created_at']); return $this; } + /** * Apply a new discount to the given subscription ID. * @param string $subscriptionId @@ -355,8 +352,7 @@ public function fillWithData($data) */ public function apply($subscriptionId, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/subscriptions/" . urlencode($subscriptionId) . "/discounts"; $data = array( @@ -365,19 +361,18 @@ public function apply($subscriptionId, $options = array()) "metadata" => $this->getMetadata() ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field discount $body = $response->getBody(); - $body = $body['discount']; - - $returnValues["apply"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['discount']; + $returnValues['apply'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Apply a new discount to the given subscription ID from a coupon ID. * @param string $subscriptionId @@ -387,8 +382,7 @@ public function apply($subscriptionId, $options = array()) */ public function applyCoupon($subscriptionId, $couponId, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/subscriptions/" . urlencode($subscriptionId) . "/discounts"; $data = array( @@ -398,19 +392,18 @@ public function applyCoupon($subscriptionId, $couponId, $options = array()) "coupon_id" => $couponId ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field discount $body = $response->getBody(); - $body = $body['discount']; - - $returnValues["applyCoupon"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['discount']; + $returnValues['applyCoupon'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Find a subscription's discount by its ID. * @param string $subscriptionId @@ -418,28 +411,25 @@ public function applyCoupon($subscriptionId, $couponId, $options = array()) * @param array $options * @return $this */ - public static function find($subscriptionId, $discountId, $options = array()) + public function find($subscriptionId, $discountId, $options = array()) { - $cur = new Discount(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/subscriptions/" . urlencode($subscriptionId) . "/discounts/" . urlencode($discountId) . ""; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field discount $body = $response->getBody(); - $body = $body['discount']; - - $returnValues["find"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['discount']; + $returnValues['find'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - } diff --git a/src/Event.php b/src/Event.php index 7d10041..50f660e 100755 --- a/src/Event.php +++ b/src/Event.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class Event { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the event @@ -56,18 +54,16 @@ class Event /** * Event constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; + + $this->fillWithData($prefill); } @@ -115,7 +111,7 @@ public function setProject($value) $this->project = $value; else { - $obj = new Project($this->instance); + $obj = new Project($this->client); $obj->fillWithData($value); $this->project = $obj; } @@ -218,43 +214,43 @@ public function setFiredAt($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["project"])) - $this->setProject($data["project"]); + if(! empty($data['project'])) + $this->setProject($data['project']); - if(! empty($data["name"])) - $this->setName($data["name"]); + if(! empty($data['name'])) + $this->setName($data['name']); - if(! empty($data["data"])) - $this->setData($data["data"]); + if(! empty($data['data'])) + $this->setData($data['data']); - if(! empty($data["sandbox"])) - $this->setSandbox($data["sandbox"]); + if(! empty($data['sandbox'])) + $this->setSandbox($data['sandbox']); - if(! empty($data["fired_at"])) - $this->setFiredAt($data["fired_at"]); + if(! empty($data['fired_at'])) + $this->setFiredAt($data['fired_at']); return $this; } + /** * Get all the webhooks of the event. * @param array $options * @return array */ - public function webhooks($options = array()) + public function fetchWebhooks($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/events/" . urlencode($this->getId()) . "/webhooks"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -263,33 +259,30 @@ public function webhooks($options = array()) $body = $response->getBody(); foreach($body['webhooks'] as $v) { - $tmp = new Webhook($cur->instance); + $tmp = new Webhook($this->client); $tmp->fillWithData($v); $a[] = $tmp; } - - $returnValues["Webhooks"] = $a; - - return array_values($returnValues)[0]; + $returnValues['Webhooks'] = $a; + return array_values($returnValues)[0]; } - + /** * Get all the events. * @param array $options * @return array */ - public static function all($options = array()) + public function all($options = array()) { - $cur = new Event(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/events"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -298,45 +291,40 @@ public static function all($options = array()) $body = $response->getBody(); foreach($body['events'] as $v) { - $tmp = new Event($cur->instance); + $tmp = new Event($this->client); $tmp->fillWithData($v); $a[] = $tmp; } - - $returnValues["Events"] = $a; - - return array_values($returnValues)[0]; + $returnValues['Events'] = $a; + return array_values($returnValues)[0]; } - + /** * Find an event by its ID. * @param string $eventId * @param array $options * @return $this */ - public static function find($eventId, $options = array()) + public function find($eventId, $options = array()) { - $cur = new Event(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/events/" . urlencode($eventId) . ""; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field event $body = $response->getBody(); - $body = $body['event']; - - $returnValues["find"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['event']; + $returnValues['find'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - } diff --git a/src/Exceptions/ApiAuthenticationException.php b/src/Exceptions/AuthenticationException.php similarity index 88% rename from src/Exceptions/ApiAuthenticationException.php rename to src/Exceptions/AuthenticationException.php index 12a65d5..34478ff 100644 --- a/src/Exceptions/ApiAuthenticationException.php +++ b/src/Exceptions/AuthenticationException.php @@ -4,7 +4,7 @@ use \Exception; -class ApiAuthenticationException extends Exception +class AuthenticationException extends Exception { /** @@ -18,4 +18,4 @@ public function __construct($message, $code = 0, Exception $previous = null) parent::__construct($message, $code, $previous); } -} \ No newline at end of file +} diff --git a/src/Exceptions/ApiException.php b/src/Exceptions/GenericException.php similarity index 89% rename from src/Exceptions/ApiException.php rename to src/Exceptions/GenericException.php index df68a4d..afddc16 100644 --- a/src/Exceptions/ApiException.php +++ b/src/Exceptions/GenericException.php @@ -4,7 +4,7 @@ use \Exception; -class ApiException extends Exception +class GenericException extends Exception { /** @@ -18,4 +18,4 @@ public function __construct($message, $code = 0, Exception $previous = null) parent::__construct($message, $code, $previous); } -} \ No newline at end of file +} diff --git a/src/Exceptions/InternalException.php b/src/Exceptions/InternalException.php new file mode 100644 index 0000000..1cc765f --- /dev/null +++ b/src/Exceptions/InternalException.php @@ -0,0 +1,21 @@ +instance = $processOut; + $this->client = $client; + + $this->fillWithData($prefill); } @@ -267,29 +263,29 @@ public function setDescription($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["name"])) - $this->setName($data["name"]); + if(! empty($data['name'])) + $this->setName($data['name']); - if(! empty($data["display_name"])) - $this->setDisplayName($data["display_name"]); + if(! empty($data['display_name'])) + $this->setDisplayName($data['display_name']); - if(! empty($data["logo_url"])) - $this->setLogoUrl($data["logo_url"]); + if(! empty($data['logo_url'])) + $this->setLogoUrl($data['logo_url']); - if(! empty($data["url"])) - $this->setUrl($data["url"]); + if(! empty($data['url'])) + $this->setUrl($data['url']); - if(! empty($data["flows"])) - $this->setFlows($data["flows"]); + if(! empty($data['flows'])) + $this->setFlows($data['flows']); - if(! empty($data["tags"])) - $this->setTags($data["tags"]); + if(! empty($data['tags'])) + $this->setTags($data['tags']); - if(! empty($data["description"])) - $this->setDescription($data["description"]); + if(! empty($data['description'])) + $this->setDescription($data['description']); return $this; } diff --git a/src/GatewayConfiguration.php b/src/GatewayConfiguration.php index 8ea56af..7299426 100755 --- a/src/GatewayConfiguration.php +++ b/src/GatewayConfiguration.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class GatewayConfiguration { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the gateway configuration @@ -50,19 +48,17 @@ class GatewayConfiguration /** * GatewayConfiguration constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; $this->setPublicKeys(array('_library' => 'php')); + + $this->fillWithData($prefill); } @@ -110,7 +106,7 @@ public function setProject($value) $this->project = $value; else { - $obj = new Project($this->instance); + $obj = new Project($this->client); $obj->fillWithData($value); $this->project = $obj; } @@ -139,7 +135,7 @@ public function setGateway($value) $this->gateway = $value; else { - $obj = new Gateway($this->instance); + $obj = new Gateway($this->client); $obj->fillWithData($value); $this->gateway = $obj; } @@ -198,20 +194,20 @@ public function setPublicKeys($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["project"])) - $this->setProject($data["project"]); + if(! empty($data['project'])) + $this->setProject($data['project']); - if(! empty($data["gateway"])) - $this->setGateway($data["gateway"]); + if(! empty($data['gateway'])) + $this->setGateway($data['gateway']); - if(! empty($data["enabled"])) - $this->setEnabled($data["enabled"]); + if(! empty($data['enabled'])) + $this->setEnabled($data['enabled']); - if(! empty($data["public_keys"])) - $this->setPublicKeys($data["public_keys"]); + if(! empty($data['public_keys'])) + $this->setPublicKeys($data['public_keys']); return $this; } diff --git a/src/Invoice.php b/src/Invoice.php index 7101a81..9fd1c50 100755 --- a/src/Invoice.php +++ b/src/Invoice.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class Invoice { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the invoice @@ -116,21 +114,19 @@ class Invoice /** * Invoice constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; $this->setMetadata(array('_library' => 'php')); $this->setRequestEmail((bool) false); $this->setRequestShipping((bool) false); + + $this->fillWithData($prefill); } @@ -178,7 +174,7 @@ public function setProject($value) $this->project = $value; else { - $obj = new Project($this->instance); + $obj = new Project($this->client); $obj->fillWithData($value); $this->project = $obj; } @@ -207,7 +203,7 @@ public function setTransaction($value) $this->transaction = $value; else { - $obj = new Transaction($this->instance); + $obj = new Transaction($this->client); $obj->fillWithData($value); $this->transaction = $obj; } @@ -236,7 +232,7 @@ public function setCustomer($value) $this->customer = $value; else { - $obj = new Customer($this->instance); + $obj = new Customer($this->client); $obj->fillWithData($value); $this->customer = $obj; } @@ -265,7 +261,7 @@ public function setSubscription($value) $this->subscription = $value; else { - $obj = new Subscription($this->instance); + $obj = new Subscription($this->client); $obj->fillWithData($value); $this->subscription = $obj; } @@ -522,57 +518,58 @@ public function setCreatedAt($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["project"])) - $this->setProject($data["project"]); + if(! empty($data['project'])) + $this->setProject($data['project']); - if(! empty($data["transaction"])) - $this->setTransaction($data["transaction"]); + if(! empty($data['transaction'])) + $this->setTransaction($data['transaction']); - if(! empty($data["customer"])) - $this->setCustomer($data["customer"]); + if(! empty($data['customer'])) + $this->setCustomer($data['customer']); - if(! empty($data["subscription"])) - $this->setSubscription($data["subscription"]); + if(! empty($data['subscription'])) + $this->setSubscription($data['subscription']); - if(! empty($data["url"])) - $this->setUrl($data["url"]); + if(! empty($data['url'])) + $this->setUrl($data['url']); - if(! empty($data["name"])) - $this->setName($data["name"]); + if(! empty($data['name'])) + $this->setName($data['name']); - if(! empty($data["amount"])) - $this->setAmount($data["amount"]); + if(! empty($data['amount'])) + $this->setAmount($data['amount']); - if(! empty($data["currency"])) - $this->setCurrency($data["currency"]); + if(! empty($data['currency'])) + $this->setCurrency($data['currency']); - if(! empty($data["metadata"])) - $this->setMetadata($data["metadata"]); + if(! empty($data['metadata'])) + $this->setMetadata($data['metadata']); - if(! empty($data["request_email"])) - $this->setRequestEmail($data["request_email"]); + if(! empty($data['request_email'])) + $this->setRequestEmail($data['request_email']); - if(! empty($data["request_shipping"])) - $this->setRequestShipping($data["request_shipping"]); + if(! empty($data['request_shipping'])) + $this->setRequestShipping($data['request_shipping']); - if(! empty($data["return_url"])) - $this->setReturnUrl($data["return_url"]); + if(! empty($data['return_url'])) + $this->setReturnUrl($data['return_url']); - if(! empty($data["cancel_url"])) - $this->setCancelUrl($data["cancel_url"]); + if(! empty($data['cancel_url'])) + $this->setCancelUrl($data['cancel_url']); - if(! empty($data["sandbox"])) - $this->setSandbox($data["sandbox"]); + if(! empty($data['sandbox'])) + $this->setSandbox($data['sandbox']); - if(! empty($data["created_at"])) - $this->setCreatedAt($data["created_at"]); + if(! empty($data['created_at'])) + $this->setCreatedAt($data['created_at']); return $this; } + /** * Authorize the invoice using the given source (customer or token) * @param string $source @@ -581,28 +578,27 @@ public function fillWithData($data) */ public function authorize($source, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/invoices/" . urlencode($this->getId()) . "/authorize"; $data = array( "source" => $source ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field transaction $body = $response->getBody(); $body = $body['transaction']; - $transaction = new Transaction($cur->instance); - $returnValues["transaction"] = $transaction->fillWithData($body); + $transaction = new Transaction($this->client); + $returnValues['transaction'] = $transaction->fillWithData($body); - return array_values($returnValues)[0]; + return array_values($returnValues)[0]; } - + /** * Capture the invoice using the given source (customer or token) * @param string $source @@ -611,57 +607,55 @@ public function authorize($source, $options = array()) */ public function capture($source, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/invoices/" . urlencode($this->getId()) . "/capture"; $data = array( "source" => $source ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field transaction $body = $response->getBody(); $body = $body['transaction']; - $transaction = new Transaction($cur->instance); - $returnValues["transaction"] = $transaction->fillWithData($body); + $transaction = new Transaction($this->client); + $returnValues['transaction'] = $transaction->fillWithData($body); - return array_values($returnValues)[0]; + return array_values($returnValues)[0]; } - + /** * Get the customer linked to the invoice. * @param array $options * @return Customer */ - public function customer($options = array()) + public function fetchCustomer($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/invoices/" . urlencode($this->getId()) . "/customers"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field customer $body = $response->getBody(); $body = $body['customer']; - $customer = new Customer($cur->instance); - $returnValues["customer"] = $customer->fillWithData($body); + $customer = new Customer($this->client); + $returnValues['customer'] = $customer->fillWithData($body); - return array_values($returnValues)[0]; + return array_values($returnValues)[0]; } - + /** * Assign a customer to the invoice. * @param string $customerId @@ -670,57 +664,55 @@ public function customer($options = array()) */ public function assignCustomer($customerId, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/invoices/" . urlencode($this->getId()) . "/customers"; $data = array( "customer_id" => $customerId ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field customer $body = $response->getBody(); $body = $body['customer']; - $customer = new Customer($cur->instance); - $returnValues["customer"] = $customer->fillWithData($body); + $customer = new Customer($this->client); + $returnValues['customer'] = $customer->fillWithData($body); - return array_values($returnValues)[0]; + return array_values($returnValues)[0]; } - + /** * Get the transaction of the invoice. * @param array $options * @return Transaction */ - public function transaction($options = array()) + public function fetchTransaction($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/invoices/" . urlencode($this->getId()) . "/transactions"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field transaction $body = $response->getBody(); $body = $body['transaction']; - $transaction = new Transaction($cur->instance); - $returnValues["transaction"] = $transaction->fillWithData($body); + $transaction = new Transaction($this->client); + $returnValues['transaction'] = $transaction->fillWithData($body); - return array_values($returnValues)[0]; + return array_values($returnValues)[0]; } - + /** * Void the invoice * @param array $options @@ -728,44 +720,42 @@ public function transaction($options = array()) */ public function void($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/invoices/" . urlencode($this->getId()) . "/void"; $data = array( ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field transaction $body = $response->getBody(); $body = $body['transaction']; - $transaction = new Transaction($cur->instance); - $returnValues["transaction"] = $transaction->fillWithData($body); + $transaction = new Transaction($this->client); + $returnValues['transaction'] = $transaction->fillWithData($body); - return array_values($returnValues)[0]; + return array_values($returnValues)[0]; } - + /** * Get all the invoices. * @param array $options * @return array */ - public static function all($options = array()) + public function all($options = array()) { - $cur = new Invoice(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/invoices"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -774,17 +764,15 @@ public static function all($options = array()) $body = $response->getBody(); foreach($body['invoices'] as $v) { - $tmp = new Invoice($cur->instance); + $tmp = new Invoice($this->client); $tmp->fillWithData($v); $a[] = $tmp; } - - $returnValues["Invoices"] = $a; - - return array_values($returnValues)[0]; + $returnValues['Invoices'] = $a; + return array_values($returnValues)[0]; } - + /** * Create a new invoice. * @param array $options @@ -792,8 +780,7 @@ public static function all($options = array()) */ public function create($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/invoices"; $data = array( @@ -807,19 +794,18 @@ public function create($options = array()) "cancel_url" => $this->getCancelUrl() ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field invoice $body = $response->getBody(); - $body = $body['invoice']; - - $returnValues["create"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['invoice']; + $returnValues['create'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Create a new invoice for the given customer ID. * @param string $customerId @@ -828,8 +814,7 @@ public function create($options = array()) */ public function createForCustomer($customerId, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/invoices"; $data = array( @@ -844,47 +829,43 @@ public function createForCustomer($customerId, $options = array()) "customer_id" => $customerId ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field invoice $body = $response->getBody(); - $body = $body['invoice']; - - $returnValues["createForCustomer"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['invoice']; + $returnValues['createForCustomer'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Find an invoice by its ID. * @param string $invoiceId * @param array $options * @return $this */ - public static function find($invoiceId, $options = array()) + public function find($invoiceId, $options = array()) { - $cur = new Invoice(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/invoices/" . urlencode($invoiceId) . ""; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field invoice $body = $response->getBody(); - $body = $body['invoice']; - - $returnValues["find"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['invoice']; + $returnValues['find'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - } diff --git a/src/Networking/Request.php b/src/Networking/Request.php new file mode 100644 index 0000000..520a577 --- /dev/null +++ b/src/Networking/Request.php @@ -0,0 +1,178 @@ +client = $client; + } + + /** + * Prepare the request + * @param array $options + * @return $request + */ + protected function prepare($options, $len = null) + { + $request = curl_init(); + $headers = array( + 'API-Version: 1.3.0.0', + 'Content-Type: application/json' + ); + if (! empty($options['idempotency_key'])) + $headers[] = 'Idempotency-Key: ' . $options['idempotency_key']; + if (! empty($options['disable_logging'])) + $headers[] = 'Disable-Logging' . $options['disable_logging'] ? 'true' : ''; + if ($len != null) + $headers[] = 'Content-Length' . ((string) $len); + + curl_setopt($request, CURLOPT_USERPWD, $this->client->getProjectID().':'. + $this->client->getProjectSecret()); + curl_setopt($request, CURLOPT_HTTPHEADER, $headers); + + curl_setopt($request, CURLOPT_TIMEOUT, 30); + curl_setopt($request, CURLOPT_MAXREDIRS, 4); + curl_setopt($request, CURLOPT_RETURNTRANSFER, true); + curl_setopt($request, CURLOPT_FOLLOWLOCATION, true); + + return $request; + } + + /** + * Prepare the request data + * @param array $data + * @param array $options + * @return array $data + */ + protected function getData($data, $options) + { + if (! empty($options['expand'])) + $data['expand'] = $options['expand']; + if (! empty($options['filter'])) + $data['filter'] = $options['filter']; + if (! empty($options['limit'])) + $data['limit'] = $options['limit']; + if (! empty($options['page'])) + $data['page'] = $options['page']; + if (! empty($options['end_before'])) + $data['end_before'] = $options['end_before']; + if (! empty($options['start_after'])) + $data['start_after'] = $options['start_after']; + + return $data; + } + + /** + * Generate a get request + * @param string $path + * @param array $data + * @param array $options + * @return return Response + */ + public function get($path, $data, $options) + { + $req = $this->prepare($options); + curl_setopt($req, CURLOPT_URL, $this->client->getHost() . $path . '?' . + http_build_query($this->getData($data, $options))); + $r = curl_exec($req); + if (!$r) + throw new \Exception('curl exception: '.curl_error($req). + ', code: '.curl_errno($req)); + + $status = curl_getinfo($req, CURLINFO_HTTP_CODE); + curl_close($req); + + return new Response($r, $status); + } + + /** + * Generate a POST request + * @param string $path + * @param array $data + * @param array $options + * @return return Response + */ + public function post($path, $data, $options) + { + $body = json_encode($this->getData($data, $options)); + + $req = $this->prepare($options, strlen($body)); + curl_setopt($req, CURLOPT_POST, true); + curl_setopt($req, CURLOPT_POSTFIELDS, $body); + + curl_setopt($req, CURLOPT_URL, $this->client->getHost() . $path); + $r = curl_exec($req); + if (!$r) + throw new \Exception('curl exception: '.curl_error($req). + ', code: '.curl_errno($req)); + + $status = curl_getinfo($req, CURLINFO_HTTP_CODE); + curl_close($req); + + return new Response($r, $status); + } + + /** + * Generate a PUT request + * @param string $path + * @param array $data + * @param array $options + * @return return Response + */ + public function put($path, $data, $options) + { + $body = json_encode($this->getData($data, $options)); + + $req = $this->prepare($options, strlen($body)); + curl_setopt($req, CURLOPT_CUSTOMREQUEST, 'PUT'); + curl_setopt($req, CURLOPT_POSTFIELDS, $body); + + curl_setopt($req, CURLOPT_URL, $this->client->getHost() . $path); + $r = curl_exec($req); + if (!$r) + throw new \Exception('curl exception: '.curl_error($req). + ', code: '.curl_errno($req)); + + $status = curl_getinfo($req, CURLINFO_HTTP_CODE); + curl_close($req); + + return new Response($r, $status); + } + + /** + * Generate a DELETE request + * @param string $path + * @param array $data + * @param array $options + * @return return Response + */ + public function delete($path, $data, $options) + { + $req = $this->prepare($options); + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); + curl_setopt($req, CURLOPT_URL, $this->client->getHost() . $path . '?' . + http_build_query($this->getData($data, $options))); + $r = curl_exec($req); + if (!$r) + throw new \Exception('curl exception: '.curl_error($req). + ', code: '.curl_errno($req)); + + $status = curl_getinfo($req, CURLINFO_HTTP_CODE); + curl_close($req); + + return new Response($r, $status); + } +} diff --git a/src/Networking/RequestProcessoutPrivate.php b/src/Networking/RequestProcessoutPrivate.php deleted file mode 100755 index 7380d40..0000000 --- a/src/Networking/RequestProcessoutPrivate.php +++ /dev/null @@ -1,150 +0,0 @@ -processOut = $processOut; - - $this->cURL = $this->processOut->getCurl(); - } - - /** - * Authenticate the request - * @param anlutro\cURL\Request $request - * @return anlutro\cURL\Request $request - */ - protected function authenticate($request) - { - $auth = ''; - $auth .= $this->processOut->getProjectId(); - $auth .= ':'; - $auth .= $this->processOut->getProjectSecret(); - $request->setOptions(array(CURLOPT_USERPWD => $auth)); - - return $request; - } - - /** - * Prepare the request - * @param anlutro\cURL\Request $request - * @param array $options - * @return anlutro\cURL\Request $request - */ - protected function prepare($request, $options) - { - $request = $this->authenticate($request); - $request = $request->setHeader('API-Version', '1.3.0.0'); - if (! empty($options['idempotency_key'])) - $request = $request->setHeader('Idempotency-Key', - $options['idempotency_key']); - - return $request; - } - - /** - * Prepare the request data - * @param array $data - * @param array $options - * @return array $data - */ - protected function getData($data, $options) - { - if (! empty($options['expand'])) - $data['expand'] = $data['expand']; - - if (! empty($options['filter'])) - $data['filter'] = $data['filter']; - - return $data; - } - - /** - * Generate a get request - * @param string $path - * @param array $data - * @param array $options - * @return return anlutro\cURL\Response - */ - public function get($path, $data, $options) - { - $request = $this->cURL->newJsonRequest('GET', - $this->processOut->getHost() . $path . "?" . - http_build_query($this->getData($data, $options)) - ); - $this->prepare($request, $options); - - return $request->send(); - } - - /** - * Generate a POST request - * @param string $path - * @param array $data - * @param array $options - * @return return anlutro\cURL\Response - */ - public function post($path, $data, $options) - { - $request = $this->cURL->newJsonRequest('POST', - $this->processOut->getHost() . $path, - $this->getData($data, $options) - ); - $this->prepare($request, $options); - - return $request->send(); - } - - /** - * Generate a PUT request - * @param string $path - * @param array $data - * @param array $options - * @return return anlutro\cURL\Response - */ - public function put($path, $data, $options) - { - $request = $this->cURL->newJsonRequest('PUT', - $this->processOut->getHost() . $path, - $this->getData($data, $options) - ); - $this->prepare($request, $options); - - return $request->send(); - } - - /** - * Generate a DELETE request - * @param string $path - * @param array $data - * @param array $options - * @return return anlutro\cURL\Response - */ - public function delete($path, $data, $options) - { - $request = $this->cURL->newJsonRequest('DELETE', - $this->processOut->getHost() . $path . "?" . - http_build_query($this->getData($data, $options)) - ); - $this->prepare($request, $options); - - return $request->send(); - } - -} diff --git a/src/Networking/Response.php b/src/Networking/Response.php index 3dbe3e8..0d6a702 100644 --- a/src/Networking/Response.php +++ b/src/Networking/Response.php @@ -2,31 +2,21 @@ namespace ProcessOut\Networking; -use \ProcessOut\Exceptions\ApiException; +use \ProcessOut\Exceptions\AuthenticationException; +use \ProcessOut\Exceptions\GenericException; +use \ProcessOut\Exceptions\InternalException; use \ProcessOut\Exceptions\NotFoundException; -use \ProcessOut\Exceptions\ApiAuthenticationException; +use \ProcessOut\Exceptions\ValidationException; class Response { - /** - * Raw response - * @var anlutro\cURL\Response - */ - protected $raw; - /** * Status code * @var string */ protected $statusCode; - /** - * Response headers - * @var array - */ - protected $headers; - /** * Json decoded body * @var StdClass @@ -35,30 +25,17 @@ class Response /** * Response's constructor - * @param anlutro\cURL\Response $raw - * @param bool|true $checkStatusCode + * @param string $raw + * @param in $status */ - public function __construct(\anlutro\cURL\Response $raw) + public function __construct($raw, $status) { - $this->raw = $raw; - $this->statusCode = $raw->statusCode; - $this->headers = array_change_key_case($raw->headers, CASE_LOWER); - $this->rawObject = $raw->body; - - $this->body = (array) json_decode($raw->body, true); + $this->statusCode = $status; + $this->body = (array) json_decode($raw, true); $this->check(); } - /** - * Return the raw response - * @return anlutro\cURL\Response - */ - public function getRaw() - { - return $this->raw; - } - /** * Get the status code * @return int @@ -74,7 +51,7 @@ public function getStatusCode() */ public function getStatusString() { - return $this->statusCode; + return (string) $this->statusCode; } /** @@ -92,10 +69,7 @@ public function getBody() */ public function isSuccess() { - if(! isset($this->body["success"]) || ! $this->body["success"]) - return false; - - return true; + return (isset($this->body['success']) && $this->body['success']); } /** @@ -106,13 +80,12 @@ public function isSuccess() public function getMessage() { $message = ''; - if(! empty($this->body["message"])) - $message .= $this->body["message"]; + if(! empty($this->body['message'])) + $message .= $this->body['message']; return $message; } - /** * Throw an exception if there's been an error in the current response * @return void @@ -129,14 +102,26 @@ protected function check() } if($this->getStatusCode() == 401) { - throw new ApiAuthenticationException( + throw new AuthenticationException( 'Your ProcessOut credentials could not be verified (401): ' . $this->getMessage()); } + if($this->getStatusCode() == 400) + { + throw new ValidationException( + 'Your request could not be processed (400): ' . + $this->getMessage()); + } + if($this->getStatusCode() >= 500) + { + throw new InternalException( + 'ProcessOut returned an internal error (' . + $this->getStatusString() . '): ' . $this->getMessage()); + } - throw new ApiException( + throw new GenericException( 'ProcessOut returned an error (' . - $this->getStatusCode() . '): ' . $this->getMessage()); + $this->getStatusString() . '): ' . $this->getMessage()); } } } diff --git a/src/Plan.php b/src/Plan.php index 94b3a58..083baba 100755 --- a/src/Plan.php +++ b/src/Plan.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class Plan { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the plan @@ -92,20 +90,18 @@ class Plan /** * Plan constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; $this->setMetadata(array('_library' => 'php')); $this->setTrialPeriod("0d"); + + $this->fillWithData($prefill); } @@ -153,7 +149,7 @@ public function setProject($value) $this->project = $value; else { - $obj = new Project($this->instance); + $obj = new Project($this->client); $obj->fillWithData($value); $this->project = $obj; } @@ -388,61 +384,61 @@ public function setCreatedAt($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["project"])) - $this->setProject($data["project"]); + if(! empty($data['project'])) + $this->setProject($data['project']); - if(! empty($data["name"])) - $this->setName($data["name"]); + if(! empty($data['name'])) + $this->setName($data['name']); - if(! empty($data["amount"])) - $this->setAmount($data["amount"]); + if(! empty($data['amount'])) + $this->setAmount($data['amount']); - if(! empty($data["currency"])) - $this->setCurrency($data["currency"]); + if(! empty($data['currency'])) + $this->setCurrency($data['currency']); - if(! empty($data["metadata"])) - $this->setMetadata($data["metadata"]); + if(! empty($data['metadata'])) + $this->setMetadata($data['metadata']); - if(! empty($data["interval"])) - $this->setInterval($data["interval"]); + if(! empty($data['interval'])) + $this->setInterval($data['interval']); - if(! empty($data["trial_period"])) - $this->setTrialPeriod($data["trial_period"]); + if(! empty($data['trial_period'])) + $this->setTrialPeriod($data['trial_period']); - if(! empty($data["return_url"])) - $this->setReturnUrl($data["return_url"]); + if(! empty($data['return_url'])) + $this->setReturnUrl($data['return_url']); - if(! empty($data["cancel_url"])) - $this->setCancelUrl($data["cancel_url"]); + if(! empty($data['cancel_url'])) + $this->setCancelUrl($data['cancel_url']); - if(! empty($data["sandbox"])) - $this->setSandbox($data["sandbox"]); + if(! empty($data['sandbox'])) + $this->setSandbox($data['sandbox']); - if(! empty($data["created_at"])) - $this->setCreatedAt($data["created_at"]); + if(! empty($data['created_at'])) + $this->setCreatedAt($data['created_at']); return $this; } + /** * Get all the plans. * @param array $options * @return array */ - public static function all($options = array()) + public function all($options = array()) { - $cur = new Plan(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/plans"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -451,17 +447,15 @@ public static function all($options = array()) $body = $response->getBody(); foreach($body['plans'] as $v) { - $tmp = new Plan($cur->instance); + $tmp = new Plan($this->client); $tmp->fillWithData($v); $a[] = $tmp; } - - $returnValues["Plans"] = $a; - - return array_values($returnValues)[0]; + $returnValues['Plans'] = $a; + return array_values($returnValues)[0]; } - + /** * Create a new plan. * @param array $options @@ -469,8 +463,7 @@ public static function all($options = array()) */ public function create($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/plans"; $data = array( @@ -485,48 +478,45 @@ public function create($options = array()) "cancel_url" => $this->getCancelUrl() ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field plan $body = $response->getBody(); - $body = $body['plan']; - - $returnValues["create"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['plan']; + $returnValues['create'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Find a plan by its ID. * @param string $planId * @param array $options * @return $this */ - public static function find($planId, $options = array()) + public function find($planId, $options = array()) { - $cur = new Plan(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/plans/" . urlencode($planId) . ""; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field plan $body = $response->getBody(); - $body = $body['plan']; - - $returnValues["find"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['plan']; + $returnValues['find'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Update the plan. This action won't affect subscriptions already linked to this plan. * @param array $options @@ -534,8 +524,7 @@ public static function find($planId, $options = array()) */ public function update($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/plans/" . urlencode($this->getId()) . ""; $data = array( @@ -546,19 +535,18 @@ public function update($options = array()) "cancel_url" => $this->getCancelUrl() ); - $response = new Response($request->put($path, $data, $options)); + $response = $request->put($path, $data, $options); $returnValues = array(); // Handling for field plan $body = $response->getBody(); - $body = $body['plan']; - - $returnValues["update"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['plan']; + $returnValues['update'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Delete a plan. Subscriptions linked to this plan won't be affected. * @param array $options @@ -566,22 +554,19 @@ public function update($options = array()) */ public function end($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/plans/" . urlencode($this->getId()) . ""; $data = array( ); - $response = new Response($request->delete($path, $data, $options)); + $response = $request->delete($path, $data, $options); $returnValues = array(); + $returnValues['success'] = $response->isSuccess(); - $returnValues["success"] = $response->isSuccess(); return array_values($returnValues)[0]; - } - } diff --git a/src/ProcessOut.php b/src/ProcessOut.php index a461133..d5a9811 100644 --- a/src/ProcessOut.php +++ b/src/ProcessOut.php @@ -2,7 +2,6 @@ namespace ProcessOut; -use anlutro\cURL\cURL; use ProcessOut\Exceptions\ApiAuthenticationException; class ProcessOut @@ -13,17 +12,11 @@ class ProcessOut */ protected $host = 'https://api.processout.com'; - /** - * Contains cURL instance - * @var anlutro\cURL\cURL - */ - protected $cURL; - /** * ProcessOut project ID * @var string */ - protected $projectId; + protected $projectID; /** * ProcessOut project secret key @@ -31,23 +24,15 @@ class ProcessOut */ protected $projectSecret; - /** - * Default ProcessOut's instance - * @var ProcessOut - */ - protected static $default = null; - /** * ProcessOut constructor + * @param string $projectID + * @param string $projectSecret */ - public function __construct($projectId, $projectSecret) + public function __construct($projectID, $projectSecret) { - $this->cURL = new cURL; - $this->projectId = $projectId; + $this->projectID = $projectID; $this->projectSecret = $projectSecret; - - if (self::$default == null) - self::setDefault($this); } /** @@ -70,77 +55,193 @@ public function setHost($host) } /** - * Set projectId - * @return $this + * Get the ProcessOut project ID + * @return string */ - public function setProjectId($value) + public function getProjectID() { - $this->projectId = $value; - - return $this; + return $this->projectID; } /** - * Get projectId + * Get the ProcessOut project secret * @return string */ - public function getProjectId() + public function getProjectSecret() { - return $this->projectId; + return $this->projectSecret; } + /** - * Set projectSecret - * @return $this + * Create a new Activity instance + * @param array|null $prefill array used to prefill the object + * @return Activity */ - public function setProjectSecret($value) - { - $this->projectSecret = $value; - - return $this; + public function newActivity($prefill = array()) { + return new Activity($this, $prefill); } - + /** - * Get projectSecret - * @return string + * Create a new AuthorizationRequest instance + * @param array|null $prefill array used to prefill the object + * @return AuthorizationRequest */ - public function getProjectSecret() - { - return $this->projectSecret; + public function newAuthorizationRequest($prefill = array()) { + return new AuthorizationRequest($this, $prefill); } - + /** - * Get the cURL instance - * @return anlutro\cURL\cURL + * Create a new Card instance + * @param array|null $prefill array used to prefill the object + * @return Card */ - public function getCurl() - { - return $this->cURL; + public function newCard($prefill = array()) { + return new Card($this, $prefill); } - + /** - * Set the default ProcessOut's instance - * @param ProcessOut $processOut - * @return ProcessOut + * Create a new Coupon instance + * @param array|null $prefill array used to prefill the object + * @return Coupon */ - public static function setDefault(ProcessOut $processOut) - { - self::$default = $processOut; - - return $processOut; + public function newCoupon($prefill = array()) { + return new Coupon($this, $prefill); } - + /** - * Get the default ProcessOut's instance - * @return ProcessOut + * Create a new Customer instance + * @param array|null $prefill array used to prefill the object + * @return Customer */ - public static function getDefault() - { - if(! (self::$default instanceof ProcessOut)) - throw new ApiAuthenticationException( - 'You need to define a default ProcessOut object (ProcessOut::setDefault()).'); - - return self::$default; + public function newCustomer($prefill = array()) { + return new Customer($this, $prefill); } - -} \ No newline at end of file + + /** + * Create a new Token instance + * @param array|null $prefill array used to prefill the object + * @return Token + */ + public function newToken($prefill = array()) { + return new Token($this, $prefill); + } + + /** + * Create a new Discount instance + * @param array|null $prefill array used to prefill the object + * @return Discount + */ + public function newDiscount($prefill = array()) { + return new Discount($this, $prefill); + } + + /** + * Create a new Event instance + * @param array|null $prefill array used to prefill the object + * @return Event + */ + public function newEvent($prefill = array()) { + return new Event($this, $prefill); + } + + /** + * Create a new Gateway instance + * @param array|null $prefill array used to prefill the object + * @return Gateway + */ + public function newGateway($prefill = array()) { + return new Gateway($this, $prefill); + } + + /** + * Create a new GatewayConfiguration instance + * @param array|null $prefill array used to prefill the object + * @return GatewayConfiguration + */ + public function newGatewayConfiguration($prefill = array()) { + return new GatewayConfiguration($this, $prefill); + } + + /** + * Create a new Invoice instance + * @param array|null $prefill array used to prefill the object + * @return Invoice + */ + public function newInvoice($prefill = array()) { + return new Invoice($this, $prefill); + } + + /** + * Create a new CustomerAction instance + * @param array|null $prefill array used to prefill the object + * @return CustomerAction + */ + public function newCustomerAction($prefill = array()) { + return new CustomerAction($this, $prefill); + } + + /** + * Create a new Plan instance + * @param array|null $prefill array used to prefill the object + * @return Plan + */ + public function newPlan($prefill = array()) { + return new Plan($this, $prefill); + } + + /** + * Create a new Product instance + * @param array|null $prefill array used to prefill the object + * @return Product + */ + public function newProduct($prefill = array()) { + return new Product($this, $prefill); + } + + /** + * Create a new Project instance + * @param array|null $prefill array used to prefill the object + * @return Project + */ + public function newProject($prefill = array()) { + return new Project($this, $prefill); + } + + /** + * Create a new Refund instance + * @param array|null $prefill array used to prefill the object + * @return Refund + */ + public function newRefund($prefill = array()) { + return new Refund($this, $prefill); + } + + /** + * Create a new Subscription instance + * @param array|null $prefill array used to prefill the object + * @return Subscription + */ + public function newSubscription($prefill = array()) { + return new Subscription($this, $prefill); + } + + /** + * Create a new Transaction instance + * @param array|null $prefill array used to prefill the object + * @return Transaction + */ + public function newTransaction($prefill = array()) { + return new Transaction($this, $prefill); + } + + /** + * Create a new Webhook instance + * @param array|null $prefill array used to prefill the object + * @return Webhook + */ + public function newWebhook($prefill = array()) { + return new Webhook($this, $prefill); + } + +} diff --git a/src/Product.php b/src/Product.php index bda469e..c1c2b0d 100755 --- a/src/Product.php +++ b/src/Product.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class Product { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the product @@ -98,21 +96,19 @@ class Product /** * Product constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; $this->setMetadata(array('_library' => 'php')); $this->setRequestEmail((bool) false); $this->setRequestShipping((bool) false); + + $this->fillWithData($prefill); } @@ -160,7 +156,7 @@ public function setProject($value) $this->project = $value; else { - $obj = new Project($this->instance); + $obj = new Project($this->client); $obj->fillWithData($value); $this->project = $obj; } @@ -417,93 +413,92 @@ public function setCreatedAt($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["project"])) - $this->setProject($data["project"]); + if(! empty($data['project'])) + $this->setProject($data['project']); - if(! empty($data["url"])) - $this->setUrl($data["url"]); + if(! empty($data['url'])) + $this->setUrl($data['url']); - if(! empty($data["name"])) - $this->setName($data["name"]); + if(! empty($data['name'])) + $this->setName($data['name']); - if(! empty($data["amount"])) - $this->setAmount($data["amount"]); + if(! empty($data['amount'])) + $this->setAmount($data['amount']); - if(! empty($data["currency"])) - $this->setCurrency($data["currency"]); + if(! empty($data['currency'])) + $this->setCurrency($data['currency']); - if(! empty($data["metadata"])) - $this->setMetadata($data["metadata"]); + if(! empty($data['metadata'])) + $this->setMetadata($data['metadata']); - if(! empty($data["request_email"])) - $this->setRequestEmail($data["request_email"]); + if(! empty($data['request_email'])) + $this->setRequestEmail($data['request_email']); - if(! empty($data["request_shipping"])) - $this->setRequestShipping($data["request_shipping"]); + if(! empty($data['request_shipping'])) + $this->setRequestShipping($data['request_shipping']); - if(! empty($data["return_url"])) - $this->setReturnUrl($data["return_url"]); + if(! empty($data['return_url'])) + $this->setReturnUrl($data['return_url']); - if(! empty($data["cancel_url"])) - $this->setCancelUrl($data["cancel_url"]); + if(! empty($data['cancel_url'])) + $this->setCancelUrl($data['cancel_url']); - if(! empty($data["sandbox"])) - $this->setSandbox($data["sandbox"]); + if(! empty($data['sandbox'])) + $this->setSandbox($data['sandbox']); - if(! empty($data["created_at"])) - $this->setCreatedAt($data["created_at"]); + if(! empty($data['created_at'])) + $this->setCreatedAt($data['created_at']); return $this; } + /** * Create a new invoice from the product. * @param array $options * @return Invoice */ - public function invoice($options = array()) + public function createInvoice($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/products/" . urlencode($this->getId()) . "/invoices"; $data = array( ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field invoice $body = $response->getBody(); $body = $body['invoice']; - $invoice = new Invoice($cur->instance); - $returnValues["invoice"] = $invoice->fillWithData($body); + $invoice = new Invoice($this->client); + $returnValues['invoice'] = $invoice->fillWithData($body); - return array_values($returnValues)[0]; + return array_values($returnValues)[0]; } - + /** * Get all the products. * @param array $options * @return array */ - public static function all($options = array()) + public function all($options = array()) { - $cur = new Product(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/products"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -512,17 +507,15 @@ public static function all($options = array()) $body = $response->getBody(); foreach($body['products'] as $v) { - $tmp = new Product($cur->instance); + $tmp = new Product($this->client); $tmp->fillWithData($v); $a[] = $tmp; } - - $returnValues["Products"] = $a; - - return array_values($returnValues)[0]; + $returnValues['Products'] = $a; + return array_values($returnValues)[0]; } - + /** * Create a new product. * @param array $options @@ -530,8 +523,7 @@ public static function all($options = array()) */ public function create($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/products"; $data = array( @@ -545,48 +537,45 @@ public function create($options = array()) "cancel_url" => $this->getCancelUrl() ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field product $body = $response->getBody(); - $body = $body['product']; - - $returnValues["create"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['product']; + $returnValues['create'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Find a product by its ID. * @param string $productId * @param array $options * @return $this */ - public static function find($productId, $options = array()) + public function find($productId, $options = array()) { - $cur = new Product(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/products/" . urlencode($productId) . ""; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field product $body = $response->getBody(); - $body = $body['product']; - - $returnValues["find"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['product']; + $returnValues['find'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Save the updated product attributes. * @param array $options @@ -594,8 +583,7 @@ public static function find($productId, $options = array()) */ public function save($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/products/" . urlencode($this->getId()) . ""; $data = array( @@ -609,19 +597,18 @@ public function save($options = array()) "cancel_url" => $this->getCancelUrl() ); - $response = new Response($request->put($path, $data, $options)); + $response = $request->put($path, $data, $options); $returnValues = array(); // Handling for field product $body = $response->getBody(); - $body = $body['product']; - - $returnValues["save"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['product']; + $returnValues['save'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Delete the product. * @param array $options @@ -629,22 +616,19 @@ public function save($options = array()) */ public function delete($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/products/" . urlencode($this->getId()) . ""; $data = array( ); - $response = new Response($request->delete($path, $data, $options)); + $response = $request->delete($path, $data, $options); $returnValues = array(); + $returnValues['success'] = $response->isSuccess(); - $returnValues["success"] = $response->isSuccess(); return array_values($returnValues)[0]; - } - } diff --git a/src/Project.php b/src/Project.php index 9153106..1f445a6 100755 --- a/src/Project.php +++ b/src/Project.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class Project { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the project @@ -50,18 +48,16 @@ class Project /** * Project constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; + + $this->fillWithData($prefill); } @@ -183,24 +179,25 @@ public function setCreatedAt($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["name"])) - $this->setName($data["name"]); + if(! empty($data['name'])) + $this->setName($data['name']); - if(! empty($data["logo_url"])) - $this->setLogoUrl($data["logo_url"]); + if(! empty($data['logo_url'])) + $this->setLogoUrl($data['logo_url']); - if(! empty($data["email"])) - $this->setEmail($data["email"]); + if(! empty($data['email'])) + $this->setEmail($data['email']); - if(! empty($data["created_at"])) - $this->setCreatedAt($data["created_at"]); + if(! empty($data['created_at'])) + $this->setCreatedAt($data['created_at']); return $this; } + /** * Get all the gateway configurations of the project * @param array $options @@ -208,15 +205,14 @@ public function fillWithData($data) */ public function gatewayConfigurations($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/projects/" . urlencode($this->getId()) . "/gateway-configurations"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -225,16 +221,13 @@ public function gatewayConfigurations($options = array()) $body = $response->getBody(); foreach($body['gateway_configurations'] as $v) { - $tmp = new GatewayConfiguration($cur->instance); + $tmp = new GatewayConfiguration($this->client); $tmp->fillWithData($v); $a[] = $tmp; } - - $returnValues["GatewayConfigurations"] = $a; - - return array_values($returnValues)[0]; + $returnValues['GatewayConfigurations'] = $a; + return array_values($returnValues)[0]; } - } diff --git a/src/Refund.php b/src/Refund.php index 775aced..99f3c78 100755 --- a/src/Refund.php +++ b/src/Refund.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class Refund { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the refund @@ -68,19 +66,17 @@ class Refund /** * Refund constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; $this->setMetadata(array('_library' => 'php')); + + $this->fillWithData($prefill); } @@ -128,7 +124,7 @@ public function setTransaction($value) $this->transaction = $value; else { - $obj = new Transaction($this->instance); + $obj = new Transaction($this->client); $obj->fillWithData($value); $this->transaction = $obj; } @@ -275,33 +271,34 @@ public function setCreatedAt($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["transaction"])) - $this->setTransaction($data["transaction"]); + if(! empty($data['transaction'])) + $this->setTransaction($data['transaction']); - if(! empty($data["reason"])) - $this->setReason($data["reason"]); + if(! empty($data['reason'])) + $this->setReason($data['reason']); - if(! empty($data["information"])) - $this->setInformation($data["information"]); + if(! empty($data['information'])) + $this->setInformation($data['information']); - if(! empty($data["amount"])) - $this->setAmount($data["amount"]); + if(! empty($data['amount'])) + $this->setAmount($data['amount']); - if(! empty($data["metadata"])) - $this->setMetadata($data["metadata"]); + if(! empty($data['metadata'])) + $this->setMetadata($data['metadata']); - if(! empty($data["sandbox"])) - $this->setSandbox($data["sandbox"]); + if(! empty($data['sandbox'])) + $this->setSandbox($data['sandbox']); - if(! empty($data["created_at"])) - $this->setCreatedAt($data["created_at"]); + if(! empty($data['created_at'])) + $this->setCreatedAt($data['created_at']); return $this; } + /** * Find a transaction's refund by its ID. * @param string $transactionId @@ -309,29 +306,27 @@ public function fillWithData($data) * @param array $options * @return $this */ - public static function find($transactionId, $refundId, $options = array()) + public function find($transactionId, $refundId, $options = array()) { - $cur = new Refund(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/transactions/" . urlencode($transactionId) . "/refunds/" . urlencode($refundId) . ""; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field refund $body = $response->getBody(); - $body = $body['refund']; - - $returnValues["find"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['refund']; + $returnValues['find'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Apply a refund to a transaction. * @param string $transactionId @@ -340,8 +335,7 @@ public static function find($transactionId, $refundId, $options = array()) */ public function apply($transactionId, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/transactions/" . urlencode($transactionId) . "/refunds"; $data = array( @@ -351,14 +345,12 @@ public function apply($transactionId, $options = array()) "information" => $this->getInformation() ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); + $returnValues['success'] = $response->isSuccess(); - $returnValues["success"] = $response->isSuccess(); return array_values($returnValues)[0]; - } - } diff --git a/src/Subscription.php b/src/Subscription.php index 34044d5..1ac3715 100755 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class Subscription { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the subscription @@ -164,19 +162,17 @@ class Subscription /** * Subscription constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; $this->setMetadata(array('_library' => 'php')); + + $this->fillWithData($prefill); } @@ -224,7 +220,7 @@ public function setProject($value) $this->project = $value; else { - $obj = new Project($this->instance); + $obj = new Project($this->client); $obj->fillWithData($value); $this->project = $obj; } @@ -253,7 +249,7 @@ public function setPlan($value) $this->plan = $value; else { - $obj = new Plan($this->instance); + $obj = new Plan($this->client); $obj->fillWithData($value); $this->plan = $obj; } @@ -282,7 +278,7 @@ public function setCustomer($value) $this->customer = $value; else { - $obj = new Customer($this->instance); + $obj = new Customer($this->client); $obj->fillWithData($value); $this->customer = $obj; } @@ -311,7 +307,7 @@ public function setToken($value) $this->token = $value; else { - $obj = new Token($this->instance); + $obj = new Token($this->client); $obj->fillWithData($value); $this->token = $obj; } @@ -744,126 +740,125 @@ public function setIterateAt($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["project"])) - $this->setProject($data["project"]); + if(! empty($data['project'])) + $this->setProject($data['project']); - if(! empty($data["plan"])) - $this->setPlan($data["plan"]); + if(! empty($data['plan'])) + $this->setPlan($data['plan']); - if(! empty($data["customer"])) - $this->setCustomer($data["customer"]); + if(! empty($data['customer'])) + $this->setCustomer($data['customer']); - if(! empty($data["token"])) - $this->setToken($data["token"]); + if(! empty($data['token'])) + $this->setToken($data['token']); - if(! empty($data["url"])) - $this->setUrl($data["url"]); + if(! empty($data['url'])) + $this->setUrl($data['url']); - if(! empty($data["name"])) - $this->setName($data["name"]); + if(! empty($data['name'])) + $this->setName($data['name']); - if(! empty($data["amount"])) - $this->setAmount($data["amount"]); + if(! empty($data['amount'])) + $this->setAmount($data['amount']); - if(! empty($data["currency"])) - $this->setCurrency($data["currency"]); + if(! empty($data['currency'])) + $this->setCurrency($data['currency']); - if(! empty($data["metadata"])) - $this->setMetadata($data["metadata"]); + if(! empty($data['metadata'])) + $this->setMetadata($data['metadata']); - if(! empty($data["interval"])) - $this->setInterval($data["interval"]); + if(! empty($data['interval'])) + $this->setInterval($data['interval']); - if(! empty($data["trial_end_at"])) - $this->setTrialEndAt($data["trial_end_at"]); + if(! empty($data['trial_end_at'])) + $this->setTrialEndAt($data['trial_end_at']); - if(! empty($data["activated"])) - $this->setActivated($data["activated"]); + if(! empty($data['activated'])) + $this->setActivated($data['activated']); - if(! empty($data["active"])) - $this->setActive($data["active"]); + if(! empty($data['active'])) + $this->setActive($data['active']); - if(! empty($data["canceled"])) - $this->setCanceled($data["canceled"]); + if(! empty($data['canceled'])) + $this->setCanceled($data['canceled']); - if(! empty($data["cancellation_reason"])) - $this->setCancellationReason($data["cancellation_reason"]); + if(! empty($data['cancellation_reason'])) + $this->setCancellationReason($data['cancellation_reason']); - if(! empty($data["pending_cancellation"])) - $this->setPendingCancellation($data["pending_cancellation"]); + if(! empty($data['pending_cancellation'])) + $this->setPendingCancellation($data['pending_cancellation']); - if(! empty($data["cancel_at"])) - $this->setCancelAt($data["cancel_at"]); + if(! empty($data['cancel_at'])) + $this->setCancelAt($data['cancel_at']); - if(! empty($data["return_url"])) - $this->setReturnUrl($data["return_url"]); + if(! empty($data['return_url'])) + $this->setReturnUrl($data['return_url']); - if(! empty($data["cancel_url"])) - $this->setCancelUrl($data["cancel_url"]); + if(! empty($data['cancel_url'])) + $this->setCancelUrl($data['cancel_url']); - if(! empty($data["sandbox"])) - $this->setSandbox($data["sandbox"]); + if(! empty($data['sandbox'])) + $this->setSandbox($data['sandbox']); - if(! empty($data["created_at"])) - $this->setCreatedAt($data["created_at"]); + if(! empty($data['created_at'])) + $this->setCreatedAt($data['created_at']); - if(! empty($data["activated_at"])) - $this->setActivatedAt($data["activated_at"]); + if(! empty($data['activated_at'])) + $this->setActivatedAt($data['activated_at']); - if(! empty($data["iterate_at"])) - $this->setIterateAt($data["iterate_at"]); + if(! empty($data['iterate_at'])) + $this->setIterateAt($data['iterate_at']); return $this; } + /** * Get the customer owning the subscription. * @param array $options * @return Customer */ - public function customer($options = array()) + public function fetchCustomer($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/subscriptions/" . urlencode($this->getId()) . "/customers"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field customer $body = $response->getBody(); $body = $body['customer']; - $customer = new Customer($cur->instance); - $returnValues["customer"] = $customer->fillWithData($body); + $customer = new Customer($this->client); + $returnValues['customer'] = $customer->fillWithData($body); - return array_values($returnValues)[0]; + return array_values($returnValues)[0]; } - + /** * Get the discounts applied to the subscription. * @param array $options * @return array */ - public function discounts($options = array()) + public function fetchDiscounts($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/subscriptions/" . urlencode($this->getId()) . "/discounts"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -872,33 +867,86 @@ public function discounts($options = array()) $body = $response->getBody(); foreach($body['discounts'] as $v) { - $tmp = new Discount($cur->instance); + $tmp = new Discount($this->client); $tmp->fillWithData($v); $a[] = $tmp; } + $returnValues['Discounts'] = $a; + + return array_values($returnValues)[0]; + } + + /** + * Find a subscription's discount by its ID. + * @param string $discountId + * @param array $options + * @return Discount + */ + public function findDiscount($discountId, $options = array()) + { + $request = new Request($this->client); + $path = "/subscriptions/" . urlencode($this->getId()) . "/discounts/" . urlencode($discountId) . ""; + + $data = array( + + ); + + $response = $request->get($path, $data, $options); + $returnValues = array(); - $returnValues["Discounts"] = $a; + + // Handling for field discount + $body = $response->getBody(); + $body = $body['discount']; + $discount = new Discount($this->client); + $returnValues['discount'] = $discount->fillWithData($body); - return array_values($returnValues)[0]; + return array_values($returnValues)[0]; } + + /** + * Remove a discount applied to a subscription. + * @param string $discountId + * @param array $options + * @return $this + */ + public function removeDiscount($discountId, $options = array()) + { + $request = new Request($this->client); + $path = "/subscriptions/" . urlencode($this->getId()) . "/discounts/" . urlencode($discountId) . ""; + $data = array( + + ); + + $response = $request->delete($path, $data, $options); + $returnValues = array(); + + + // Handling for field discount + $body = $response->getBody(); + $body = $body['discount']; + $returnValues['removeDiscount'] = $this->fillWithData($body); + + return array_values($returnValues)[0]; + } + /** * Get the subscriptions past transactions. * @param array $options * @return array */ - public function transactions($options = array()) + public function fetchTransactions($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/subscriptions/" . urlencode($this->getId()) . "/transactions"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -907,33 +955,30 @@ public function transactions($options = array()) $body = $response->getBody(); foreach($body['transactions'] as $v) { - $tmp = new Transaction($cur->instance); + $tmp = new Transaction($this->client); $tmp->fillWithData($v); $a[] = $tmp; } - - $returnValues["Transactions"] = $a; - - return array_values($returnValues)[0]; + $returnValues['Transactions'] = $a; + return array_values($returnValues)[0]; } - + /** * Get all the subscriptions. * @param array $options * @return array */ - public static function all($options = array()) + public function all($options = array()) { - $cur = new Subscription(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/subscriptions"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -942,17 +987,15 @@ public static function all($options = array()) $body = $response->getBody(); foreach($body['subscriptions'] as $v) { - $tmp = new Subscription($cur->instance); + $tmp = new Subscription($this->client); $tmp->fillWithData($v); $a[] = $tmp; } - - $returnValues["Subscriptions"] = $a; - - return array_values($returnValues)[0]; + $returnValues['Subscriptions'] = $a; + return array_values($returnValues)[0]; } - + /** * Create a new subscription for the given customer. * @param string $customerId @@ -961,8 +1004,7 @@ public static function all($options = array()) */ public function create($customerId, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/subscriptions"; $data = array( @@ -978,19 +1020,18 @@ public function create($customerId, $options = array()) "customer_id" => $customerId ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field subscription $body = $response->getBody(); - $body = $body['subscription']; - - $returnValues["create"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['subscription']; + $returnValues['create'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Create a new subscription for the customer from the given plan ID. * @param string $customerId @@ -1000,8 +1041,7 @@ public function create($customerId, $options = array()) */ public function createFromPlan($customerId, $planId, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/subscriptions"; $data = array( @@ -1018,76 +1058,73 @@ public function createFromPlan($customerId, $planId, $options = array()) "plan_id" => $planId ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field subscription $body = $response->getBody(); - $body = $body['subscription']; - - $returnValues["createFromPlan"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['subscription']; + $returnValues['createFromPlan'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Find a subscription by its ID. * @param string $subscriptionId * @param array $options * @return $this */ - public static function find($subscriptionId, $options = array()) + public function find($subscriptionId, $options = array()) { - $cur = new Subscription(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/subscriptions/" . urlencode($subscriptionId) . ""; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field subscription $body = $response->getBody(); - $body = $body['subscription']; - - $returnValues["find"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['subscription']; + $returnValues['find'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Update the subscription. + * @param string $prorate * @param array $options * @return $this */ - public function update($options = array()) + public function update($prorate, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/subscriptions/" . urlencode($this->getId()) . ""; $data = array( - "trial_end_at" => $this->getTrialEndAt() + "trial_end_at" => $this->getTrialEndAt(), + "prorate" => $prorate ); - $response = new Response($request->put($path, $data, $options)); + $response = $request->put($path, $data, $options); $returnValues = array(); // Handling for field subscription $body = $response->getBody(); - $body = $body['subscription']; - - $returnValues["update"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['subscription']; + $returnValues['update'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Update the subscription's plan. * @param string $planId @@ -1097,8 +1134,7 @@ public function update($options = array()) */ public function updatePlan($planId, $prorate, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/subscriptions/" . urlencode($this->getId()) . ""; $data = array( @@ -1106,19 +1142,18 @@ public function updatePlan($planId, $prorate, $options = array()) "prorate" => $prorate ); - $response = new Response($request->put($path, $data, $options)); + $response = $request->put($path, $data, $options); $returnValues = array(); // Handling for field subscription $body = $response->getBody(); - $body = $body['subscription']; - - $returnValues["updatePlan"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['subscription']; + $returnValues['updatePlan'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Apply a source to the subscription to activate or update the subscription's source. * @param string $source @@ -1127,27 +1162,25 @@ public function updatePlan($planId, $prorate, $options = array()) */ public function applySource($source, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/subscriptions/" . urlencode($this->getId()) . ""; $data = array( "source" => $source ); - $response = new Response($request->put($path, $data, $options)); + $response = $request->put($path, $data, $options); $returnValues = array(); // Handling for field subscription $body = $response->getBody(); - $body = $body['subscription']; - - $returnValues["applySource"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['subscription']; + $returnValues['applySource'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Cancel a subscription. The reason may be provided as well. * @param string $cancellationReason @@ -1156,27 +1189,25 @@ public function applySource($source, $options = array()) */ public function cancel($cancellationReason, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/subscriptions/" . urlencode($this->getId()) . ""; $data = array( "cancellation_reason" => $cancellationReason ); - $response = new Response($request->delete($path, $data, $options)); + $response = $request->delete($path, $data, $options); $returnValues = array(); // Handling for field subscription $body = $response->getBody(); - $body = $body['subscription']; - - $returnValues["cancel"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['subscription']; + $returnValues['cancel'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Schedule the cancellation of the subscription. The reason may be provided as well. * @param string $cancelAt @@ -1184,10 +1215,9 @@ public function cancel($cancellationReason, $options = array()) * @param array $options * @return $this */ - public function cancelAt($cancelAt, $cancellationReason, $options = array()) + public function cancelAtDate($cancelAt, $cancellationReason, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/subscriptions/" . urlencode($this->getId()) . ""; $data = array( @@ -1195,18 +1225,16 @@ public function cancelAt($cancelAt, $cancellationReason, $options = array()) "cancellation_reason" => $cancellationReason ); - $response = new Response($request->delete($path, $data, $options)); + $response = $request->delete($path, $data, $options); $returnValues = array(); // Handling for field subscription $body = $response->getBody(); - $body = $body['subscription']; - - $returnValues["cancelAt"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['subscription']; + $returnValues['cancelAtDate'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - } diff --git a/src/Token.php b/src/Token.php index 133fcfe..0c5728e 100755 --- a/src/Token.php +++ b/src/Token.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class Token { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the customer token @@ -30,6 +28,12 @@ class Token */ protected $customer; + /** + * Card used to create this token, if any + * @var object + */ + protected $card; + /** * Metadata related to the token, in the form of a dictionary (key-value pair) * @var dictionary @@ -50,19 +54,17 @@ class Token /** * Token constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; $this->setMetadata(array('_library' => 'php')); + + $this->fillWithData($prefill); } @@ -110,13 +112,42 @@ public function setCustomer($value) $this->customer = $value; else { - $obj = new Customer($this->instance); + $obj = new Customer($this->client); $obj->fillWithData($value); $this->customer = $obj; } return $this; } + /** + * Get Card + * Card used to create this token, if any + * @return object + */ + public function getCard() + { + return $this->card; + } + + /** + * Set Card + * Card used to create this token, if any + * @param object $value + * @return $this + */ + public function setCard($value) + { + if (is_object($value)) + $this->card = $value; + else + { + $obj = new Card($this->client); + $obj->fillWithData($value); + $this->card = $obj; + } + return $this; + } + /** * Get Metadata * Metadata related to the token, in the form of a dictionary (key-value pair) @@ -191,24 +222,28 @@ public function setCreatedAt($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); + + if(! empty($data['customer'])) + $this->setCustomer($data['customer']); - if(! empty($data["customer"])) - $this->setCustomer($data["customer"]); + if(! empty($data['card'])) + $this->setCard($data['card']); - if(! empty($data["metadata"])) - $this->setMetadata($data["metadata"]); + if(! empty($data['metadata'])) + $this->setMetadata($data['metadata']); - if(! empty($data["is_subscription_only"])) - $this->setIsSubscriptionOnly($data["is_subscription_only"]); + if(! empty($data['is_subscription_only'])) + $this->setIsSubscriptionOnly($data['is_subscription_only']); - if(! empty($data["created_at"])) - $this->setCreatedAt($data["created_at"]); + if(! empty($data['created_at'])) + $this->setCreatedAt($data['created_at']); return $this; } + /** * Find a customer's token by its ID. * @param string $customerId @@ -216,29 +251,27 @@ public function fillWithData($data) * @param array $options * @return $this */ - public static function find($customerId, $tokenId, $options = array()) + public function find($customerId, $tokenId, $options = array()) { - $cur = new Token(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/customers/" . urlencode($customerId) . "/tokens/" . urlencode($tokenId) . ""; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field token $body = $response->getBody(); - $body = $body['token']; - - $returnValues["find"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['token']; + $returnValues['find'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Create a new token for the given customer ID. * @param string $customerId @@ -248,8 +281,7 @@ public static function find($customerId, $tokenId, $options = array()) */ public function create($customerId, $source, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/customers/" . urlencode($customerId) . "/tokens"; $data = array( @@ -257,19 +289,18 @@ public function create($customerId, $source, $options = array()) "source" => $source ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field token $body = $response->getBody(); - $body = $body['token']; - - $returnValues["create"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['token']; + $returnValues['create'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - + /** * Create a new token for the given customer ID from an authorization request * @param string $customerId @@ -280,8 +311,7 @@ public function create($customerId, $source, $options = array()) */ public function createFromRequest($customerId, $source, $target, $options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/customers/" . urlencode($customerId) . "/tokens"; $data = array( @@ -290,18 +320,16 @@ public function createFromRequest($customerId, $source, $target, $options = arra "target" => $target ); - $response = new Response($request->post($path, $data, $options)); + $response = $request->post($path, $data, $options); $returnValues = array(); // Handling for field token $body = $response->getBody(); - $body = $body['token']; - - $returnValues["createFromRequest"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['token']; + $returnValues['createFromRequest'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - } diff --git a/src/Transaction.php b/src/Transaction.php index ab412f0..0939195 100755 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class Transaction { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the transaction @@ -31,25 +29,25 @@ class Transaction protected $project; /** - * Subscription to which this transaction belongs + * Customer that was linked to this transaction * @var object */ - protected $subscription; + protected $customer; /** - * Customer that was linked to this transaction + * Subscription to which this transaction belongs * @var object */ - protected $customer; + protected $subscription; /** - * Token that was used to capture the payment of this transaction + * Token that was used to capture the payment of this transaction, if any * @var object */ protected $token; /** - * Card that was used to capture the payment of this transaction + * Card that was used to capture the payment of this transaction, if any * @var object */ protected $card; @@ -122,19 +120,17 @@ class Transaction /** * Transaction constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; $this->setMetadata(array('_library' => 'php')); + + $this->fillWithData($prefill); } @@ -182,7 +178,7 @@ public function setProject($value) $this->project = $value; else { - $obj = new Project($this->instance); + $obj = new Project($this->client); $obj->fillWithData($value); $this->project = $obj; } @@ -190,66 +186,66 @@ public function setProject($value) } /** - * Get Subscription - * Subscription to which this transaction belongs + * Get Customer + * Customer that was linked to this transaction * @return object */ - public function getSubscription() + public function getCustomer() { - return $this->subscription; + return $this->customer; } /** - * Set Subscription - * Subscription to which this transaction belongs + * Set Customer + * Customer that was linked to this transaction * @param object $value * @return $this */ - public function setSubscription($value) + public function setCustomer($value) { if (is_object($value)) - $this->subscription = $value; + $this->customer = $value; else { - $obj = new Subscription($this->instance); + $obj = new Customer($this->client); $obj->fillWithData($value); - $this->subscription = $obj; + $this->customer = $obj; } return $this; } /** - * Get Customer - * Customer that was linked to this transaction + * Get Subscription + * Subscription to which this transaction belongs * @return object */ - public function getCustomer() + public function getSubscription() { - return $this->customer; + return $this->subscription; } /** - * Set Customer - * Customer that was linked to this transaction + * Set Subscription + * Subscription to which this transaction belongs * @param object $value * @return $this */ - public function setCustomer($value) + public function setSubscription($value) { if (is_object($value)) - $this->customer = $value; + $this->subscription = $value; else { - $obj = new Customer($this->instance); + $obj = new Subscription($this->client); $obj->fillWithData($value); - $this->customer = $obj; + $this->subscription = $obj; } return $this; } /** * Get Token - * Token that was used to capture the payment of this transaction + * Token that was used to capture the payment of this transaction, if any * @return object */ public function getToken() @@ -259,7 +255,7 @@ public function getToken() /** * Set Token - * Token that was used to capture the payment of this transaction + * Token that was used to capture the payment of this transaction, if any * @param object $value * @return $this */ @@ -269,7 +265,7 @@ public function setToken($value) $this->token = $value; else { - $obj = new Token($this->instance); + $obj = new Token($this->client); $obj->fillWithData($value); $this->token = $obj; } @@ -278,7 +274,7 @@ public function setToken($value) /** * Get Card - * Card that was used to capture the payment of this transaction + * Card that was used to capture the payment of this transaction, if any * @return object */ public function getCard() @@ -288,7 +284,7 @@ public function getCard() /** * Set Card - * Card that was used to capture the payment of this transaction + * Card that was used to capture the payment of this transaction, if any * @param object $value * @return $this */ @@ -298,7 +294,7 @@ public function setCard($value) $this->card = $value; else { - $obj = new Card($this->instance); + $obj = new Card($this->client); $obj->fillWithData($value); $this->card = $obj; } @@ -555,76 +551,76 @@ public function setCreatedAt($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["project"])) - $this->setProject($data["project"]); + if(! empty($data['project'])) + $this->setProject($data['project']); - if(! empty($data["subscription"])) - $this->setSubscription($data["subscription"]); + if(! empty($data['customer'])) + $this->setCustomer($data['customer']); - if(! empty($data["customer"])) - $this->setCustomer($data["customer"]); + if(! empty($data['subscription'])) + $this->setSubscription($data['subscription']); - if(! empty($data["token"])) - $this->setToken($data["token"]); + if(! empty($data['token'])) + $this->setToken($data['token']); - if(! empty($data["card"])) - $this->setCard($data["card"]); + if(! empty($data['card'])) + $this->setCard($data['card']); - if(! empty($data["name"])) - $this->setName($data["name"]); + if(! empty($data['name'])) + $this->setName($data['name']); - if(! empty($data["authorized_amount"])) - $this->setAuthorizedAmount($data["authorized_amount"]); + if(! empty($data['authorized_amount'])) + $this->setAuthorizedAmount($data['authorized_amount']); - if(! empty($data["captured_amount"])) - $this->setCapturedAmount($data["captured_amount"]); + if(! empty($data['captured_amount'])) + $this->setCapturedAmount($data['captured_amount']); - if(! empty($data["currency"])) - $this->setCurrency($data["currency"]); + if(! empty($data['currency'])) + $this->setCurrency($data['currency']); - if(! empty($data["status"])) - $this->setStatus($data["status"]); + if(! empty($data['status'])) + $this->setStatus($data['status']); - if(! empty($data["authorized"])) - $this->setAuthorized($data["authorized"]); + if(! empty($data['authorized'])) + $this->setAuthorized($data['authorized']); - if(! empty($data["captured"])) - $this->setCaptured($data["captured"]); + if(! empty($data['captured'])) + $this->setCaptured($data['captured']); - if(! empty($data["processout_fee"])) - $this->setProcessoutFee($data["processout_fee"]); + if(! empty($data['processout_fee'])) + $this->setProcessoutFee($data['processout_fee']); - if(! empty($data["metadata"])) - $this->setMetadata($data["metadata"]); + if(! empty($data['metadata'])) + $this->setMetadata($data['metadata']); - if(! empty($data["sandbox"])) - $this->setSandbox($data["sandbox"]); + if(! empty($data['sandbox'])) + $this->setSandbox($data['sandbox']); - if(! empty($data["created_at"])) - $this->setCreatedAt($data["created_at"]); + if(! empty($data['created_at'])) + $this->setCreatedAt($data['created_at']); return $this; } + /** * Get the transaction's refunds. * @param array $options * @return array */ - public function refunds($options = array()) + public function fetchRefunds($options = array()) { - $cur = $this; - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/transactions/" . urlencode($this->getId()) . "/refunds"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -633,33 +629,59 @@ public function refunds($options = array()) $body = $response->getBody(); foreach($body['refunds'] as $v) { - $tmp = new Refund($cur->instance); + $tmp = new Refund($this->client); $tmp->fillWithData($v); $a[] = $tmp; } + $returnValues['Refunds'] = $a; + + return array_values($returnValues)[0]; + } + + /** + * Find a transaction's refund by its ID. + * @param string $refundId + * @param array $options + * @return Refund + */ + public function findRefund($refundId, $options = array()) + { + $request = new Request($this->client); + $path = "/transactions/" . urlencode($this->getId()) . "/refunds/" . urlencode($refundId) . ""; + + $data = array( - $returnValues["Refunds"] = $a; + ); + + $response = $request->get($path, $data, $options); + $returnValues = array(); + + + // Handling for field refund + $body = $response->getBody(); + $body = $body['refund']; + $refund = new Refund($this->client); + $returnValues['refund'] = $refund->fillWithData($body); - return array_values($returnValues)[0]; + return array_values($returnValues)[0]; } - + /** * Get all the transactions. * @param array $options * @return array */ - public static function all($options = array()) + public function all($options = array()) { - $cur = new Transaction(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/transactions"; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); @@ -668,45 +690,40 @@ public static function all($options = array()) $body = $response->getBody(); foreach($body['transactions'] as $v) { - $tmp = new Transaction($cur->instance); + $tmp = new Transaction($this->client); $tmp->fillWithData($v); $a[] = $tmp; } - - $returnValues["Transactions"] = $a; - - return array_values($returnValues)[0]; + $returnValues['Transactions'] = $a; + return array_values($returnValues)[0]; } - + /** * Find a transaction by its ID. * @param string $transactionId * @param array $options * @return $this */ - public static function find($transactionId, $options = array()) + public function find($transactionId, $options = array()) { - $cur = new Transaction(); - $request = new RequestProcessoutPrivate($cur->instance); + $request = new Request($this->client); $path = "/transactions/" . urlencode($transactionId) . ""; $data = array( ); - $response = new Response($request->get($path, $data, $options)); + $response = $request->get($path, $data, $options); $returnValues = array(); // Handling for field transaction $body = $response->getBody(); - $body = $body['transaction']; - - $returnValues["find"] = $cur->fillWithData($body); - return array_values($returnValues)[0]; + $body = $body['transaction']; + $returnValues['find'] = $this->fillWithData($body); + return array_values($returnValues)[0]; } - } diff --git a/src/Webhook.php b/src/Webhook.php index d4ffc9b..9c1eeda 100755 --- a/src/Webhook.php +++ b/src/Webhook.php @@ -5,18 +5,16 @@ namespace ProcessOut; use ProcessOut\ProcessOut; -use ProcessOut\Networking\Response; -use ProcessOut\Networking\RequestProcessoutPrivate; - +use ProcessOut\Networking\Request; class Webhook { /** - * ProcessOut's instance + * ProcessOut's client * @var ProcessOut\ProcessOut */ - protected $instance; + protected $client; /** * ID of the recurring invoice @@ -92,18 +90,16 @@ class Webhook /** * Webhook constructor - * @param ProcessOut\ProcessOut|null $processOut + * @param ProcessOut\ProcessOut $client + * @param array|null $prefill */ - public function __construct(ProcessOut $processOut = null) + public function __construct(ProcessOut $client, $prefill = array()) { - if(is_null($processOut)) - { - $processOut = ProcessOut::getDefault(); - } - - $this->instance = $processOut; + $this->client = $client; + + $this->fillWithData($prefill); } @@ -151,7 +147,7 @@ public function setProject($value) $this->project = $value; else { - $obj = new Project($this->instance); + $obj = new Project($this->client); $obj->fillWithData($value); $this->project = $obj; } @@ -180,7 +176,7 @@ public function setEvent($value) $this->event = $value; else { - $obj = new Event($this->instance); + $obj = new Event($this->client); $obj->fillWithData($value); $this->event = $obj; } @@ -393,41 +389,41 @@ public function setReleaseAt($value) */ public function fillWithData($data) { - if(! empty($data["id"])) - $this->setId($data["id"]); + if(! empty($data['id'])) + $this->setId($data['id']); - if(! empty($data["project"])) - $this->setProject($data["project"]); + if(! empty($data['project'])) + $this->setProject($data['project']); - if(! empty($data["event"])) - $this->setEvent($data["event"]); + if(! empty($data['event'])) + $this->setEvent($data['event']); - if(! empty($data["request_url"])) - $this->setRequestUrl($data["request_url"]); + if(! empty($data['request_url'])) + $this->setRequestUrl($data['request_url']); - if(! empty($data["request_method"])) - $this->setRequestMethod($data["request_method"]); + if(! empty($data['request_method'])) + $this->setRequestMethod($data['request_method']); - if(! empty($data["response_body"])) - $this->setResponseBody($data["response_body"]); + if(! empty($data['response_body'])) + $this->setResponseBody($data['response_body']); - if(! empty($data["response_code"])) - $this->setResponseCode($data["response_code"]); + if(! empty($data['response_code'])) + $this->setResponseCode($data['response_code']); - if(! empty($data["response_headers"])) - $this->setResponseHeaders($data["response_headers"]); + if(! empty($data['response_headers'])) + $this->setResponseHeaders($data['response_headers']); - if(! empty($data["response_time_ms"])) - $this->setResponseTimeMs($data["response_time_ms"]); + if(! empty($data['response_time_ms'])) + $this->setResponseTimeMs($data['response_time_ms']); - if(! empty($data["status"])) - $this->setStatus($data["status"]); + if(! empty($data['status'])) + $this->setStatus($data['status']); - if(! empty($data["created_at"])) - $this->setCreatedAt($data["created_at"]); + if(! empty($data['created_at'])) + $this->setCreatedAt($data['created_at']); - if(! empty($data["release_at"])) - $this->setReleaseAt($data["release_at"]); + if(! empty($data['release_at'])) + $this->setReleaseAt($data['release_at']); return $this; }