Skip to content

Commit

Permalink
Add service with methods for nested resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Mar 9, 2020
1 parent b8ba85f commit 4633c6e
Show file tree
Hide file tree
Showing 4 changed files with 591 additions and 0 deletions.
1 change: 1 addition & 0 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
// Services
require __DIR__ . '/lib/Service/AbstractService.php';
require __DIR__ . '/lib/Service/CouponService.php';
require __DIR__ . '/lib/Service/CustomerService.php';
require __DIR__ . '/lib/Service/FileService.php';
require __DIR__ . '/lib/Service/Issuing/CardService.php';
require __DIR__ . '/lib/Service/PaymentIntentService.php';
41 changes: 41 additions & 0 deletions lib/Service/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,61 @@ protected function allObjects($params, $opts)
return $this->request('get', $this->basePath(), $params, $opts);
}

protected function allNestedObjects($nestedPath, $parentId, $params, $opts)
{
return $this->request('get', $this->baseNestedPath($parentId, $nestedPath), $params, $opts);
}

protected function createObject($params, $opts)
{
return $this->request('post', $this->basePath(), $params, $opts);
}

protected function createNestedObject($nestedPath, $parentId, $params, $opts)
{
return $this->request('post', $this->baseNestedPath($parentId, $nestedPath), $params, $opts);
}

protected function deleteObject($id, $params, $opts)
{
return $this->request('delete', $this->instancePath($id), $params, $opts);
}

protected function deleteNestedObject($nestedPath, $parentId, $id, $params, $opts)
{
return $this->request('delete', $this->instanceNestedPath($parentId, $nestedPath, $id), $params, $opts);
}

protected function retrieveObject($id, $params, $opts)
{
return $this->request('get', $this->instancePath($id), $params, $opts);
}

protected function retrieveNestedObject($nestedPath, $parentId, $id, $params, $opts)
{
return $this->request('get', $this->instanceNestedPath($parentId, $nestedPath, $id), $params, $opts);
}

protected function updateObject($id, $params, $opts)
{
return $this->request('post', $this->instancePath($id), $params, $opts);
}

protected function updateNestedObject($nestedPath, $parentId, $id, $params, $opts)
{
return $this->request('post', $this->instanceNestedPath($parentId, $nestedPath, $id), $params, $opts);
}

protected function request($method, $path, $params, $opts)
{
return $this->getClient()->request($method, $path, $params, $opts);
}

protected function baseNestedPath($parentId, $nestedPath)
{
return $this->instancePath($parentId) . '/' . $nestedPath;
}

protected function instancePath($id)
{
if (null === $id || '' === \trim($id)) {
Expand All @@ -80,4 +110,15 @@ protected function instancePath($id)

return $this->basePath() . '/' . \urlencode($id);
}

protected function instanceNestedPath($parentId, $nestedPath, $id)
{
if (null === $id || '' === \trim($id)) {
$msg = 'The resource ID cannot be null or whitespace.';

throw new \Stripe\Exception\InvalidArgumentException($msg);
}

return $this->baseNestedPath($parentId, $nestedPath) . '/' . \urlencode($id);
}
}
306 changes: 306 additions & 0 deletions lib/Service/CustomerService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
<?php

namespace Stripe\Service;

