Skip to content

Commit

Permalink
Add payments api base
Browse files Browse the repository at this point in the history
  • Loading branch information
8ctopus committed Oct 28, 2024
1 parent cec554a commit 72605a4
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 0 deletions.
16 changes: 16 additions & 0 deletions demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Oct8pus\PayPal\OAuthCache;
use Oct8pus\PayPal\Orders;
use Oct8pus\PayPal\Orders\Intent;
use Oct8pus\PayPal\Payments;
use Oct8pus\PayPal\Plans;
use Oct8pus\PayPal\Plans\BillingCycle;
use Oct8pus\PayPal\Plans\BillingCycles;
Expand Down Expand Up @@ -331,6 +332,21 @@
dump($orders->track($args['id'], $args['carrier'], $args['tracking-number'], $args['capture-id'], false));
});

$router->add('payments get authorized <id>', static function (array $args) use ($sandbox, $handler, $auth) : void {
$payments = new Payments($sandbox, $handler, $auth);
dump($payments->getAuthorized($args['id']));
});

$router->add('payments get captured <id>', static function (array $args) use ($sandbox, $handler, $auth) : void {
$payments = new Payments($sandbox, $handler, $auth);
dump($payments->getCaptured($args['id']));
});

$router->add('payments get refunded <id>', static function (array $args) use ($sandbox, $handler, $auth) : void {
$payments = new Payments($sandbox, $handler, $auth);
dump($payments->getRefunded($args['id']));
});

$router->add('auth token', static function () use ($auth) : void {
dump($auth->token());
});
Expand Down
120 changes: 120 additions & 0 deletions src/Payments.php
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);
}
}
96 changes: 96 additions & 0 deletions tests/PaymentsTest.php
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.
51 changes: 51 additions & 0 deletions tests/fixtures/PaymentGetCaptured.json
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"
}
]
}
44 changes: 44 additions & 0 deletions tests/fixtures/PaymentGetRefunded.json
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"
}
]
}

0 comments on commit 72605a4

Please sign in to comment.