diff --git a/demo.php b/demo.php index 45076c5..30797f5 100644 --- a/demo.php +++ b/demo.php @@ -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; @@ -331,6 +332,21 @@ dump($orders->track($args['id'], $args['carrier'], $args['tracking-number'], $args['capture-id'], false)); }); +$router->add('payments get authorized ', 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 ', 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 ', 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()); }); diff --git a/src/Payments.php b/src/Payments.php new file mode 100644 index 0000000..23e392d --- /dev/null +++ b/src/Payments.php @@ -0,0 +1,120 @@ +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); + } +} diff --git a/tests/PaymentsTest.php b/tests/PaymentsTest.php new file mode 100644 index 0000000..9429ec1 --- /dev/null +++ b/tests/PaymentsTest.php @@ -0,0 +1,96 @@ +setResponse(new Response(200, file_get_contents(__DIR__ . '/fixtures/PaymentGetAuthorized.json'))); + + $id = '30L74699WY872124E'; + + self::$payments->getAuthorized($id); + + $expected = <<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 = <<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 = <<dumpRequest()); + } +} diff --git a/tests/fixtures/PaymentGetAuthorized.json b/tests/fixtures/PaymentGetAuthorized.json new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/PaymentGetCaptured.json b/tests/fixtures/PaymentGetCaptured.json new file mode 100644 index 0000000..fd86a2b --- /dev/null +++ b/tests/fixtures/PaymentGetCaptured.json @@ -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" + } + ] +} diff --git a/tests/fixtures/PaymentGetRefunded.json b/tests/fixtures/PaymentGetRefunded.json new file mode 100644 index 0000000..8961eb8 --- /dev/null +++ b/tests/fixtures/PaymentGetRefunded.json @@ -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" + } + ] +}