Skip to content

Commit

Permalink
Merge pull request #38 from mollie/issue/21-add-customer-update
Browse files Browse the repository at this point in the history
Issue/21 add customer update
  • Loading branch information
Daan van Marsbergen committed Sep 16, 2016
2 parents b6e73f4 + 4fd6898 commit 99cdc97
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
@@ -1,7 +1,7 @@
{
"name": "mollie/mollie-api-php",
"description": "Mollie API client library for PHP",
"version": "1.6.6",
"version": "1.7.0",
"homepage": "https://github.com/mollie/mollie-api-php",
"license": "BSD-2-Clause",
"authors": [
Expand Down
35 changes: 35 additions & 0 deletions examples/18-update-customer.php
@@ -0,0 +1,35 @@
<?php
/*
* Example 18 - Updating an existing customer via the Mollie API.
*/

try
{
/*
* Initialize the Mollie API library with your API key or OAuth access token.
*/
include "initialize.php";

/*
* Retrieve an existing customer by his customerId
*/
$customer = $mollie->customers->get("cst_zAQzfr3Raq");

/**
* Customer fields that can be updated.
*
* @See https://www.mollie.com/en/docs/reference/customers/update
*/
$customer->name = "Luke Skywalker";
$customer->email = "luke@example.org";
$customer->locale = "en";
$customer->metadata->isJedi = TRUE;

$customer = $mollie->customers->update($customer);

echo "<p>Customer updated: " . htmlspecialchars($customer->name) . "</p>";
}
catch (Mollie_API_Exception $e)
{
echo "API call failed: " . htmlspecialchars($e->getMessage());
}
27 changes: 27 additions & 0 deletions src/Mollie/API/Resource/Base.php
Expand Up @@ -155,6 +155,33 @@ private function rest_delete ($rest_resource, $id)
return $this->copy($result, $this->getResourceObject());
}

/**
* Sends a POST request to a single Molle API object to update it.
*
* @param string $rest_resource
* @param string $id
* @param string $body
*
* @return object
* @throws Mollie_API_Exception
*/
protected function rest_update ($rest_resource, $id, $body)
{
if (empty($id))
{
throw new Mollie_API_Exception("Invalid resource id.");
}

$id = urlencode($id);
$result = $this->performApiCall(
self::REST_UPDATE,
"{$rest_resource}/{$id}",
$body
);

return $this->copy($result, $this->getResourceObject());
}

/**
* Get a collection of objects from the REST API.
*
Expand Down
21 changes: 21 additions & 0 deletions src/Mollie/API/Resource/Customers.php
Expand Up @@ -31,9 +31,30 @@
*
* @method Mollie_API_Object_Customer[]|Mollie_API_Object_List all($offset = 0, $limit = 0, array $filters = array())
* @method Mollie_API_Object_Customer get($id, array $filters = array())
* @method Mollie_API_Object_Customer create(array $data, array $filters = array())
*/
class Mollie_API_Resource_Customers extends Mollie_API_Resource_Base
{
/**
* @param Mollie_API_Object_Customer $customer
*
* @return Mollie_API_Object_Customer
*/
public function update (Mollie_API_Object_Customer $customer)
{
$body = json_encode(array(
"name" => $customer->name,
"email" => $customer->email,
"locale" => $customer->locale,
"metadata" => $customer->metadata,
));

/** @var Mollie_API_Object_Customer $updated_customer */
$updated_customer = $this->rest_update($this->getResourcePath(), $customer->id, $body);

return $updated_customer;
}

/**
* @return Mollie_API_Object_Customer
*/
Expand Down
48 changes: 44 additions & 4 deletions tests/apiUnitTest.php
Expand Up @@ -60,7 +60,7 @@ public function testNotSettingApiKeyGivesException()
$this->api->expects($this->any())
->method("getCompatibilityChecker")
->will($this->returnValue($this->compatibilityChecker));

$this->api->__construct();
$this->api->payments->all();
}
Expand Down Expand Up @@ -373,15 +373,15 @@ public function testUndefinedResourceCallsResourceEndpoint ()
* @expectedException Mollie_API_Exception
* @expectedExceptionMessage Subresource 'foos_bars' used without parent 'foos' ID.
*/
public function testUndefinedSubresourceRequiresParentId ()
public function testUndefinedSubResourceRequiresParentId ()
{
$this->api->expects($this->never())
->method("performHttpCall");

$this->api->Foos_Bars->get("bar_ID", array("f" => "B"));
}

public function testUndefinedSubesourceCallsSubresourceEndpointWithParentId ()
public function testUndefinedSubResourceCallsSubresourceEndpointWithParentId ()
{
$this->api->expects($this->once())
->method("performHttpCall")
Expand All @@ -391,7 +391,7 @@ public function testUndefinedSubesourceCallsSubresourceEndpointWithParentId ()
$this->api->Foos_Bars->withParentId("foo_PARENT")->get("bar_CHILD", array("f" => "B"));
}

public function testUndefinedSubesourceCallsSubresourceEndpointWithParentObject ()
public function testUndefinedSubResourceCallsSubresourceEndpointWithParentObject ()
{
$this->api->expects($this->once())
->method("performHttpCall")
Expand All @@ -403,4 +403,44 @@ public function testUndefinedSubesourceCallsSubresourceEndpointWithParentObject

$this->api->Foos_Bars->with($parent)->get("bar_CHILD", array("f" => "B"));
}

public function testCustomerUpdateWorksCorrectly ()
{
$customer_id = "cst_8wmqcHMN4U";

$expected_customer_api_call_data = array(
"name" => "",
"email" => "",
"locale" => "",
"metadata" => array(
"my_id" => "1234567"
)
);

$return_value = array_merge($expected_customer_api_call_data, array(
"id" => $customer_id
));

$customer = new Mollie_API_Object_Customer();
$customer->resource = 'customers';
$customer->id = $customer_id;
$customer->name = $expected_customer_api_call_data['name'];
$customer->email = $expected_customer_api_call_data['email'];
$customer->locale = $expected_customer_api_call_data['locale'];
$customer->metadata = $expected_customer_api_call_data['metadata'];

$this->api->expects($this->once())
->method("performHttpCall")
->with(Mollie_API_Client::HTTP_POST, "customers/cst_8wmqcHMN4U", json_encode($expected_customer_api_call_data))
->will($this->returnValue(json_encode($return_value)));

/** @var Mollie_API_Object_Customer $updated_customer */
$updated_customer = $this->api->customers->update($customer);

self::assertEquals($updated_customer->id, $customer_id);
self::assertEquals($updated_customer->name, $expected_customer_api_call_data['name']);
self::assertEquals($updated_customer->email, $expected_customer_api_call_data['email']);
self::assertEquals($updated_customer->locale, $expected_customer_api_call_data['locale']);
self::assertEquals($updated_customer->metadata->my_id, $expected_customer_api_call_data['metadata']['my_id']);
}
}

0 comments on commit 99cdc97

Please sign in to comment.