Skip to content

Commit 942d616

Browse files
authored
Merge pull request #121 from fraterblack/master
Implemented RestRefundCaptureRequest to refund captured payments. And RestVoidRequest to void a previously authorized payments
2 parents 4608c41 + 555f473 commit 942d616

File tree

4 files changed

+126
-1
lines changed

4 files changed

+126
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* PayPal REST Refund Captured Payment Request
4+
*/
5+
6+
namespace Omnipay\PayPal\Message;
7+
8+
/**
9+
* PayPal REST Refund Captured Payment Request
10+
*
11+
* Use this call to refund a captured payment.
12+
*
13+
* @link https://developer.paypal.com/docs/api/#refund-a-captured-payment
14+
* @see RestAuthorizeRequest
15+
* @see RestCaptureRequest
16+
*/
17+
class RestRefundCaptureRequest extends AbstractRestRequest
18+
{
19+
public function getData()
20+
{
21+
$this->validate('transactionReference');
22+
23+
return array(
24+
'amount' => array(
25+
'currency' => $this->getCurrency(),
26+
'total' => $this->getAmount(),
27+
),
28+
'description' => $this->getDescription(),
29+
);
30+
}
31+
32+
public function getEndpoint()
33+
{
34+
return parent::getEndpoint() . '/payments/capture/' . $this->getTransactionReference() . '/refund';
35+
}
36+
}

src/Message/RestVoidRequest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* PayPal REST Void an authorization
4+
*/
5+
6+
namespace Omnipay\PayPal\Message;
7+
8+
/**
9+
* PayPal REST Void an authorization
10+
*
11+
* Use this call to void a previously authorized payment.
12+
* Note: A fully captured authorization cannot be voided.
13+
*
14+
* @link https://developer.paypal.com/docs/api/#void-an-authorization
15+
* @see RestAuthorizeRequest
16+
*/
17+
class RestVoidRequest extends AbstractRestRequest
18+
{
19+
public function getData()
20+
{
21+
$this->validate('transactionReference');
22+
}
23+
24+
public function getEndpoint()
25+
{
26+
return parent::getEndpoint() . '/payments/authorization/' . $this->getTransactionReference() . '/void';
27+
}
28+
}

src/RestGateway.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,20 @@ public function authorize(array $parameters = array())
441441
return $this->createRequest('\Omnipay\PayPal\Message\RestAuthorizeRequest', $parameters);
442442
}
443443

444+
/**
445+
* Void an authorization.
446+
*
447+
* To to void a previously authorized payment.
448+
*
449+
* @link https://developer.paypal.com/docs/api/#void-an-authorization
450+
* @param array $parameters
451+
* @return \Omnipay\PayPal\Message\RestVoidRequest
452+
*/
453+
public function void(array $parameters = array())
454+
{
455+
return $this->createRequest('\Omnipay\PayPal\Message\RestVoidRequest', $parameters);
456+
}
457+
444458
/**
445459
* Capture an authorization.
446460
*
@@ -458,7 +472,20 @@ public function capture(array $parameters = array())
458472
}
459473

460474
// TODO: Authorizations with payment_method == paypal.
461-
// TODO: Look up and refund captured payments.
475+
476+
/**
477+
* Refund a Captured Payment
478+
*
479+
* To refund captured payments (authorization transaction) created by a authorize request.
480+
*
481+
* @link https://developer.paypal.com/docs/api/#refund-a-captured-payment
482+
* @param array $parameters
483+
* @return \Omnipay\PayPal\Message\RestRefundCaptureRequest
484+
*/
485+
public function refundCapture(array $parameters = array())
486+
{
487+
return $this->createRequest('\Omnipay\PayPal\Message\RestRefundCaptureRequest', $parameters);
488+
}
462489

463490
//
464491
// Sale Transactions -- Get and refund completed payments (sale transactions).

tests/RestGatewayTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,38 @@ public function testReactivateSubscription()
245245
$this->assertTrue($response->isSuccessful());
246246
$this->assertNull($response->getMessage());
247247
}
248+
249+
public function testRefundCapture()
250+
{
251+
$request = $this->gateway->refundCapture(array(
252+
'transactionReference' => 'abc123'
253+
));
254+
255+
$this->assertInstanceOf('\Omnipay\PayPal\Message\RestRefundCaptureRequest', $request);
256+
$this->assertSame('abc123', $request->getTransactionReference());
257+
$endPoint = $request->getEndpoint();
258+
$this->assertSame('https://api.paypal.com/v1/payments/capture/abc123/refund', $endPoint);
259+
260+
$request->setAmount('15.99');
261+
$request->setCurrency('BRL');
262+
$request->setDescription('Test Description');
263+
$data = $request->getData();
264+
// we're expecting an empty object here
265+
$json = json_encode($data);
266+
$this->assertEquals('{"amount":{"currency":"BRL","total":"15.99"},"description":"Test Description"}', $json);
267+
}
268+
269+
public function testVoid()
270+
{
271+
$request = $this->gateway->void(array(
272+
'transactionReference' => 'abc123'
273+
));
274+
275+
$this->assertInstanceOf('\Omnipay\PayPal\Message\RestVoidRequest', $request);
276+
$this->assertSame('abc123', $request->getTransactionReference());
277+
$endPoint = $request->getEndpoint();
278+
$this->assertSame('https://api.paypal.com/v1/payments/authorization/abc123/void', $endPoint);
279+
$data = $request->getData();
280+
$this->assertEmpty($data);
281+
}
248282
}

0 commit comments

Comments
 (0)