Skip to content

Commit

Permalink
Add support for refunding payments
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximiliano Gabriel Monge committed May 19, 2016
1 parent cb9ce48 commit d966c97
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 8 deletions.
3 changes: 2 additions & 1 deletion config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* This configuration is only needed for running tests.
*/
return [
'version' => '2015-04-29',
'version' => '2015-07-06',
'accessToken' => '',
'readOnlyAccessToken' => '',
'paymentToRefund' => '',
];
30 changes: 30 additions & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use GoCardless\Pro\Models\Mandate;
use GoCardless\Pro\Models\Payment;
use GoCardless\Pro\Models\RedirectFlow;
use GoCardless\Pro\Models\Refund;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\BadResponseException;

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

Expand Down Expand Up @@ -436,6 +438,20 @@ public function createPayment(Payment $payment)
return Payment::fromArray($response);
}

/**
* @see https://developer.gocardless.com/pro/#refunds-create-a-refund
*
* @param Refund $refund
*
* @return Refund
*/
public function createRefund(Refund $refund)
{
$response = $this->post(self::REFUNDS, $refund->toArray());

return Refund::fromArray($response);
}

/**
* @see https://developer.gocardless.com/pro/#payments-get-a-single-payment
*
Expand All @@ -450,6 +466,20 @@ public function getPayment($id)
return Payment::fromArray($response);
}

/**
* @see https://developer.gocardless.com/pro/#payments-list-payments
*
* @param array $options
*
* @return Payment[]
*/
public function listPayments(array $options = [])
{
$response = $this->get(self::PAYMENTS, $options);

return $this->buildCollection(new Payment, $response);
}

/**
* @see https://developer.gocardless.com/pro/#payments-cancel-a-payment
*
Expand Down
14 changes: 7 additions & 7 deletions src/Models/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Payment extends Entity
* @param $amount
* @param $currency
*
* @return $this
* @return Payment
*/
public function collect($amount, $currency)
{
Expand All @@ -53,7 +53,7 @@ public function using(Mandate $mandate)
/**
* @param Mandate $mandate
*
* @return $this
* @return Payment
*/
public function setMandate(Mandate $mandate)
{
Expand Down Expand Up @@ -83,7 +83,7 @@ public function getChargeDate()
/**
* @param $date
*
* @return $this
* @return Payment
*/
public function setChargeDate($date)
{
Expand All @@ -103,7 +103,7 @@ public function getAmount()
/**
* @param $amount
*
* @return $this
* @return Payment
*/
public function setAmount($amount)
{
Expand All @@ -123,7 +123,7 @@ public function getCurrency()
/**
* @param $currency
*
* @return $this
* @return Payment
*/
public function setCurrency($currency)
{
Expand All @@ -143,7 +143,7 @@ public function getDescription()
/**
* @param $description
*
* @return $this
* @return Payment
*/
public function setDescription($description)
{
Expand Down Expand Up @@ -227,7 +227,7 @@ public function getReference()
/**
* @param $reference
*
* @return $this
* @return Payment
*/
public function setReference($reference)
{
Expand Down
165 changes: 165 additions & 0 deletions src/Models/Refund.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

namespace GoCardless\Pro\Models;

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

class Refund extends Entity
{
use Factory;
use Metadata;

/** @var string */
private $amount;

/** @var string */
private $reference;

/** @var string */
private $currency;

/** @var string */
private $total_amount_confirmation;

/** @var Payment */
protected $payment;

/**
* @param Payment $payment
*
* @return Refund
*/
public function of(Payment $payment)
{
return $this->setPayment($payment);
}

/**
* @param $amount
*
* @return Refund
*/
public function returning($amount)
{
return $this->setAmount($amount);
}

/**
* @param $amount
*
* @return Refund
*/
public function totalling($amount)
{
return $this->setTotalAmountConfirmation($amount);
}

/**
* @param $amount
*
* @return Refund
*/
public function setAmount($amount)
{
$this->amount = $amount;

return $this;
}

/**
* @param $currency
*
* @return Refund
*/
public function setCurrency($currency)
{
$this->currency = $currency;

return $this;
}

/**
* @param $amount
*
* @return Refund
*/
public function setTotalAmountConfirmation($amount)
{
$this->total_amount_confirmation = $amount;

return $this;
}

/**
* @param $reference
*
* @return Refund
*/
public function setReference($reference)
{
$this->reference = $reference;

return $this;
}

/**
* @param Payment $payment
*
* @return Refund
*/
public function setPayment(Payment $payment)
{
$this->payment = $payment;

return $this;
}

/**
* @return int
*/
public function getAmount()
{
return intval($this->amount);
}

/**
* @return int
*/
public function getTotalAmountConfirmation()
{
return intval($this->total_amount_confirmation);
}

/**
* @return string
*/
public function getCurrency()
{
return $this->currency;
}

/**
* @return string
*/
public function getReference()
{
return $this->reference;
}

/**
* @return array
*/
public function toArray()
{
$refund = array_filter(get_object_vars($this));

if ($this->payment instanceof Payment) {
unset($refund['payment']);
$refund['links']['payment'] = $this->payment->getId();
}

return $refund;
}
}
45 changes: 45 additions & 0 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use GoCardless\Pro\Models\CustomerBankAccount;
use GoCardless\Pro\Models\Mandate;
use GoCardless\Pro\Models\Payment;
use GoCardless\Pro\Models\Refund;
use GuzzleHttp\Client;

class ApiTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -369,6 +370,35 @@ function test_it_can_get_a_single_payment(Payment $old)
return $new;
}

/** @test */
function it_returns_a_list_of_payments()
{
$payments = $this->api->listPayments();

$this->assertInternalType('array', $payments);
foreach ($payments as $payment) {
$this->assertInstanceOf('GoCardless\Pro\Models\Payment', $payment);
}
}

function test_it_can_create_a_refund()
{
$this->guardAgainstInvalidPaymentToRefund();

$config = require __DIR__ . '/../config.php';

$payment = $this->api->getPayment($config['paymentToRefund']);
$refund = (new Refund())->of($payment)->returning($payment->getAmount())->totalling($payment->getAmount());

$refund = $this->api->createRefund($refund);

$this->assertInstanceOf('GoCardless\Pro\Models\Refund', $refund);
$this->assertNotNull($refund->getId());
$this->assertNotNull($refund->getCreatedAt());
$this->assertSame($payment->getAmount(), $refund->getAmount());
$this->assertSame('GBP', $refund->getCurrency());
}

/** @depends test_it_can_get_a_single_payment */
function test_it_can_cancel_payments(Payment $payment)
{
Expand Down Expand Up @@ -544,4 +574,19 @@ private function guardAgainstSmallNumberOfMandates()
$this->markTestSkipped('Skipping test due to lack of mandates in system. This test requires at least 5.');
}
}

private function guardAgainstInvalidPaymentToRefund()
{
$config = require __DIR__ . '/../config.php';

if ( ! isset($config['paymentToRefund']) or $config['paymentToRefund'] == '') {
$this->markTestSkipped('Skipping test due to [paymentToRefund] was not set in the config');
}

$payment = $this->api->getPayment($config['paymentToRefund']);

if ( ! $payment->isConfirmed() or ! $payment->isPaidOut()) {
$this->markTestSkipped('Skipping test due to payment status is not confirmed or paid_out');
}
}
}
Loading

0 comments on commit d966c97

Please sign in to comment.