Skip to content

Commit

Permalink
Added Customer and Customer-Payments resources and examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rick committed Mar 1, 2016
1 parent fe44e0e commit bb72cc8
Show file tree
Hide file tree
Showing 14 changed files with 456 additions and 106 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "mollie/mollie-api-php",
"version": "1.3.3",

This comment has been minimized.

Copy link
@mathiasselleslach

mathiasselleslach Mar 9, 2016

Dear @RickWong,

Is there a reason that the version number was removed here?
This causes trouble with the Drupal mollie_payment module...

This comment has been minimized.

Copy link
@RickWong

RickWong Mar 9, 2016

Contributor

https://getcomposer.org/doc/04-schema.md#version

The version field is not required by composer/packagist. We're now using git tags for versioning. It works well with GitHub releases.

Do you know why Drupal requires this field to be in composer.json?

This comment has been minimized.

Copy link
@mathiasselleslach

mathiasselleslach Mar 9, 2016

Hi @RickWong,
yeah sure.
The libraries API is depending on it to determine the version of the mollie API

Please find the snippet below

`function mollie_payment_libraries_info() {
$libraries = array();

$libraries['mollie_api'] = array(
'name' => 'Mollie API client for PHP',
'vendor url' => 'https://www.mollie.nl/',
'download url' => 'https://github.com/mollie/mollie-api-php',
'version arguments' => array(
'file' => 'composer.json',
'pattern' => '/"version": "([0-9a-zA-Z.-]+)"/',
'lines' => 22,
),
'files' => array(
'php' => array('src/Mollie/API/Autoloader.php'),
),
);

return $libraries;
}`

This comment has been minimized.

Copy link
@RickWong

RickWong Mar 9, 2016

Contributor

Can you create an Issue for this? We'll then probably put it back.

"description": "Mollie API client library for PHP",
"homepage": "https://github.com/mollie/mollie-api-php",
"license": "BSD-2-Clause",
Expand Down
31 changes: 31 additions & 0 deletions examples/11-new-customer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
* Example 11 - How to create a new customer in the Mollie API.
*/

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

/*
* Customer creation parameters.
*
* See: https://www.mollie.com/en/docs/reference/customers/create
*/
$customer = $mollie->customers->create(array(
"name" => "Luke Skywalker",
"email" => "[email protected]",
"metadata" => array(
"isJedi" => TRUE,
),
));

echo "<p>New customer created " . htmlspecialchars($customer->id) . " (" . htmlspecialchars($customer->name) . ").</p>";
}
catch (Mollie_API_Exception $e)
{
echo "API call failed: " . htmlspecialchars($e->getMessage());
}
69 changes: 69 additions & 0 deletions examples/12-new-customer-payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/*
* Example 12 - How to create a new customer in the Mollie API.
*/

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

/**
* Retrieve the last created customer for this example.
* If no customers are created yet, run example 11.
*/
$customer = $mollie->customers->all(0, 1)->data[0];

/*
* Generate a unique order id for this example. It is important to include this unique attribute
* in the redirectUrl (below) so a proper return page can be shown to the customer.
*/
$order_id = time();

/*
* Determine the url parts to these example files.
*/
$protocol = isset($_SERVER['HTTPS']) && strcasecmp('off', $_SERVER['HTTPS']) !== 0 ? "https" : "http";
$hostname = $_SERVER['HTTP_HOST'];
$path = dirname(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF']);

/*
* Customer Payment creation parameters.
*
* See: https://www.mollie.com/en/docs/reference/customers/create-payment
*/
$payment = $mollie->customers_payments->with($customer)->create(array(
"amount" => 10.00,
"description" => "My first Customer payment",
"redirectUrl" => "{$protocol}://{$hostname}{$path}/03-return-page.php?order_id={$order_id}",
"webhookUrl" => "{$protocol}://{$hostname}{$path}/02-webhook-verification.php"
));

/*
* In this example we store the order with its payment status in a database.
*/
database_write($order_id, $payment->status);

/*
* Send the customer off to complete the payment.
*/
header("Location: " . $payment->getPaymentUrl());
}
catch (Mollie_API_Exception $e)
{
echo "API call failed: " . htmlspecialchars($e->getMessage());
}


/*
* NOTE: This example uses a text file as a database. Please use a real database like MySQL in production code.
*/
function database_write ($order_id, $status)
{
$order_id = intval($order_id);
$database = dirname(__FILE__) . "/orders/order-{$order_id}.txt";

file_put_contents($database, $status);
}
38 changes: 38 additions & 0 deletions examples/13-customer-payments-history.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/*
* Example 13 - How to retrieve your customers' payments history.
*/

