Skip to content

Commit

Permalink
Merge branch 'fyfey-redirect_flow'
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristian Giordano committed May 29, 2015
2 parents f4b7de4 + ab44459 commit 70377c7
Show file tree
Hide file tree
Showing 6 changed files with 420 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use GoCardless\Pro\Models\CustomerBankAccount;
use GoCardless\Pro\Models\Mandate;
use GoCardless\Pro\Models\Payment;
use GoCardless\Pro\Models\RedirectFlow;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;

Expand All @@ -24,6 +25,7 @@ class Api
const CUSTOMER_BANK_ACCOUNTS = 'customer_bank_accounts';
const MANDATES = 'mandates';
const PAYMENTS = 'payments';
const REDIRECT_FLOWS = 'redirect_flows';
const HELPERS = 'helpers';

const SANDBOX_URL = 'https://api-sandbox.gocardless.com/';
Expand Down Expand Up @@ -471,6 +473,59 @@ public function retryPayment($id)
return Payment::fromArray($response);
}

/**
* @see https://developer.gocardless.com/pro/#redirect-flows-create-a-redirect-flow
*
* @param RedirectFlow $redirectFlow Redirect Flow Entity
*
* @return RedirectFlow
*/
public function createRedirectFlow(RedirectFlow $redirectFlow)
{
$response = $this->post(self::REDIRECT_FLOWS, $redirectFlow->toArray());

return RedirectFlow::fromArray($response);
}

/**
* @see https://developer.gocardless.com/pro/#redirect-flows-get-a-single-redirect-flow
*
* @param string $id Redirect Flow ID
*
* @return RedirectFlow
*/
public function getRedirectFlow($id)
{
$response = $this->get(self::REDIRECT_FLOWS, [], $id);

return RedirectFlow::fromArray($response);
}

/**
* @see https://developer.gocardless.com/pro/#redirect-flows-complete-a-redirect-flow
*
* @param string $id Redirect Flow ID
* @param string $sessionToken Session token used to create the flow
*
* @return RedirectFlow
*/
public function completeRedirectFlow($id, $sessionToken)
{
try {
$response = $this->client->post(
$this->url(self::REDIRECT_FLOWS, $id . '/actions/complete'), [
'headers' => $this->headers(),
'json' => ['data' => ['session_token' => $sessionToken]]
]
)->json();

} catch (BadResponseException $ex) {
$this->handleBadResponseException($ex);
}

return RedirectFlow::fromArray($response[self::REDIRECT_FLOWS]);
}

/**
* @param string $endpoint
* @param array $params
Expand Down
20 changes: 20 additions & 0 deletions src/Models/Abstracts/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ abstract class Entity

/**
* @param $id
*
* @return static
*/
public static function withId($id)
Expand All @@ -30,6 +31,7 @@ public function getId()

/**
* @param string $id
*
* @return $this
*/
public function setId($id)
Expand All @@ -48,10 +50,28 @@ public function getCreatedAt()
}

/**
* Return all links associated with the Entity
*
* @return array
*/
public function getLinks()
{
return $this->links;
}

/**
* Return a single link from the entity
*
* @param string $link Key of link to return
*
* @return string Link ID or null if not set
*/
public function getLink($link)
{
if (!isset($this->links[$link])) {
return null;
}

return $this->links[$link];
}
}
231 changes: 231 additions & 0 deletions src/Models/RedirectFlow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
<?php

namespace GoCardless\Pro\Models;

use GoCardless\Pro\Models\Abstracts\Entity;
use GoCardless\Pro\Models\Traits\Factory;
use GoCardless\Pro\Models\Traits\Metadata;