class CustomerService extends AbstractService
{
const BALANCE_TRANSACTIONS = 'balance_transactions';
const SOURCES = 'sources';
const TAX_IDS = 'tax_ids';

public function basePath()
{
return '/v1/customers';
}

/**
* List all customers.
*
* @param array $params
* @param array $opts
*
* @return \Stripe\Collection
*/
public function all($params = [], $opts = [])
{
return $this->allObjects($params, $opts);
}

/**
* List customer balance transactions.
*
* @param string $customerId
* @param array $params
* @param array $opts
*
* @return \Stripe\Collection
*/
public function allBalanceTransactions($customerId, $params = [], $opts = [])
{
return $this->allNestedObjects(self::BALANCE_TRANSACTIONS, $customerId, $params, $opts);
}

/**
* List all sources.
*
* @param string $customerId
* @param array $params
* @param array $opts
*
* @return \Stripe\Collection
*/
public function allSources($customerId, $params = [], $opts = [])
{
return $this->allNestedObjects(self::SOURCES, $customerId, $params, $opts);
}

/**
* List all tax IDs.
*
* @param string $customerId
* @param array $params
* @param array $opts
*
* @return \Stripe\Collection
*/
public function allTaxIds($customerId, $params = [], $opts = [])
{
return $this->allNestedObjects(self::TAX_IDS, $customerId, $params, $opts);
}

/**
* Create a customer.
*
* @param array $params
* @param array $opts
*
* @return \Stripe\Customer
*/
public function create($params = [], $opts = [])
{
return $this->createObject($params, $opts);
}

/**
* Create a balance transaction.
*
* @param string $customerId
* @param array $params
* @param array $opts
*
* @return \Stripe\CustomerBalanceTransaction
*/
public function createBalanceTransaction($customerId, $params = [], $opts = [])
{
return $this->createNestedObject(self::BALANCE_TRANSACTIONS, $customerId, $params, $opts);
}

/**
* Create a source.
*
* @param string $customerId
* @param array $params
* @param array $opts
*
* @return \Stripe\BankAccount|\Stripe\Card|\Stripe\Source
*/
public function createSource($customerId, $params = [], $opts = [])
{
return $this->createNestedObject(self::SOURCES, $customerId, $params, $opts);
}

/**
* Create a tax ID.
*
* @param string $customerId
* @param array $params
* @param array $opts
*
* @return \Stripe\TaxId
*/
public function createTaxId($customerId, $params = [], $opts = [])
{
return $this->createNestedObject(self::TAX_IDS, $customerId, $params, $opts);
}

/**
* Delete a customer.
*
* @param string $id
* @param array $params
* @param array $opts
*
* @return \Stripe\Customer
*/
public function delete($id, $params = [], $opts = [])
{
return $this->deleteObject($id, $params, $opts);
}

/**
* Delete a customer discount.
*
* @param string $id
* @param array $params
* @param array $opts
*
* @return \Stripe\Discount
*/
public function deleteDiscount($id, $params = [], $opts = [])
{
return $this->request('delete', $this->instancePath($id) . '/discount', $params, $opts);
}

/**
* Delete a source.
*
* @param string $customerId
* @param string $id
* @param array $params
* @param array $opts
*
* @return \Stripe\BankAccount|\Stripe\Card|\Stripe\Source
*/
public function deleteSource($customerId, $id, $params = [], $opts = [])
{
return $this->deleteNestedObject(self::SOURCES, $customerId, $id, $params, $opts);
}

/**
* Delete a tax ID.
*
* @param string $customerId
* @param string $id
* @param array $params
* @param array $opts
*
* @return \Stripe\TaxId
*/
public function deleteTaxId($customerId, $id, $params = [], $opts = [])
{
return $this->deleteNestedObject(self::TAX_IDS, $customerId, $id, $params, $opts);
}

/**
* Retrieve a customer.
*
* @param string $id
* @param array $params
* @param array $opts
*
* @return \Stripe\Customer
*/
public function retrieve($id, $params = [], $opts = [])
{
return $this->retrieveObject($id, $params, $opts);
}

/**
* Retrieve a balance transaction.
*
* @param string $customerId
* @param string $id
* @param array $params
* @param array $opts
*
* @return \Stripe\CustomerBalanceTransaction
*/
public function retrieveBalanceTransaction($customerId, $id, $params = [], $opts = [])
{
return $this->retrieveNestedObject(self::BALANCE_TRANSACTIONS, $customerId, $id, $params, $opts);
}

/**
* Retrieve a source.
*
* @param string $customerId
* @param string $id
* @param array $params
* @param array $opts
*
* @return \Stripe\BankAccount|\Stripe\Card|\Stripe\Source
*/
public function retrieveSource($customerId, $id, $params = [], $opts = [])
{
return $this->retrieveNestedObject(self::SOURCES, $customerId, $id, $params, $opts);
}

/**
* Retrieve a tax ID.
*
* @param string $customerId
* @param string $id
* @param array $params
* @param array $opts
*
* @return \Stripe\TaxId
*/
public function retrieveTaxId($customerId, $id, $params = [], $opts = [])
{
return $this->retrieveNestedObject(self::TAX_IDS, $customerId, $id, $params, $opts);
}

/**
* Update a customer.
*
* @param string $id
* @param array $params
* @param array $opts
*
* @return \Stripe\Customer
*/
public function update($id, $params = [], $opts = [])
{
return $this->updateObject($id, $params, $opts);
}

/**
* Update a balance transaction.
*
* @param string $customerId
* @param string $id
* @param array $params
* @param array $opts
*
* @return \Stripe\CustomerBalanceTransaction
*/
public function updateBalanceTransaction($customerId, $id, $params = [], $opts = [])
{
return $this->updateNestedObject(self::BALANCE_TRANSACTIONS, $customerId, $id, $params, $opts);
}

/**
* Update a source.
*
* @param string $customerId
* @param string $id
* @param array $params
* @param array $opts
*
* @return \Stripe\BankAccount|\Stripe\Card|\Stripe\Source
*/
public function updateSource($customerId, $id, $params = [], $opts = [])
{
return $this->updateNestedObject(self::SOURCES, $customerId, $id, $params, $opts);
}

/**
* Verify a source.
*
* @param string $customerId
* @param string $id
* @param array $params
* @param array $opts
*
* @return \Stripe\BankAccount
*/
public function verifySource($customerId, $id, $params = [], $opts = [])
{
return $this->request(
'post',
$this->instanceNestedPath($customerId, self::SOURCES, $id) . '/verify',
$params,
$opts
);
}
}
Loading

0 comments on commit 4633c6e

Please sign in to comment.