Skip to content

Commit d28afd0

Browse files
committed
Merge pull request #41 from pilot/master
Add support update request for Stripe gateway
2 parents fa568c9 + 1ba32aa commit d28afd0

File tree

9 files changed

+191
-0
lines changed

9 files changed

+191
-0
lines changed

example/index.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,47 @@
241241
));
242242
});
243243

244+
// create gateway update
245+
$app->get('/gateways/{name}/update', function($name) use ($app) {
246+
$gateway = Omnipay\Common\GatewayFactory::create($name);
247+
$sessionVar = 'omnipay.'.$gateway->getShortName();
248+
$gateway->initialize((array) $app['session']->get($sessionVar));
249+
250+
$params = $app['session']->get($sessionVar.'.update', array());
251+
$card = new Omnipay\Common\CreditCard($app['session']->get($sessionVar.'.card'));
252+
253+
return $app['twig']->render('request.twig', array(
254+
'gateway' => $gateway,
255+
'method' => 'update',
256+
'params' => $params,
257+
'card' => $card->getParameters(),
258+
));
259+
});
260+
261+
// submit gateway update
262+
$app->post('/gateways/{name}/update', function($name) use ($app) {
263+
$gateway = Omnipay\Common\GatewayFactory::create($name);
264+
$sessionVar = 'omnipay.'.$gateway->getShortName();
265+
$gateway->initialize((array) $app['session']->get($sessionVar));
266+
267+
// load POST data
268+
$params = $app['request']->get('params');
269+
$card = $app['request']->get('card');
270+
271+
// save POST data into session
272+
$app['session']->set($sessionVar.'.update', $params);
273+
$app['session']->set($sessionVar.'.card', $card);
274+
275+
$params['card'] = $card;
276+
$params['clientIp'] = $app['request']->getClientIp();
277+
$response = $gateway->update($params)->send();
278+
279+
return $app['twig']->render('response.twig', array(
280+
'gateway' => $gateway,
281+
'response' => $response,
282+
));
283+
});
284+
244285
// create gateway unstore
245286
$app->get('/gateways/{name}/unstore', function($name) use ($app) {
246287
$gateway = Omnipay\Common\GatewayFactory::create($name);

example/views/gateway.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ $response = $gateway->initialize($params);</pre>
4545
{% if gateway.supportsStore() %}
4646
<li><a href="/gateways/{{gateway.shortName}}/store">store()</a></li>
4747
{% endif %}
48+
{% if gateway.supportsUpdate() %}
49+
<li><a href="/gateways/{{gateway.shortName}}/update">update()</a></li>
50+
{% endif %}
4851
{% if gateway.supportsUnstore() %}
4952
<li><a href="/gateways/{{gateway.shortName}}/unstore">unstore()</a></li>
5053
{% endif %}

src/Omnipay/Common/AbstractGateway.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ public function supportsUnstore()
189189
return method_exists($this, 'unstore');
190190
}
191191

192+
/**
193+
* Supports Update
194+
*
195+
* @return boolean True if this gateway supports the update() method
196+
*/
197+
public function supportsUpdate()
198+
{
199+
return method_exists($this, 'update');
200+
}
201+
192202
/**
193203
* Create and initialize a request object using existing parameters from this gateway
194204
*/

src/Omnipay/Stripe/Gateway.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public function store(array $parameters = array())
5959
return $this->createRequest('\Omnipay\Stripe\Message\StoreRequest', $parameters);
6060
}
6161