/**
* Redirect flow entity
*
* @see Entity
*/
class RedirectFlow extends Entity
{
use Factory;

/**
* Creditor entity
*
* @var Creditor
*/
protected $creditor;

/**
* Mandate entity
*
* @var Mandate
*/
protected $mandate;

/**
* The URI to redirect to upon success mandate setup.
*
* @var string
*/
protected $success_redirect_url;

/**
* The URI to redirect the customer to to setup their mandate.
*
* @var string
*/
protected $redirect_url;

/**
* Direct Debit scheme for the mandate. If specified, the payment
* pages will only allow set-up of a mandate for the specified scheme.
*
* @var string
*/
protected $scheme;

/**
* The customer's session ID
*
* @var string
*/
protected $session_token;

/**
* Create Payment Flow with optional Creditor & Mandate
*
* @param Creditor $creditor Creditor
* @param Mandate $mandate Mandate
*/
public function __construct(Creditor $creditor = null, Mandate $mandate = null)
{
$this->setCreditor($creditor);
$this->setMandate($mandate);
$this->useBacs();
}

/**
* Set credtiror
*
* @param Creditor $creditor Creditor entity
*
* @return $this
*/
public function setCreditor(Creditor $creditor = null)
{
$this->creditor = $creditor;

return $this;
}

/**
* Set mandate
*
* @param Mandate $mandate Mandate entity
*
* @return $this
*/
public function setMandate(Mandate $mandate = null)
{
$this->mandate = $mandate;

return $this;
}

/**
* Set the success redirect URL
*
* @param string $successRedirectUrl Url to redirect user to after completion
*
* @return $this
*/
public function setSuccessRedirectUrl($successRedirectUrl)
{
$this->success_redirect_url = $successRedirectUrl;

return $this;
}

/**
* Sets scheme internally. Use specific setter e.g. useBacs publically
*
* @param string $scheme Scheme type
*
* @return $this
*/
protected function setScheme($scheme)
{
$this->scheme = $scheme;

return $this;
}

/**
* Set to use the bacs scheme for the resulting mandate
*
* @return Mandate
*/
public function useBacs()
{
return $this->setScheme('bacs');
}

/**
* Set to use the sepa core scheme for the resulting mandate
*
* @return Mandate
*/
public function useSepaCore()
{
return $this->setScheme('sepa_core');
}

/**
* Gets the Direct Debit scheme
*
* @return string
*/
public function getScheme()
{
return $this->scheme;
}

/**
* Is the scheme bacs?
*
* @return bool
*/
public function isBacs()
{
return $this->getScheme() === 'bacs';
}

/**
* Is the scheme SepeCore?
*
* @return bool
*/
public function isSepaCore()
{
return $this->getScheme() === 'sepa_core';
}

/**
* Gets the session token
*
* @return string
*/
public function getSessionToken()
{
return $this->session_token;
}

/**
* Gets the redirect URL
*
* @return string
*/
public function getRedirectUrl()
{
return $this->redirect_url;
}

/**
* Gets the success redirect URL
*
* @return string
*/
public function getSuccessRedirectUrl()
{
return $this->success_redirect_url;
}

/**
* Returns the entity as an array (as the API expects)
*
* @return array
*/
public function toArray()
{
$redirectFlow = array_filter(get_object_vars($this));

if ($this->creditor instanceof Creditor) {
unset($redirectFlow['creditor']);
$redirectFlow['links']['creditor'] = $this->creditor->getId();
}

if ($this->mandate instanceof Mandate) {
unset($redirectFlow['mandate']);
$redirectFlow['links']['mandate'] = $this->mandate->getId();
}

return $redirectFlow;
}
}
11 changes: 11 additions & 0 deletions tests/Models/BasicEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace GoCardless\Pro\Tests\Models;

use GoCardless\Pro\Models\Abstracts\Entity;
use GoCardless\Pro\Models\Traits\Factory;

class BasicEntity extends Entity
{
use Factory;
}
27 changes: 27 additions & 0 deletions tests/Models/EntityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php namespace GoCardless\Pro\Tests\Models;

class EntityTest extends \PHPUnit_Framework_TestCase
{
/** @test */
function it_can_get_all_links()
{
$entity = BasicEntity::fromArray(
[
'links' => [
'customer_id' => 'CU12345',
'mandate_id' => 'MD12345'
]
]
);

$this->assertCount(2, $entity->getLinks());
}

/** @test */
function it_can_get_a_single_link()
{
$entity = BasicEntity::fromArray(['links' => ['customer_id' => 'CU12345']]);

$this->assertEquals('CU12345', $entity->getLink('customer_id'));
}
}
Loading

0 comments on commit 70377c7

Please sign in to comment.