-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
327 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
/** | ||
* https://developer.paypal.com/docs/api/payments/v2/ | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Oct8pus\PayPal; | ||
|
||
class Payments extends RestBase | ||
{ | ||
/** | ||
* Constructor | ||
* | ||
* @param bool $sandbox | ||
* @param HttpHandler $handler | ||
* @param OAuth $auth | ||
*/ | ||
public function __construct(bool $sandbox, HttpHandler $handler, OAuth $auth) | ||
{ | ||
parent::__construct($sandbox, $handler, $auth); | ||
} | ||
|
||
/** | ||
* Get info for authorized payment | ||
* | ||
* @param string $authorizationId | ||
* | ||
* @return array | ||
*/ | ||
public function getAuthorized(string $authorizationId) : array | ||
{ | ||
$url = "/v2/payments/authorizations/{$authorizationId}"; | ||
|
||
$response = $this->sendRequest('GET', $url, [], null, 200); | ||
|
||
return json_decode($response, true); | ||
} | ||
|
||
/** | ||
* Get info for captured payment | ||
* | ||
* @param string $captureId | ||
* | ||
* @return array | ||
*/ | ||
public function getCaptured(string $captureId) : array | ||
{ | ||
$url = "/v2/payments/captures/{$captureId}"; | ||
|
||
$response = $this->sendRequest('GET', $url, [], null, 200); | ||
|
||
return json_decode($response, true); | ||
} | ||
|
||
/** | ||
* Get info for refunded payment | ||
* | ||
* @param string $refundId | ||
* | ||
* @return array | ||
*/ | ||
public function getRefunded(string $refundId) : array | ||
{ | ||
$url = "/v2/payments/refunds/{$refundId}"; | ||
|
||
$response = $this->sendRequest('GET', $url, [], null, 200); | ||
|
||
return json_decode($response, true); | ||
} | ||
|
||
/** | ||
* Capture authorized payment | ||
* | ||
* @param string $id authorization id | ||
* | ||
* @return array | ||
*/ | ||
public function capture(string $id) : array | ||
{ | ||
$url = "/v2/payments/authorizations/{$id}/capture"; | ||
|
||
$response = $this->sendRequest('POST', $url, [], null, 201); | ||
|
||
return json_decode($response, true); | ||
} | ||
|
||
/** | ||
* Refund captured payment | ||
* | ||
* @param string $captureId | ||
* | ||
* @return array | ||
*/ | ||
public function refund(string $captureId) : array | ||
{ | ||
$url = "/v2/payments/captures/{$captureId}/refund"; | ||
|
||
$response = $this->sendRequest('POST', $url, [], null, 201); | ||
|
||
return json_decode($response, true); | ||
} | ||
|
||
/** | ||
* Void authorized payment | ||
* | ||
* @param string $authorizationId | ||
* | ||
* @return array | ||
*/ | ||
public function void(string $authorizationId) : array | ||
{ | ||
$url = "/v2/payments/authorizations/{$authorizationId}/void"; | ||
|
||
$response = $this->sendRequest('POST', $url, [], null, 201); | ||
|
||
return json_decode($response, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests; | ||
|
||
use Nimbly\Capsule\Factory\RequestFactory; | ||
use Nimbly\Capsule\Factory\StreamFactory; | ||
use Nimbly\Capsule\Response; | ||
use Nimbly\Shuttle\Shuttle; | ||
use Oct8pus\PayPal\Payments; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Oct8pus\PayPal\Payments | ||
*/ | ||
final class PaymentsTest extends TestCase | ||
{ | ||
private static HttpHandlerMock $handler; | ||
private static OAuthMock $auth; | ||
private static Payments $payments; | ||
|
||
public static function setUpBeforeClass() : void | ||
{ | ||
self::$handler = new HttpHandlerMock(new Shuttle(), new RequestFactory(), new StreamFactory()); | ||
self::$auth = new OAuthMock(self::$handler, 'testId', 'testSecret'); | ||
self::$payments = new Payments(true, self::$handler, self::$auth); | ||
} | ||
|
||
public function testConstructor() : void | ||
{ | ||
self::assertInstanceOf(Payments::class, new Payments(true, self::$handler, self::$auth)); | ||
} | ||
|
||
/* | ||
// FIX ME - add sample json | ||
public function testGetAuthorized() : void | ||
{ | ||
self::$handler->setResponse(new Response(200, file_get_contents(__DIR__ . '/fixtures/PaymentGetAuthorized.json'))); | ||
$id = '30L74699WY872124E'; | ||
self::$payments->getAuthorized($id); | ||
$expected = <<<TEXT | ||
https://api-m.sandbox.paypal.com/v2/payments/authorizations/{$id} | ||
Host: api-m.sandbox.paypal.com | ||
Authorization: Bearer test | ||
Content-Type: application/json | ||
TEXT; | ||
self::assertSame($expected, self::$handler->dumpRequest()); | ||
} | ||
*/ | ||
|
||
public function testGetCaptured() : void | ||
{ | ||
self::$handler->setResponse(new Response(200, file_get_contents(__DIR__ . '/fixtures/PaymentGetCaptured.json'))); | ||
|
||
$id = '8FJ94262P5616910T'; | ||
|
||
self::$payments->getCaptured($id); | ||
|
||
$expected = <<<TEXT | ||
https://api-m.sandbox.paypal.com/v2/payments/captures/{$id} | ||
Host: api-m.sandbox.paypal.com | ||
Authorization: Bearer test | ||
Content-Type: application/json | ||
TEXT; | ||
|
||
self::assertSame($expected, self::$handler->dumpRequest()); | ||
} | ||
|
||
public function testGetRefunded() : void | ||
{ | ||
self::$handler->setResponse(new Response(200, file_get_contents(__DIR__ . '/fixtures/PaymentGetRefunded.json'))); | ||
|
||
$id = '1HG7183619108511N'; | ||
|
||
self::$payments->getRefunded($id); | ||
|
||
$expected = <<<TEXT | ||
https://api-m.sandbox.paypal.com/v2/payments/refunds/{$id} | ||
Host: api-m.sandbox.paypal.com | ||
Authorization: Bearer test | ||
Content-Type: application/json | ||
TEXT; | ||
|
||
self::assertSame($expected, self::$handler->dumpRequest()); | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
"id": "23046316AY790152G", | ||
"amount": { | ||
"currency_code": "USD", | ||
"value": "3.00" | ||
}, | ||
"final_capture": true, | ||
"seller_protection": { | ||
"status": "ELIGIBLE", | ||
"dispute_categories": [ | ||
"ITEM_NOT_RECEIVED", | ||
"UNAUTHORIZED_TRANSACTION" | ||
] | ||
}, | ||
"seller_receivable_breakdown": { | ||
"gross_amount": { | ||
"currency_code": "USD", | ||
"value": "3.00" | ||
}, | ||
"paypal_fee": { | ||
"currency_code": "USD", | ||
"value": "0.42" | ||
}, | ||
"net_amount": { | ||
"currency_code": "USD", | ||
"value": "2.58" | ||
} | ||
}, | ||
"status": "COMPLETED", | ||
"supplementary_data": { | ||
"related_ids": [] | ||
}, | ||
"payee": { | ||
"email_address": "sb-33yxa30918706@business.example.com", | ||
"merchant_id": "XJR76XKQFKKDE" | ||
}, | ||
"create_time": "2024-10-24T13:14:22Z", | ||
"update_time": "2024-10-24T13:14:22Z", | ||
"links": [ | ||
{ | ||
"href": "https:\/\/api.sandbox.paypal.com\/v2\/payments\/captures\/23046316AY790152G", | ||
"rel": "self", | ||
"method": "GET" | ||
}, | ||
{ | ||
"href": "https:\/\/api.sandbox.paypal.com\/v2\/payments\/captures\/23046316AY790152G\/refund", | ||
"rel": "refund", | ||
"method": "POST" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"id": "1HG7183619108511N", | ||
"amount": { | ||
"currency_code": "USD", | ||
"value": "3.00" | ||
}, | ||
"seller_payable_breakdown": { | ||
"gross_amount": { | ||
"currency_code": "USD", | ||
"value": "3.00" | ||
}, | ||
"paypal_fee": { | ||
"currency_code": "USD", | ||
"value": "0.42" | ||
}, | ||
"net_amount": { | ||
"currency_code": "USD", | ||
"value": "2.58" | ||
}, | ||
"total_refunded_amount": { | ||
"currency_code": "USD", | ||
"value": "3.00" | ||
} | ||
}, | ||
"status": "COMPLETED", | ||
"create_time": "2024-10-27T23:01:01-07:00", | ||
"update_time": "2024-10-27T23:01:01-07:00", | ||
"payer": { | ||
"email_address": "sb-33yxa30918706@business.example.com", | ||
"merchant_id": "XJR76XKQFKKDE" | ||
}, | ||
"links": [ | ||
{ | ||
"href": "https:\/\/api.sandbox.paypal.com\/v2\/payments\/refunds\/1HG7183619108511N", | ||
"rel": "self", | ||
"method": "GET" | ||
}, | ||
{ | ||
"href": "https:\/\/api.sandbox.paypal.com\/v2\/payments\/captures\/8FJ94262P5616910T", | ||
"rel": "up", | ||
"method": "GET" | ||
} | ||
] | ||
} |