62+
public function update(array $parameters = array())
63+
{
64+
return $this->createRequest('\Omnipay\Stripe\Message\UpdateRequest', $parameters);
65+
}
66+
6267
public function unstore(array $parameters = array())
6368
{
6469
return $this->createRequest('\Omnipay\Stripe\Message\UnstoreRequest', $parameters);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Omnipay package.
5+
*
6+
* (c) Adrian Macneil <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Omnipay\Stripe\Message;
13+
14+
/**
15+
* Stripe Update Request
16+
*/
17+
class UpdateRequest extends PurchaseRequest
18+
{
19+
public function getData()
20+
{
21+
$data = array();
22+
$data['description'] = $this->getDescription();
23+
24+
if ($this->getCardToken()) {
25+
$data['card'] = $this->getCardToken();
26+
} elseif ($this->getCard()) {
27+
$data['card'] = $this->getCardData();
28+
$data['email'] = $this->getCard()->getEmail();
29+
}
30+
31+
$this->validate('cardReference');
32+
33+
return $data;
34+
}
35+
36+
public function getEndpoint()
37+
{
38+
return $this->endpoint.'/customers/'.$this->getCardReference();
39+
}
40+
}

tests/Omnipay/Stripe/GatewayTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public function setUp()
3636
$this->storeOptions = array(
3737
'card' => $this->getValidCard(),
3838
);
39+
40+
$this->updateOptions = array(
41+
'cardReference' => 'cus_1MZSEtqSghKx99',
42+
);
3943

4044
$this->unstoreOptions = array(
4145
'cardReference' => 'cus_1MZSEtqSghKx99',
@@ -113,6 +117,30 @@ public function testStoreFailure()
113117
$this->assertNull($response->getCardReference());
114118
$this->assertSame('You must provide an integer value for \'exp_year\'.', $response->getMessage());
115119
}
120+
121+
public function testUpdateSuccess()
122+
{
123+
$this->setMockHttpResponse('UpdateSuccess.txt');
124+
$response = $this->gateway->store($this->storeOptions)->send();
125+
126+
$this->assertTrue($response->isSuccessful());
127+
$this->assertFalse($response->isRedirect());
128+
$this->assertNull($response->getTransactionReference());
129+
$this->assertSame('cus_1MZeNih5LdKxDq', $response->getCardReference());
130+
$this->assertNull($response->getMessage());
131+
}
132+
133+
public function testUpdateFailure()
134+
{
135+
$this->setMockHttpResponse('UpdateFailure.txt');
136+
$response = $this->gateway->store($this->storeOptions)->send();
137+
138+
$this->assertFalse($response->isSuccessful());
139+
$this->assertFalse($response->isRedirect());
140+
$this->assertNull($response->getTransactionReference());
141+
$this->assertNull($response->getCardReference());
142+
$this->assertSame('No such customer: cus_1MZeNih5LdKxDq', $response->getMessage());
143+
}
116144

117145
public function testUnstoreSuccess()
118146
{

tests/Omnipay/Stripe/Message/ResponseTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,30 @@ public function testStoreFailure()
6262
$this->assertNull($response->getCardReference());
6363
$this->assertSame('You must provide an integer value for \'exp_year\'.', $response->getMessage());
6464
}
65+
66+
public function testUpdateSuccess()
67+
{
68+
$httpResponse = $this->getMockHttpResponse('UpdateSuccess.txt');
69+
$response = new Response($this->getMockRequest(), $httpResponse->json());
70+
71+
$this->assertTrue($response->isSuccessful());
72+
$this->assertFalse($response->isRedirect());
73+
$this->assertNull($response->getTransactionReference());
74+
$this->assertSame('cus_1MZeNih5LdKxDq', $response->getCardReference());
75+
$this->assertNull($response->getMessage());
76+
}
77+
78+
public function testUpdateFailure()
79+
{
80+
$httpResponse = $this->getMockHttpResponse('UpdateFailure.txt');
81+
$response = new Response($this->getMockRequest(), $httpResponse->json());
82+
83+
$this->assertFalse($response->isSuccessful());
84+
$this->assertFalse($response->isRedirect());
85+
$this->assertNull($response->getTransactionReference());
86+
$this->assertNull($response->getCardReference());
87+
$this->assertSame('No such customer: cus_1MZeNih5LdKxDq', $response->getMessage());
88+
}
6589

6690
public function testUnstoreSuccess()
6791
{
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
HTTP/1.1 404 Not Found
2+
Server: nginx
3+
Date: Tue, 26 Feb 2013 16:32:51 GMT
4+
Content-Type: application/json;charset=utf-8
5+
Content-Length: 131
6+
Connection: keep-alive
7+
Access-Control-Max-Age: 300
8+
Access-Control-Allow-Credentials: true
9+
Cache-Control: no-cache, no-store
10+
11+
{
12+
"error": {
13+
"type": "invalid_request_error",
14+
"message": "No such customer: cus_1MZeNih5LdKxDq",
15+
"param": "id"
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
HTTP/1.1 200 OK
2+
Server: nginx
3+
Date: Tue, 26 Feb 2013 16:11:12 GMT
4+
Content-Type: application/json;charset=utf-8
5+
Content-Length: 694
6+
Connection: keep-alive
7+
Access-Control-Max-Age: 300
8+
Access-Control-Allow-Credentials: true
9+
Cache-Control: no-cache, no-store
10+
11+
{
12+
"object": "customer",
13+
"created": 1365771516,
14+
"id": "cus_1MZeNih5LdKxDq",
15+
"livemode": false,
16+
"description": "fdsa",
17+
"active_card": null,
18+
"email": null,
19+
"delinquent": false,
20+
"subscription": null,
21+
"discount": null,
22+
"account_balance": 0
23+
}

0 commit comments

Comments
 (0)