Skip to content

Commit

Permalink
Merge pull request #4 from brandedcrate/add_vaults_suppoert
Browse files Browse the repository at this point in the history
refs #3 add vaults support
  • Loading branch information
stevecrozz committed Apr 2, 2015
2 parents a2b8137 + daab99b commit eab5b52
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 3 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lib/BrandedCrate/PayJunction/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use BrandedCrate\PayJunction\TransactionClient;
use BrandedCrate\PayJunction\CustomerClient;
use BrandedCrate\PayJunction\CustomerVaultClient;
use BrandedCrate\PayJunction\ReceiptClient;
use BrandedCrate\PayJunction\Exception;

Expand Down Expand Up @@ -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;
}
}
59 changes: 59 additions & 0 deletions lib/BrandedCrate/PayJunction/CustomerVaultClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php namespace BrandedCrate\PayJunction;

class CustomerVaultClient extends Client
{
/**
* create a new customer vault
* @param $customerId
* @param $params
* @return array|mixed
*/
public function create($customerId, $params)
{
return $this->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");
}
}
2 changes: 1 addition & 1 deletion test/integration/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
134 changes: 134 additions & 0 deletions test/integration/CustomerVaultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace BrandedCrate\PayJunction\Test\Integration;

use BrandedCrate\PayJunction;

class CustomerVaultTest extends \PHPUnit_Framework_TestCase
{
/**
* Runs once before all tests are started
*/
public function setUp()
{
$options = array(
'username' => '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' => '[email protected]',
'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');
}
}
2 changes: 1 addition & 1 deletion test/integration/ReceiptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion test/integration/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit eab5b52

Please sign in to comment.