Skip to content

Commit

Permalink
Add subscription list transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
8ctopus committed Oct 28, 2024
1 parent cd9ecd3 commit cec554a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
6 changes: 5 additions & 1 deletion demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@
dump($subscriptions->get($args['id']));
});

$router->add('subscriptions list transactions <id>', static function (array $args) use ($sandbox, $handler, $auth) : void {
$subscriptions = new Subscriptions($sandbox, $handler, $auth);
dump($subscriptions->listTransactions($args['id']));
});

$router->add('subscriptions create <plan-id> <success-url> <cancel-url>', static function (array $args) use ($sandbox, $handler, $auth) : void {
$subscriptions = new Subscriptions($sandbox, $handler, $auth);

Expand All @@ -161,7 +166,6 @@
}

dump($response);

});

$router->add('subscriptions capture <id> <currency> <amount> <note>', static function (array $args) use ($sandbox, $handler, $auth) : void {
Expand Down
21 changes: 21 additions & 0 deletions src/Subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,25 @@ public function activate(string $id) : self

return $this;
}

/**
* List subscription transactions
*
* @param string $id
*
* @return array<mixed>
*/
public function listTransactions(string $id) : array
{
$url = "/v1/billing/subscriptions/{$id}/transactions";

$url .= '?' . http_build_query([
'start_time' => '2018-01-21T07:50:20.940Z',
'end_time' => '2030-01-21T07:50:20.940Z',
]);

$response = $this->sendRequest('GET', $url, [], null, 200);

return json_decode($response, true);
}
}
21 changes: 20 additions & 1 deletion tests/SubscriptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function testCapture() : void

$id = 'I-MT4EHFSKC1U4';

self::$subscriptions->capture($id, 'USD', 1, 'plan payment');
self::$subscriptions->capture($id, 'USD', 1.0, 'plan payment');

$expected = <<<TEXT
https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}/capture
Expand Down Expand Up @@ -148,4 +148,23 @@ public function testCancel() : void

self::assertSame($expected, self::$handler->dumpRequest());
}

public function testListTransactions() : void
{
self::$handler->setResponse(new Response(200, file_get_contents(__DIR__ . '/fixtures/SubscriptionListTransactions.json')));

$id = 'I-BW452GLLEP1G';

self::$subscriptions->listTransactions($id);

$expected = <<<TEXT
https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}/transactions?start_time=2018-01-21T07%3A50%3A20.940Z&end_time=2030-01-21T07%3A50%3A20.940Z
Host: api-m.sandbox.paypal.com
Authorization: Bearer test
Content-Type: application/json
TEXT;

self::assertSame($expected, self::$handler->dumpRequest());
}
}
35 changes: 35 additions & 0 deletions tests/fixtures/SubscriptionListTransactions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"transactions": [
{
"status": "COMPLETED",
"id": "0K248413N6360881G",
"amount_with_breakdown": {
"gross_amount": {
"currency_code": "USD",
"value": "19.99"
},
"fee_amount": {
"currency_code": "USD",
"value": "0.98"
},
"net_amount": {
"currency_code": "USD",
"value": "19.01"
}
},
"payer_name": {
"given_name": "John",
"surname": "Doe"
},
"payer_email": "sb-0479eu30918010@personal.example.com",
"time": "2024-10-24T10:36:06.000Z"
}
],
"links": [
{
"href": "https:\/\/api-m.sandbox.paypal.com\/v1\/billing\/subscriptions\/I-1S3UVF7WT1G2\/transactions?start_time=2018-01-21T07%3A50%3A20.940Z&end_time=2025-01-21T07%3A50%3A20.940Z",
"rel": "SELF",
"method": "GET"
}
]
}

0 comments on commit cec554a

Please sign in to comment.