try
{
/*
* Initialize the Mollie API library with your API key.
*
* See: https://www.mollie.com/beheer/account/profielen/
*/
include "initialize.php";

/**
* Retrieve the last created customer for this example.
* If no customers are created yet, run example 11.
*/
$customer = $mollie->customers->all(0, 1)->data[0];

// Pagination
$offset = 0;
$limit = 25;

/*
* Get the all payments for this API key ordered by newest.
*/
$payments = $mollie->customers_payments->with($customer)->all($offset, $limit);

foreach ($payments as $payment)
{
echo "&euro; " . htmlspecialchars($payment->amount) . ", status: " . htmlspecialchars($payment->status) . "<br>";
}
}
catch (Mollie_API_Exception $e)
{
echo "API call failed: " . htmlspecialchars($e->getMessage());
}
32 changes: 24 additions & 8 deletions src/Mollie/API/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Mollie_API_Client
/**
* Version of our client.
*/
const CLIENT_VERSION = "1.3.3";
const CLIENT_VERSION = "1.4.0";

/**
* Endpoint of the remote API.
Expand Down Expand Up @@ -111,6 +111,20 @@ class Mollie_API_Client
*/
public $settlements;

/**
* RESTful Customers resource.
*
* @var Mollie_API_Resource_Customers
*/
public $customers;

/**
* RESTful Customers Payments resource.
*
* @var Mollie_API_Resource_Customers_Payments
*/
public $customers_payments;

/**
* @var string
*/
Expand Down Expand Up @@ -141,10 +155,12 @@ public function __construct ()
$this->getCompatibilityChecker()
->checkCompatibility();

$this->payments = new Mollie_API_Resource_Payments($this);
$this->payments_refunds = new Mollie_API_Resource_Payments_Refunds($this);
$this->issuers = new Mollie_API_Resource_Issuers($this);
$this->methods = new Mollie_API_Resource_Methods($this);
$this->payments = new Mollie_API_Resource_Payments($this);
$this->payments_refunds = new Mollie_API_Resource_Payments_Refunds($this);
$this->issuers = new Mollie_API_Resource_Issuers($this);
$this->methods = new Mollie_API_Resource_Methods($this);
$this->customers = new Mollie_API_Resource_Customers($this);
$this->customers_payments = new Mollie_API_Resource_Customers_Payments($this);

// OAuth2 endpoints
$this->permissions = new Mollie_API_Resource_Permissions($this);
Expand All @@ -161,13 +177,13 @@ public function __construct ()
}

/**
* @param string $resource_name
* @param string $resource_path
* @return Mollie_API_Resource_Undefined
*/
public function __get ($resource_name)
public function __get ($resource_path)
{
$undefined_resource = new Mollie_API_Resource_Undefined($this);
$undefined_resource->setResourceName($resource_name);
$undefined_resource->setResourcePath($resource_path);

return $undefined_resource;
}
Expand Down
82 changes: 82 additions & 0 deletions src/Mollie/API/Object/Customer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Copyright (c) 2015, Mollie B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* @license Berkeley Software Distribution License (BSD-License 2) http://www.opensource.org/licenses/bsd-license.php
* @author Mollie B.V. <[email protected]>
* @copyright Mollie B.V.
* @link https://www.mollie.com
*/
class Mollie_API_Object_Customer
{
/**
* @var string
*/
public $resource;

/**
* Id of the customer.
*
* @var string
*/
public $id;

/**
* Either "live" or "test". Indicates this being a test or a live (verified) customer.
*
* @var string
*/
public $mode;

/**
* @var string
*/
public $name;

/**
* @var string
*/
public $email;

/**
* @var string|null
*/
public $locale;

/**
* @var object|mixed|null
*/
public $metadata;

/**
* @var string[]|array
*/
public $recentlyUsedMethods;

/**
* @var string
*/
public $createdDatetime;
}
13 changes: 13 additions & 0 deletions src/Mollie/API/Object/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class Mollie_API_Object_Payment
*/
const STATUS_CHARGED_BACK = "charged_back";

/**
* @var string
*/
public $resource;

/**
* Id of the payment (on the Mollie platform).
*
Expand Down Expand Up @@ -180,6 +185,14 @@ class Mollie_API_Object_Payment
*/
public $profileId;

/**
* The customer ID this payment is performed by.
*
* @example cst_51EkUqla3
* @var string
*/
public $customerId;

/**
* The locale used for this payment.
*
Expand Down
Loading

0 comments on commit bb72cc8

Please sign in to comment.