Skip to content

Commit 1b55399

Browse files
committed
Merge pull request #4 from finicprint/master
add support for submerchant service fee
2 parents 516d191 + 02a5874 commit 1b55399

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

src/Message/AbstractRequest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Braintree_Gateway;
66
use Guzzle\Http\ClientInterface;
7+
use Omnipay\Common\Exception\InvalidRequestException;
78
use Symfony\Component\HttpFoundation\Request as HttpRequest;
89
use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest;
910

@@ -209,6 +210,29 @@ public function setHoldInEscrow($value)
209210
return $this->setParameter('holdInEscrow', (bool) $value);
210211
}
211212

213+
public function getServiceFeeAmount()
214+
{
215+
$amount = $this->getParameter('serviceFeeAmount');
216+
if ($amount !== null) {
217+
if (!is_float($amount) &&
218+
$this->getCurrencyDecimalPlaces() > 0 &&
219+
false === strpos((string)$amount, '.')
220+
) {
221+
throw new InvalidRequestException(
222+
'Please specify amount as a string or float, ' .
223+
'with decimal places (e.g. \'10.00\' to represent $10.00).'
224+
);
225+
}
226+
227+
return $this->formatCurrency($amount);
228+
}
229+
}
230+
231+
public function setServiceFeeAmount($value)
232+
{
233+
return $this->setParameter('serviceFeeAmount', $value);
234+
}
235+
212236
public function getStoreInVault()
213237
{
214238
return $this->getParameter('storeInVault');

src/Message/AuthorizeRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function getData()
2828
'orderId' => $this->getTransactionId(),
2929
'purchaseOrderNumber' => $this->getPurchaseOrderNumber(),
3030
'recurring' => $this->getRecurring(),
31+
'serviceFeeAmount' => $this->getServiceFeeAmount(),
3132
'shippingAddressId' => $this->getShippingAddressId(),
3233
'taxAmount' => $this->getTaxAmount(),
3334
'taxExempt' => $this->getTaxExempt(),

tests/Message/AuthorizeRequestTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,81 @@ public function testPaymentMethodNonce()
9393
$this->assertArrayNotHasKey('paymentMethodToken', $data);
9494
}
9595

96+
public function testSubMerchantSale()
97+
{
98+
$this->request->initialize(
99+
array(
100+
'amount' => '100.00',
101+
'holdInEscrow' => true,
102+
'merchantAccountId' => 'blue_ladders_store',
103+
'paymentMethodToken' => 'fake-token-123',
104+
'serviceFeeAmount' => '10.00',
105+
)
106+
);
107+
108+
$data = $this->request->getData();
109+
$this->assertTrue($data['options']['holdInEscrow']);
110+
$this->assertSame('blue_ladders_store', $data['merchantAccountId']);
111+
$this->assertSame('10.00', $data['serviceFeeAmount']);
112+
}
113+
96114
public function testSandboxEnvironment()
97115
{
98116
$this->request->setTestMode(true);
99117

100118
$this->request->configure();
101119
$this->assertSame('sandbox', \Braintree_Configuration::environment());
102120
}
121+
122+
public function testServiceFeeAmount()
123+
{
124+
$this->assertSame($this->request, $this->request->setServiceFeeAmount('2.00'));
125+
$this->assertSame('2.00', $this->request->getServiceFeeAmount());
126+
}
127+
128+
public function testServiceFeeAmountWithFloat()
129+
{
130+
$this->assertSame($this->request, $this->request->setServiceFeeAmount(2.0));
131+
$this->assertSame('2.00', $this->request->getServiceFeeAmount());
132+
}
133+
134+
public function testServiceFeeAmountWithEmpty()
135+
{
136+
$this->assertSame($this->request, $this->request->setServiceFeeAmount(null));
137+
$this->assertSame(null, $this->request->getServiceFeeAmount());
138+
}
139+
140+
public function testGetServiceFeeAmountNoDecimals()
141+
{
142+
$this->assertSame($this->request, $this->request->setCurrency('JPY'));
143+
$this->assertSame($this->request, $this->request->setServiceFeeAmount('1366'));
144+
$this->assertSame('1366', $this->request->getServiceFeeAmount());
145+
}
146+
147+
public function testGetServiceFeeAmountNoDecimalsRounding()
148+
{
149+
$this->assertSame($this->request, $this->request->setServiceFeeAmount('136.5'));
150+
$this->assertSame($this->request, $this->request->setCurrency('JPY'));
151+
$this->assertSame('137', $this->request->getServiceFeeAmount());
152+
}
153+
154+
/**
155+
* @expectedException \Omnipay\Common\Exception\InvalidRequestException
156+
*/
157+
public function testServiceFeeAmountWithIntThrowsException()
158+
{
159+
// ambiguous value, avoid errors upgrading from v0.9
160+
$this->assertSame($this->request, $this->request->setServiceFeeAmount(10));
161+
$this->request->getServiceFeeAmount();
162+
}
163+
164+
/**
165+
* @expectedException \Omnipay\Common\Exception\InvalidRequestException
166+
*/
167+
public function testServiceFeeAmountWithIntStringThrowsException()
168+
{
169+
// ambiguous value, avoid errors upgrading from v0.9
170+
$this->assertSame($this->request, $this->request->setServiceFeeAmount('10'));
171+
$this->request->getServiceFeeAmount();
172+
}
103173
}

0 commit comments

Comments
 (0)