diff --git a/README.md b/README.md index 65ad9ea..ecc6a60 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,44 @@ $customer->customerId; // int(7902) $customer->uri; // "https://api.payjunctionlabs.com/customers/7902" ``` +### vaults + +Create a customer vault + +```php +$vault = $pj->customerVault()->create('345678', array( + 'cardNumber' => '4242424242424242', + 'cardExpMonth' => '2', + 'cardExpYear' => '25' +)); +$vault->vaultId; // int(44) +``` + +Read a vault +```php +$vault = $pj->customerVault()->read('345678', '44'); +``` + +Index all vaults for a customer +```php +$vaults = $pj->customerVault()->index('345678'); +``` + +Update a vault +```php +$updatedVault = $pj->customerVault()->update('345678', '44', array( + 'address' => '1600 Pennsylvania Ave NW', + 'city' => 'Washington', + 'state' => 'DC', + 'zip' => 20500 +)); +``` + +Delete a vault +```php +$result = $pj->customerVault()->delete('345678', '44'); // true +``` + ## Running Tests This package includes standalone unit tests and integration tests. Run them diff --git a/lib/BrandedCrate/PayJunction/Client.php b/lib/BrandedCrate/PayJunction/Client.php index 58992a0..70a6c93 100644 --- a/lib/BrandedCrate/PayJunction/Client.php +++ b/lib/BrandedCrate/PayJunction/Client.php @@ -2,6 +2,7 @@ use BrandedCrate\PayJunction\TransactionClient; use BrandedCrate\PayJunction\CustomerClient; +use BrandedCrate\PayJunction\CustomerVaultClient; use BrandedCrate\PayJunction\ReceiptClient; use BrandedCrate\PayJunction\Exception; @@ -236,4 +237,16 @@ public function customer() } return $this->customerClient; } + + /** + * @description returns an instance of the customerVault client + * @return CustomerVaultClient + */ + public function customerVault() + { + if (!isset($this->customerVaultClient) && isset($this->options)) { + $this->customerVaultClient = new CustomerVaultClient($this->options); + } + return $this->customerVaultClient; + } } diff --git a/lib/BrandedCrate/PayJunction/CustomerVaultClient.php b/lib/BrandedCrate/PayJunction/CustomerVaultClient.php new file mode 100644 index 0000000..010e1d6 --- /dev/null +++ b/lib/BrandedCrate/PayJunction/CustomerVaultClient.php @@ -0,0 +1,59 @@ +post("/customers/$customerId/vaults", $params); + } + + /** + * read a customer vault + * @param $customerid + * @param $id + * @return array|mixed + */ + public function read($customerId, $id) + { + return $this->get("/customers/$customerId/vaults/$id"); + } + + /** + * index all customer vaults + * @param $customerid + * @return array|mixed + */ + public function index($customerId) + { + return $this->get("/customers/$customerId/vaults"); + } + + /** + * update existing customer vault + * @param $customerId + * @param $id + * @param null $params + * @return array|mixed + */ + public function update($customerId, $id, $params = null) + { + return $this->put("/customers/$customerId/vaults/$id", $params); + } + + /** + * delete a customer vault + * @param $customerId + * @param $id + * @return array|mixed + */ + public function delete($customerId, $id) + { + return $this->del("/customers/$customerId/vaults/$id"); + } +} diff --git a/test/integration/CustomerTest.php b/test/integration/CustomerTest.php index cefaec1..c825f8f 100644 --- a/test/integration/CustomerTest.php +++ b/test/integration/CustomerTest.php @@ -10,7 +10,7 @@ class CustomerIntegrationTest extends \PHPUnit_Framework_TestCase private $createData; /** - * Runs once before all tests are starte d * + * Runs once before all tests are started */ public function setUp() { diff --git a/test/integration/CustomerVaultTest.php b/test/integration/CustomerVaultTest.php new file mode 100644 index 0000000..587e671 --- /dev/null +++ b/test/integration/CustomerVaultTest.php @@ -0,0 +1,134 @@ + 'pj-ql-01', + 'password' => 'pj-ql-01p', + 'appkey' => '2489d40d-a74f-474f-9e8e-7b39507f3101', + 'endpoint' => 'test' + ); + + $this->client = new PayJunction\Client($options); + + parent::setUp(); + } + + public function createCustomer() + { + return $this->client->customer()->create(array( + 'email' => 'customer@acme.com', + 'firstName' => 'Joe', + 'lastName' => 'Schmoe', + )); + } + + public function createCustomerVaultMc($customer) + { + return $this->client->customerVault() + ->create($customer->customerId, array( + 'cardNumber' => '5105105105105100', + 'cardExpMonth' => '9', + 'cardExpYear' => '24', + 'address' => '1600 Pennsylvania Ave NW', + 'city' => 'Washington', + 'state' => 'DC', + 'zip' => 20500 + )); + } + + public function createCustomerVaultVisa($customer) + { + return $this->client->customerVault() + ->create($customer->customerId, array( + 'cardNumber' => '4242424242424242', + 'cardExpMonth' => '2', + 'cardExpYear' => '25' + )); + } + + public function createCustomerVaultAch($customer) + { + return $this->client->customerVault() + ->create($customer->customerId, array( + 'achRoutingNumber' => '104000016', + 'achAccountNumber' => '123456789', + 'achAccountType' => 'CHECKING', + 'achType' => 'CCD' + )); + } + + public function testCreateCustomerVault() + { + $customer = $this->createCustomer(); + $vault = $this->createCustomerVaultMc($customer); + + $type = gettype($vault->vaultId); + $this->assertTrue( + is_integer($vault->vaultId), + "Got a $type instead of an integer. A vault was not created" + ); + } + + public function testReadCustomerVault() + { + $customer = $this->createCustomer(); + $vaultId = $this->createCustomerVaultMc($customer)->vaultId; + + $vault = $this->client->customerVault() + ->read($customer->customerId, $vaultId); + + $this->assertEquals($vault->lastFour, 5100, 'Credit card is not in vault'); + } + + public function testIndexCustomerVault() + { + $customer = $this->createCustomer(); + $vault1Id = $this->createCustomerVaultMc($customer)->vaultId; + $vault2Id = $this->createCustomerVaultVisa($customer)->vaultId; + + $vaults = $this->client->customerVault() + ->index($customer->customerId); + + $vaultIdsInIndex = array( + $vaults->results[0]->vaultId, + $vaults->results[1]->vaultId, + ); + + $this->assertEquals(in_array($vault1Id, $vaultIdsInIndex), true, 'Missing vault 1'); + $this->assertEquals(in_array($vault2Id, $vaultIdsInIndex), true, 'Missing vault 2'); + } + + public function testUpdateCustomerVault() + { + $customer = $this->createCustomer(); + $vaultId = $this->createCustomerVaultAch($customer)->vaultId; + + $vault = $this->client->customerVault() + ->update($customer->customerId, $vaultId, array( + 'address' => 'somenewaddress' + )); + + $this->assertEquals($vault->address->address, 'somenewaddress', 'Address unchanged'); + } + + public function testDeleteCustomerVault() + { + $customer = $this->createCustomer(); + $vaultId = $this->createCustomerVaultAch($customer)->vaultId; + + $response = $this->client->customerVault() + ->delete($customer->customerId, $vaultId); + + $this->assertEquals($response, true, 'Vault not deleted'); + } +} diff --git a/test/integration/ReceiptTest.php b/test/integration/ReceiptTest.php index 310c54b..7f5d892 100644 --- a/test/integration/ReceiptTest.php +++ b/test/integration/ReceiptTest.php @@ -10,7 +10,7 @@ class ReceiptIntegrationTest extends \PHPUnit_Framework_TestCase private $createData; /** - * Runs once before all tests are started * + * Runs once before all tests are started */ public function setUp() { diff --git a/test/integration/TransactionTest.php b/test/integration/TransactionTest.php index 1c07aa4..780cf29 100644 --- a/test/integration/TransactionTest.php +++ b/test/integration/TransactionTest.php @@ -8,7 +8,7 @@ class TransactionIntegrationTest extends \PHPUnit_Framework_TestCase { /** - * Runs once before all tests are started * + * Runs once before all tests are started */ public function setUp() {