From c8959b6ed48647035c1fbbdc671d6ed65145128b Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Date: Sat, 14 Sep 2019 16:37:32 +1000 Subject: [PATCH] Added support for Connect Application Fees. --- src/Gateway.php | 10 ++++ src/Message/FetchApplicationFeeRequest.php | 69 ++++++++++++++++++++++ src/Message/Response.php | 17 ++++++ tests/Message/FetchApplicationFeeTest.php | 43 ++++++++++++++ tests/Mock/FetchApplicationFeeFailure.txt | 17 ++++++ tests/Mock/FetchApplicationFeeSuccess.txt | 32 ++++++++++ 6 files changed, 188 insertions(+) create mode 100644 src/Message/FetchApplicationFeeRequest.php create mode 100644 tests/Message/FetchApplicationFeeTest.php create mode 100644 tests/Mock/FetchApplicationFeeFailure.txt create mode 100644 tests/Mock/FetchApplicationFeeSuccess.txt diff --git a/src/Gateway.php b/src/Gateway.php index 0ed96fae..c1b91423 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -338,4 +338,14 @@ public function completePurchase(array $parameters = array()) { return $this->createRequest('\Omnipay\Stripe\Message\CompletePurchaseRequest', $parameters); } + + /** + * @param array $parameters + * + * @return \Omnipay\Stripe\Message\FetchApplicationFeeRequest + */ + public function fetchApplicationFee(array $parameters = array()) + { + return $this->createRequest('\Omnipay\Stripe\Message\FetchApplicationFeeRequest', $parameters); + } } diff --git a/src/Message/FetchApplicationFeeRequest.php b/src/Message/FetchApplicationFeeRequest.php new file mode 100644 index 00000000..0f708974 --- /dev/null +++ b/src/Message/FetchApplicationFeeRequest.php @@ -0,0 +1,69 @@ + + * // Fetch the transaction so that details can be found for refund, etc. + * $transaction = $gateway->fetchApplicationFee(); + * $transaction->setApplicationFeeReference($application_fee_id); + * $response = $transaction->send(); + * $data = $response->getData(); + * echo "Gateway fetchApplicationFee response data == " . print_r($data, true) . "\n"; + * + * + * @see \Omnipay\Stripe\Gateway + * + * @link https://stripe.com/docs/api#retrieve_application_fee + */ +class FetchApplicationFeeRequest extends AbstractRequest +{ + /** + * Get the application fee reference + * + * @return string + */ + public function getApplicationFeeReference() + { + return $this->getParameter('applicationFeeReference'); + } + + /** + * Set the application fee reference + * + * @param string $value + * + * @return AbstractRequest provides a fluent interface. + */ + public function setApplicationFeeReference($value) + { + return $this->setParameter('applicationFeeReference', $value); + } + + public function getData() + { + $this->validate('applicationFeeReference'); + + $data = array(); + + return $data; + } + + public function getEndpoint() + { + return $this->endpoint . '/application_fees/' . $this->getApplicationFeeReference(); + } + + public function getHttpMethod() + { + return 'GET'; + } +} diff --git a/src/Message/Response.php b/src/Message/Response.php index 6e5ea959..476ecd0f 100644 --- a/src/Message/Response.php +++ b/src/Message/Response.php @@ -100,6 +100,23 @@ public function getTransactionReference() return null; } + /** + * Get the balance transaction reference. + * + * @return string|null + */ + public function getApplicationFeeReference() + { + if (isset($this->data['object']) && 'application_fee' === $this->data['object']) { + return $this->data['id']; + } + if (isset($this->data['error']) && isset($this->data['error']['application_fee'])) { + return $this->data['error']['application_fee']; + } + + return null; + } + /** * Get the balance transaction reference. * diff --git a/tests/Message/FetchApplicationFeeTest.php b/tests/Message/FetchApplicationFeeTest.php new file mode 100644 index 00000000..6dbc97e1 --- /dev/null +++ b/tests/Message/FetchApplicationFeeTest.php @@ -0,0 +1,43 @@ +request = new FetchApplicationFeeRequest($this->getHttpClient(), $this->getHttpRequest()); + $this->request->setApplicationFeeReference('fee_1FITlv123YJsynqe3nOIfake'); + } + + public function testEndpoint() + { + $this->assertSame('https://api.stripe.com/v1/application_fees/fee_1FITlv123YJsynqe3nOIfake', $this->request->getEndpoint()); + } + + public function testSendSuccess() + { + $this->setMockHttpResponse('FetchApplicationFeeSuccess.txt'); + $response = $this->request->send(); + + $this->assertTrue($response->isSuccessful()); + $this->assertFalse($response->isRedirect()); + $this->assertSame('fee_1FITlv123YJsynqe3nOIfake', $response->getApplicationFeeReference()); + $this->assertNull($response->getCardReference()); + $this->assertNull($response->getMessage()); + } + + public function testSendError() + { + $this->setMockHttpResponse('FetchApplicationFeeFailure.txt'); + $response = $this->request->send(); + + $this->assertFalse($response->isSuccessful()); + $this->assertFalse($response->isRedirect()); + $this->assertNull($response->getBalanceTransactionReference()); + $this->assertNull($response->getCardReference()); + $this->assertSame('No such application fee: fee_1FITlv123YJsynqe3nOIfake', $response->getMessage()); + } +} diff --git a/tests/Mock/FetchApplicationFeeFailure.txt b/tests/Mock/FetchApplicationFeeFailure.txt new file mode 100644 index 00000000..bc5f04f0 --- /dev/null +++ b/tests/Mock/FetchApplicationFeeFailure.txt @@ -0,0 +1,17 @@ +HTTP/1.1 404 Not Found +Server: nginx +Date: Wed, 24 Jul 2013 13:40:31 GMT +Content-Type: application/json;charset=utf-8 +Content-Length: 132 +Connection: keep-alive +Access-Control-Allow-Credentials: true +Access-Control-Max-Age: 300 +Cache-Control: no-cache, no-store + +{ + "error": { + "type": "invalid_request_error", + "message": "No such application fee: fee_1FITlv123YJsynqe3nOIfake", + "param": "id" + } +} diff --git a/tests/Mock/FetchApplicationFeeSuccess.txt b/tests/Mock/FetchApplicationFeeSuccess.txt new file mode 100644 index 00000000..d66dbafe --- /dev/null +++ b/tests/Mock/FetchApplicationFeeSuccess.txt @@ -0,0 +1,32 @@ +HTTP/1.1 200 OK +Server: nginx +Date: Wed, 24 Jul 2013 07:14:02 GMT +Content-Type: application/json;charset=utf-8 +Content-Length: 1092 +Connection: keep-alive +Access-Control-Allow-Credentials: true +Access-Control-Max-Age: 300 +Cache-Control: no-cache, no-store + +{ + "id": "fee_1FITlv123YJsynqe3nOIfake", + "object": "application_fee", + "account": "acct_14901h0a0fh01293", + "amount": 100, + "amount_refunded": 0, + "application": "ca_Fo5xaLt123SEtSKHui0SZOgAiuVwfake", + "balance_transaction": "txn_1FH8W123vYJsynqeQKMWfake", + "charge": "ch_1FIT123rvYJsynqeQpJOFfake", + "created": 1568438771, + "currency": "usd", + "livemode": false, + "originating_transaction": null, + "refunded": false, + "refunds": { + "object": "list", + "data": [], + "has_more": false, + "total_count": 0, + "url": "/v1/application_fees/fee_1FITlvArvYJsynqe3nOIfake/refunds" + } +} \ No newline at end of file