Skip to content

Commit

Permalink
Merge pull request #11 from cfreear/master
Browse files Browse the repository at this point in the history
Stripe Connect transaction_fee
  • Loading branch information
greydnls committed Mar 23, 2015
2 parents b3ed102 + 132b20e commit fa2ad61
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ The following gateways are provided by this package:
For general usage instructions, please see the main [Omnipay](https://github.com/thephpleague/omnipay)
repository.

### Stripe.js

The Stripe integration is fairly straight forward. Essentially you just pass
a `token` field through to Stripe instead of the regular credit card data.

Expand All @@ -50,6 +52,20 @@ $token = $_POST['stripeToken'];
$response = $gateway->purchase(['amount' => '10.00', 'currency' => 'USD', 'token' => $token])->send();
```

### Stripe Connect

Stripe connect applications can charge an additional fee on top of Stripe's fees for charges they make on behalf of
their users. To do this you need to specify an additional `transactionFee` parameter as part of an authorize or purchase
request.

When a charge is refunded the transaction fee is refunded with an amount proportional to the amount of the charge
refunded and by default this will come from your connected user's Stripe account effectively leaving them out of pocket.
To refund from your (the applications) Stripe account instead you can pass a ``refundApplicationFee`` parameter with a
boolean value of true as part of a refund request.

Note: making requests with Stripe Connect specific parameters can only be made using the OAuth access token you received
as part of the authorization process. Read more on Stripe Connect [here](https://stripe.com/docs/connect).

## Test Mode

Stripe accounts have test-mode API keys as well as live-mode API keys. These keys can be active
Expand Down
19 changes: 19 additions & 0 deletions src/Message/AuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@
*/
class AuthorizeRequest extends AbstractRequest
{
public function getApplicationFee()
{
return $this->getParameter('applicationFee');
}

public function getApplicationFeeInteger()
{
return (int) round($this->getApplicationFee() * pow(10, $this->getCurrencyDecimalPlaces()));
}

public function setApplicationFee($value)
{
return $this->setParameter('applicationFee', $value);
}

public function getData()
{
$this->validate('amount', 'currency');
Expand All @@ -73,6 +88,10 @@ public function getData()
$data['metadata'] = $this->getMetadata();
$data['capture'] = 'false';

if ($this->getApplicationFee()) {
$data['application_fee'] = $this->getApplicationFeeInteger();
}

if ($this->getCardReference()) {
$data['customer'] = $this->getCardReference();
} elseif ($this->getToken()) {
Expand Down
31 changes: 31 additions & 0 deletions src/Message/RefundRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,44 @@
*/
class RefundRequest extends AbstractRequest
{
/**
* @return bool Whether the application fee should be refunded
*/
public function getRefundApplicationFee()
{
return $this->getParameter('refundApplicationFee');
}

/**
* Whether to refund the application fee associated with a charge.
*
* From the {@link https://stripe.com/docs/api#create_refund Stripe docs}:
* Boolean indicating whether the application fee should be refunded
* when refunding this charge. If a full charge refund is given, the
* full application fee will be refunded. Else, the application fee
* will be refunded with an amount proportional to the amount of the
* charge refunded. An application fee can only be refunded by the
* application that created the charge.
*
* @param bool $value Whether the application fee should be refunded
* @return AbstractRequest
*/
public function setRefundApplicationFee($value)
{
return $this->setParameter('refundApplicationFee', $value);
}

public function getData()
{
$this->validate('transactionReference', 'amount');

$data = array();
$data['amount'] = $this->getAmountInteger();

if ($this->getRefundApplicationFee()) {
$data['refund_application_fee'] = true;
}

return $data;
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Message/AuthorizeRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function setUp()
'metadata' => array(
'foo' => 'bar',
),
'applicationFee' => '1.00'
)
);
}
Expand All @@ -31,6 +32,7 @@ public function testGetData()
$this->assertSame('Order #42', $data['description']);
$this->assertSame('false', $data['capture']);
$this->assertSame(array('foo' => 'bar'), $data['metadata']);
$this->assertSame(100, $data['application_fee']);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion tests/Message/RefundRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function setUp()
{
$this->request = new RefundRequest($this->getHttpClient(), $this->getHttpRequest());
$this->request->setTransactionReference('ch_12RgN9L7XhO9mI')
->setAmount('10.00');
->setAmount('10.00')->setRefundApplicationFee(true);
}

public function testEndpoint()
Expand All @@ -24,6 +24,12 @@ public function testAmount()
$this->assertSame(1000, $data['amount']);
}

public function testRefundApplicationFee()
{
$data = $this->request->getData();
$this->assertTrue($data['refund_application_fee']);
}

public function testSendSuccess()
{
$this->setMockHttpResponse('RefundSuccess.txt');
Expand Down

0 comments on commit fa2ad61

Please sign in to comment.