From 5a820d351adf72ca7ca328f5cc0dd96943a23da9 Mon Sep 17 00:00:00 2001 From: DDEV-Local User Date: Wed, 16 Dec 2020 06:44:04 +0000 Subject: [PATCH 1/2] Add getProcessorFeeAmount and update tests/sample data. --- src/Message/RestResponse.php | 27 +++++++++++++++++++++++++++ tests/Message/RestResponseTest.php | 4 ++++ tests/Mock/RestPurchaseSuccess.txt | 2 +- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Message/RestResponse.php b/src/Message/RestResponse.php index 4ffdbe9b..9849974d 100644 --- a/src/Message/RestResponse.php +++ b/src/Message/RestResponse.php @@ -45,6 +45,33 @@ public function getTransactionReference() return null; } + /* + * The fee taken by PayPal for the transaction. Available from the Sale + * object in RelatedResources only once a transaction has been completed + * and funds have been received by merchant. + * + * https://developer.paypal.com/docs/api/payments/v1/#definition-sale + * + * + * There may be a 'fee' field associated with amount details object but + * this seems to pertain to a handling_fee charged to client rather than + * the PayPal processing fee. + * + * https://developer.paypal.com/docs/api/payments/v1/#definition-details + */ + public function getProcessorFeeAmount() + { + if (!empty($this->data['transactions']) && + !empty($this->data['transactions'][0]['related_resources']) && + !empty($this->data['transactions'][0]['related_resources'][0]['sale']) && + !empty($this->data['transactions'][0]['related_resources'][0]['sale']['transaction_fee'])) + { + return $this->data['transactions'][0]['related_resources'][0]['sale']['transaction_fee']['value']; + } + + return null; + } + public function getMessage() { if (isset($this->data['error_description'])) { diff --git a/tests/Message/RestResponseTest.php b/tests/Message/RestResponseTest.php index ce852bb8..f583d9a8 100644 --- a/tests/Message/RestResponseTest.php +++ b/tests/Message/RestResponseTest.php @@ -14,6 +14,7 @@ public function testPurchaseSuccess() $this->assertTrue($response->isSuccessful()); $this->assertSame('44E89981F8714392Y', $response->getTransactionReference()); + $this->assertSame("0.50", $response->getProcessorFeeAmount()); $this->assertNull($response->getMessage()); } @@ -25,6 +26,7 @@ public function testPurchaseFailure() $this->assertFalse($response->isSuccessful()); $this->assertNull($response->getTransactionReference()); + $this->assertNull($response->getProcessorFeeAmount()); $this->assertSame('Invalid request - see details', $response->getMessage()); } @@ -37,6 +39,7 @@ public function testCompletePurchaseSuccess() $this->assertTrue($response->isSuccessful()); $this->assertSame('9EA05739TH369572R', $response->getTransactionReference()); + $this->assertNull($response->getProcessorFeeAmount()); $this->assertNull($response->getMessage()); } @@ -72,6 +75,7 @@ public function testAuthorizeSuccess() $this->assertTrue($response->isSuccessful()); $this->assertSame('58N7596879166930B', $response->getTransactionReference()); + $this->assertNull($response->getProcessorFeeAmount()); $this->assertNull($response->getMessage()); } diff --git a/tests/Mock/RestPurchaseSuccess.txt b/tests/Mock/RestPurchaseSuccess.txt index 0e43e2f7..d12703c8 100644 --- a/tests/Mock/RestPurchaseSuccess.txt +++ b/tests/Mock/RestPurchaseSuccess.txt @@ -9,4 +9,4 @@ Date: Thu, 03 Jul 2014 14:11:10 GMT Content-Type: application/json Content-Length: 1243 -{"id":"PAY-6RT04683U7444573DKO2WI6A","create_time":"2014-07-03T14:11:04Z","update_time":"2014-07-03T14:11:10Z","state":"approved","intent":"sale","payer":{"payment_method":"credit_card","funding_instruments":[{"credit_card":{"type":"mastercard","number":"xxxxxxxxxxxx5559","expire_month":"12","expire_year":"2018","first_name":"Betsy","last_name":"Buyer"}}]},"transactions":[{"amount":{"total":"7.47","currency":"USD","details":{"subtotal":"7.47"}},"description":"This is the payment transaction description.","related_resources":[{"sale":{"id":"44E89981F8714392Y","create_time":"2014-07-03T14:11:04Z","update_time":"2014-07-03T14:11:10Z","state":"completed","amount":{"total":"7.47","currency":"USD"},"parent_payment":"PAY-6RT04683U7444573DKO2WI6A","links":[{"href":"https://api.sandbox.paypal.com/v1/payments/sale/44E89981F8714392Y","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/payments/sale/44E89981F8714392Y/refund","rel":"refund","method":"POST"},{"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-6RT04683U7444573DKO2WI6A","rel":"parent_payment","method":"GET"}]}}]}],"links":[{"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-6RT04683U7444573DKO2WI6A","rel":"self","method":"GET"}]} +{"id":"PAY-6RT04683U7444573DKO2WI6A","create_time":"2014-07-03T14:11:04Z","update_time":"2014-07-03T14:11:10Z","state":"approved","intent":"sale","payer":{"payment_method":"credit_card","funding_instruments":[{"credit_card":{"type":"mastercard","number":"xxxxxxxxxxxx5559","expire_month":"12","expire_year":"2018","first_name":"Betsy","last_name":"Buyer"}}]},"transactions":[{"amount":{"total":"7.47","currency":"USD","details":{"subtotal":"7.47"}},"description":"This is the payment transaction description.","related_resources":[{"sale":{"id":"44E89981F8714392Y","create_time":"2014-07-03T14:11:04Z","update_time":"2014-07-03T14:11:10Z","state":"completed","amount":{"total":"7.47","currency":"USD"},"transaction_fee":{"value":"0.50","currency":"USD"},"parent_payment":"PAY-6RT04683U7444573DKO2WI6A","links":[{"href":"https://api.sandbox.paypal.com/v1/payments/sale/44E89981F8714392Y","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/payments/sale/44E89981F8714392Y/refund","rel":"refund","method":"POST"},{"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-6RT04683U7444573DKO2WI6A","rel":"parent_payment","method":"GET"}]}}]}],"links":[{"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-6RT04683U7444573DKO2WI6A","rel":"self","method":"GET"}]} From d0c70017a2cae3addd56d745383637ea21847bc6 Mon Sep 17 00:00:00 2001 From: Ana Nelson Date: Tue, 15 Dec 2020 23:00:22 -0800 Subject: [PATCH 2/2] Fix formatting. --- src/Message/RestResponse.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Message/RestResponse.php b/src/Message/RestResponse.php index 9849974d..ea5530eb 100644 --- a/src/Message/RestResponse.php +++ b/src/Message/RestResponse.php @@ -64,8 +64,7 @@ public function getProcessorFeeAmount() if (!empty($this->data['transactions']) && !empty($this->data['transactions'][0]['related_resources']) && !empty($this->data['transactions'][0]['related_resources'][0]['sale']) && - !empty($this->data['transactions'][0]['related_resources'][0]['sale']['transaction_fee'])) - { + !empty($this->data['transactions'][0]['related_resources'][0]['sale']['transaction_fee'])) { return $this->data['transactions'][0]['related_resources'][0]['sale']['transaction_fee']['value']; }