Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added getProcessorFeeAmount() to return paypal fees when available #245

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Message/RestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ 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'])) {
Expand Down
4 changes: 4 additions & 0 deletions tests/Message/RestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -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());
}

Expand All @@ -37,6 +39,7 @@ public function testCompletePurchaseSuccess()

$this->assertTrue($response->isSuccessful());
$this->assertSame('9EA05739TH369572R', $response->getTransactionReference());
$this->assertNull($response->getProcessorFeeAmount());
$this->assertNull($response->getMessage());
}

Expand Down Expand Up @@ -72,6 +75,7 @@ public function testAuthorizeSuccess()

$this->assertTrue($response->isSuccessful());
$this->assertSame('58N7596879166930B', $response->getTransactionReference());
$this->assertNull($response->getProcessorFeeAmount());
$this->assertNull($response->getMessage());
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Mock/RestPurchaseSuccess.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"}]}