Skip to content

Commit 2a4036d

Browse files
committed
Merge pull request #15 from c0nstantx/subscriptions
Subscriptions/Webhooks
2 parents e426dfb + e67f842 commit 2a4036d

File tree

8 files changed

+327
-1
lines changed

8 files changed

+327
-1
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,57 @@ $response = $gateway->purchase([
4848
For general usage instructions, please see the main [Omnipay](https://github.com/thephpleague/omnipay)
4949
repository.
5050

51+
## Driver specific usage
52+
### Create customer
53+
54+
```php
55+
$customer = $gateway->createCustomer([
56+
'customerData' => [
57+
'id' => 1,
58+
'firstName' => 'John',
59+
'lastName' => 'Doe'
60+
]
61+
])->send();
62+
```
63+
You can find full list of options [here](https://developers.braintreepayments.com/reference/request/customer/create/php).
64+
65+
###Find customer (By id)
66+
67+
```php
68+
$customer = $gateway->findCustomer(1)->send();
69+
```
70+
You can find full list of options [here](https://developers.braintreepayments.com/reference/request/customer/find/php)
71+
72+
###Create subscription
73+
74+
```php
75+
$subscription = $gateway->createSubscription([
76+
'subscriptionData' => [
77+
'paymentMethodToken' => 'payment_method_token',
78+
'planId' => 'weekly',
79+
'price' => '30.00'
80+
]
81+
])->send();
82+
```
83+
You can find full list of options [here](https://developers.braintreepayments.com/reference/request/subscription/create/php)
84+
85+
###Cancel subscription
86+
87+
```php
88+
$subscription = $gateway->cancelSubscription('id')->send();
89+
```
90+
You can find full list of options [here](https://developers.braintreepayments.com/reference/request/subscription/cancel/php)
91+
92+
###Parse webhook notification
93+
94+
```php
95+
$notification = $gateway->parseNotification([
96+
'bt_signature' => 'signature',
97+
'bt_payload' => 'payload'
98+
])->send();
99+
```
100+
You can find full list of options [here](https://developers.braintreepayments.com/guides/webhooks/parse/php)
101+
51102
## Support
52103

53104
If you are having general issues with Omnipay, we suggest posting on

src/Gateway.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,39 @@ public function void(array $parameters = array())
229229
{
230230
return $this->createRequest('\Omnipay\Braintree\Message\VoidRequest', $parameters);
231231
}
232+
233+
/**
234+
* @param array $parameters
235+
*
236+
* @return \Omnipay\Common\Message\AbstractRequest
237+
*/
238+
public function createSubscription(array $parameters = array())
239+
{
240+
return $this->createRequest('\Omnipay\Braintree\Message\CreateSubscriptionRequest', $parameters);
241+
}
242+
243+
/**
244+
* @param string $subscriptionId
245+
*
246+
* @return \Omnipay\Common\Message\AbstractRequest
247+
*/
248+
public function cancelSubscription($subscriptionId)
249+
{
250+
return $this->createRequest('\Omnipay\Braintree\Message\CancelSubscriptionRequest', array('id' => $subscriptionId));
251+
}
252+
253+
/**
254+
* @param array $parameters
255+
*
256+
* @return \Braintree_WebhookNotification
257+
*
258+
* @throws \Braintree_Exception_InvalidSignature
259+
*/
260+
public function parseNotification(array $parameters = array())
261+
{
262+
return \Braintree_WebhookNotification::parse(
263+
$parameters['bt_signature'],
264+
$parameters['bt_payload']
265+
);
266+
}
232267
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
namespace Omnipay\Braintree\Message;
3+
4+
use Omnipay\Common\Message\ResponseInterface;
5+
6+
/**
7+
* Cancel Subscription Request
8+
*
9+
* @method CustomerResponse send()
10+
*/
11+
class CancelSubscriptionRequest extends AbstractRequest
12+
{
13+
/** @var string */
14+
protected $subscriptionId;
15+
16+
/**
17+
* Get the raw data array for this message. The format of this varies from gateway to
18+
* gateway, but will usually be either an associative array, or a SimpleXMLElement.
19+
*
20+
* @return mixed
21+
*/
22+
public function getData()
23+
{
24+
return $this->subscriptionId;
25+
}
26+
27+
/**
28+
* Send the request with specified data
29+
*
30+
* @param mixed $data The data to send
31+
* @return ResponseInterface
32+
*/
33+
public function sendData($subscriptionId)
34+
{
35+
$response = $this->braintree->subscription()->cancel($subscriptionId);
36+
37+
return $this->response = new SubscriptionResponse($this, $response);
38+
}
39+
40+
public function setId($subscriptionId)
41+
{
42+
$this->subscriptionId = $subscriptionId;
43+
}
44+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
namespace Omnipay\Braintree\Message;
3+
4+
/**
5+
* Create Subscription Request
6+
*
7+
* @method CustomerResponse send()
8+
*/
9+
class CreateSubscriptionRequest extends AbstractRequest
10+
{
11+
public function getData()
12+
{
13+
return $this->getSubscriptionData();
14+
}
15+
16+
/**
17+
* Send the request with specified data
18+
*
19+
* @param mixed $data The data to send
20+
*
21+
* @return SubscriptionResponse
22+
*/
23+
public function sendData($data)
24+
{
25+
$response = $this->braintree->subscription()->create($data);
26+
27+
return $this->response = new SubscriptionResponse($this, $response);
28+
}
29+
30+
public function setSubscriptionData($value)
31+
{
32+
return $this->setParameter('subscriptionData', $value);
33+
}
34+
35+
public function getSubscriptionData()
36+
{
37+
return $this->getParameter('subscriptionData');
38+
}
39+
}

src/Message/SubscriptionResponse.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Omnipay\Braintree\Message;
4+
5+
/**
6+
* Subscription Response
7+
*/
8+
class SubscriptionResponse extends Response
9+
{
10+
public function getSubscriptionData()
11+
{
12+
if (isset($this->data->subscription)) {
13+
return $this->data->subscription;
14+
}
15+
16+
return null;
17+
}
18+
}

tests/GatewayTest.php

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Omnipay\Braintree;
44

55
use Omnipay\Tests\GatewayTestCase;
6-
use Omnipay\Common\CreditCard;
76

87
class GatewayTest extends GatewayTestCase
98
{
@@ -124,4 +123,70 @@ public function testClientToken()
124123
$request = $this->gateway->clientToken(array());
125124
$this->assertInstanceOf('Omnipay\Braintree\Message\ClientTokenRequest', $request);
126125
}
126+
127+
public function testCreateSubscription()
128+
{
129+
$request = $this->gateway->createSubscription(array());
130+
$this->assertInstanceOf('Omnipay\Braintree\Message\CreateSubscriptionRequest', $request);
131+
}
132+
133+
public function testCancelSubscription()
134+
{
135+
$request = $this->gateway->cancelSubscription('1');
136+
$this->assertInstanceOf('Omnipay\Braintree\Message\CancelSubscriptionRequest', $request);
137+
}
138+
139+
public function testParseNotification()
140+
{
141+
if(\Braintree_Version::MAJOR >= 3) {
142+
$xml = '<notification></notification>';
143+
$payload = base64_encode($xml);
144+
$signature = \Braintree_Digest::hexDigestSha1(\Braintree_Configuration::privateKey(), $payload);
145+
$gatewayMock = $this->buildGatewayMock($payload);
146+
$gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest(), $gatewayMock);
147+
$params = array(
148+
'bt_signature' => $payload.'|'.$signature,
149+
'bt_payload' => $payload
150+
);
151+
$request = $gateway->parseNotification($params);
152+
$this->assertInstanceOf('\Braintree_WebhookNotification', $request);
153+
} else {
154+
$xml = '<notification><subject></subject></notification>';
155+
$payload = base64_encode($xml);
156+
$signature = \Braintree_Digest::hexDigestSha1(\Braintree_Configuration::privateKey(), $payload);
157+
$gatewayMock = $this->buildGatewayMock($payload);
158+
$gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest(), $gatewayMock);
159+
$params = array(
160+
'bt_signature' => $payload.'|'.$signature,
161+
'bt_payload' => $payload
162+
);
163+
$request = $gateway->parseNotification($params);
164+
$this->assertInstanceOf('\Braintree_WebhookNotification', $request);
165+
}
166+
}
167+
168+
/**
169+
* @param $payload
170+
*
171+
* @return \Braintree_Gateway
172+
*/
173+
protected function buildGatewayMock($payload)
174+
{
175+
$configuration = $this->getMockBuilder('\Braintree_Configuration')
176+
->disableOriginalConstructor()
177+
->setMethods(array(
178+
'assertHasAccessTokenOrKeys'
179+
))
180+
->getMock();
181+
$configuration->expects($this->any())
182+
->method('assertHasAccessTokenOrKeys')
183+
->will($this->returnValue(null));
184+
185+
186+
187+
$configuration->setPublicKey($payload);
188+
189+
\Braintree_Configuration::$global = $configuration;
190+
return \Braintree_Configuration::gateway();
191+
}
127192
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* This file is part of Rocketgraph service
4+
* <http://www.rocketgraph.com>
5+
*/
6+
7+
namespace Omnipay\Braintree\Message;
8+
use Omnipay\Tests\TestCase;
9+
10+
class CustomerResponseTest extends TestCase
11+
{
12+
/**
13+
* @var CreateCustomerRequest
14+
*/
15+
private $request;
16+
17+
public function setUp()
18+
{
19+
parent::setUp();
20+
21+
$this->request = new CreateCustomerRequest($this->getHttpClient(), $this->getHttpRequest(), \Braintree_Configuration::gateway());
22+
}
23+
24+
public function testGetSubscriptionData()
25+
{
26+
$data = new \stdClass();
27+
28+
$response = new CustomerResponse($this->request, $data);
29+
$this->assertNull($response->getCustomerData());
30+
31+
$data->customer = 'customerData';
32+
33+
$response = new CustomerResponse($this->request, $data);
34+
$this->assertEquals('customerData', $response->getCustomerData());
35+
}
36+
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* This file is part of Rocketgraph service
4+
* <http://www.rocketgraph.com>
5+
*/
6+
7+
namespace Omnipay\Braintree\Message;
8+
use Omnipay\Tests\TestCase;
9+
10+
class SubscriptionResponseTest extends TestCase
11+
{
12+
/**
13+
* @var CreateSubscriptionRequest
14+
*/
15+
private $request;
16+
17+
public function setUp()
18+
{
19+
parent::setUp();
20+
21+
$this->request = new CreateSubscriptionRequest($this->getHttpClient(), $this->getHttpRequest(), \Braintree_Configuration::gateway());
22+
}
23+
24+
public function testGetSubscriptionData()
25+
{
26+
$data = new \stdClass();
27+
28+
$response = new SubscriptionResponse($this->request, $data);
29+
$this->assertNull($response->getSubscriptionData());
30+
31+
$data->subscription = 'subscriptionData';
32+
33+
$response = new SubscriptionResponse($this->request, $data);
34+
$this->assertEquals('subscriptionData', $response->getSubscriptionData());
35+
}
36+
37+
}

0 commit comments

Comments
 (0)