Skip to content

Commit

Permalink
Merge pull request #121 from fraterblack/master
Browse files Browse the repository at this point in the history
Implemented RestRefundCaptureRequest to refund captured payments. And RestVoidRequest to void a previously authorized payments
  • Loading branch information
delatbabel authored Oct 11, 2016
2 parents 4608c41 + 555f473 commit 942d616
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/Message/RestRefundCaptureRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* PayPal REST Refund Captured Payment Request
*/

namespace Omnipay\PayPal\Message;

/**
* PayPal REST Refund Captured Payment Request
*
* Use this call to refund a captured payment.
*
* @link https://developer.paypal.com/docs/api/#refund-a-captured-payment
* @see RestAuthorizeRequest
* @see RestCaptureRequest
*/
class RestRefundCaptureRequest extends AbstractRestRequest
{
public function getData()
{
$this->validate('transactionReference');

return array(
'amount' => array(
'currency' => $this->getCurrency(),
'total' => $this->getAmount(),
),
'description' => $this->getDescription(),
);
}

public function getEndpoint()
{
return parent::getEndpoint() . '/payments/capture/' . $this->getTransactionReference() . '/refund';
}
}
28 changes: 28 additions & 0 deletions src/Message/RestVoidRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* PayPal REST Void an authorization
*/

namespace Omnipay\PayPal\Message;

/**
* PayPal REST Void an authorization
*
* Use this call to void a previously authorized payment.
* Note: A fully captured authorization cannot be voided.
*
* @link https://developer.paypal.com/docs/api/#void-an-authorization
* @see RestAuthorizeRequest
*/
class RestVoidRequest extends AbstractRestRequest
{
public function getData()
{
$this->validate('transactionReference');
}

public function getEndpoint()
{
return parent::getEndpoint() . '/payments/authorization/' . $this->getTransactionReference() . '/void';
}
}
29 changes: 28 additions & 1 deletion src/RestGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,20 @@ public function authorize(array $parameters = array())
return $this->createRequest('\Omnipay\PayPal\Message\RestAuthorizeRequest', $parameters);
}

/**
* Void an authorization.
*
* To to void a previously authorized payment.
*
* @link https://developer.paypal.com/docs/api/#void-an-authorization
* @param array $parameters
* @return \Omnipay\PayPal\Message\RestVoidRequest
*/
public function void(array $parameters = array())
{
return $this->createRequest('\Omnipay\PayPal\Message\RestVoidRequest', $parameters);
}

/**
* Capture an authorization.
*
Expand All @@ -458,7 +472,20 @@ public function capture(array $parameters = array())
}

// TODO: Authorizations with payment_method == paypal.
// TODO: Look up and refund captured payments.

/**
* Refund a Captured Payment
*
* To refund captured payments (authorization transaction) created by a authorize request.
*
* @link https://developer.paypal.com/docs/api/#refund-a-captured-payment
* @param array $parameters
* @return \Omnipay\PayPal\Message\RestRefundCaptureRequest
*/
public function refundCapture(array $parameters = array())
{
return $this->createRequest('\Omnipay\PayPal\Message\RestRefundCaptureRequest', $parameters);
}

//
// Sale Transactions -- Get and refund completed payments (sale transactions).
Expand Down
34 changes: 34 additions & 0 deletions tests/RestGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,38 @@ public function testReactivateSubscription()
$this->assertTrue($response->isSuccessful());
$this->assertNull($response->getMessage());
}

public function testRefundCapture()
{
$request = $this->gateway->refundCapture(array(
'transactionReference' => 'abc123'
));

$this->assertInstanceOf('\Omnipay\PayPal\Message\RestRefundCaptureRequest', $request);
$this->assertSame('abc123', $request->getTransactionReference());
$endPoint = $request->getEndpoint();
$this->assertSame('https://api.paypal.com/v1/payments/capture/abc123/refund', $endPoint);

$request->setAmount('15.99');
$request->setCurrency('BRL');
$request->setDescription('Test Description');
$data = $request->getData();
// we're expecting an empty object here
$json = json_encode($data);
$this->assertEquals('{"amount":{"currency":"BRL","total":"15.99"},"description":"Test Description"}', $json);
}

public function testVoid()
{
$request = $this->gateway->void(array(
'transactionReference' => 'abc123'
));

$this->assertInstanceOf('\Omnipay\PayPal\Message\RestVoidRequest', $request);
$this->assertSame('abc123', $request->getTransactionReference());
$endPoint = $request->getEndpoint();
$this->assertSame('https://api.paypal.com/v1/payments/authorization/abc123/void', $endPoint);
$data = $request->getData();
$this->assertEmpty($data);
}
}

0 comments on commit 942d616

Please sign in to comment.