Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions example/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,47 @@
));
});

// create gateway update
$app->get('/gateways/{name}/update', function($name) use ($app) {
$gateway = Omnipay\Common\GatewayFactory::create($name);
$sessionVar = 'omnipay.'.$gateway->getShortName();
$gateway->initialize((array) $app['session']->get($sessionVar));

$params = $app['session']->get($sessionVar.'.update', array());
$card = new Omnipay\Common\CreditCard($app['session']->get($sessionVar.'.card'));

return $app['twig']->render('request.twig', array(
'gateway' => $gateway,
'method' => 'update',
'params' => $params,
'card' => $card->getParameters(),
));
});

// submit gateway update
$app->post('/gateways/{name}/update', function($name) use ($app) {
$gateway = Omnipay\Common\GatewayFactory::create($name);
$sessionVar = 'omnipay.'.$gateway->getShortName();
$gateway->initialize((array) $app['session']->get($sessionVar));

// load POST data
$params = $app['request']->get('params');
$card = $app['request']->get('card');

// save POST data into session
$app['session']->set($sessionVar.'.update', $params);
$app['session']->set($sessionVar.'.card', $card);

$params['card'] = $card;
$params['clientIp'] = $app['request']->getClientIp();
$response = $gateway->update($params)->send();

return $app['twig']->render('response.twig', array(
'gateway' => $gateway,
'response' => $response,
));
});

// create gateway unstore
$app->get('/gateways/{name}/unstore', function($name) use ($app) {
$gateway = Omnipay\Common\GatewayFactory::create($name);
Expand Down
3 changes: 3 additions & 0 deletions example/views/gateway.twig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ $response = $gateway->initialize($params);</pre>
{% if gateway.supportsStore() %}
<li><a href="/gateways/{{gateway.shortName}}/store">store()</a></li>
{% endif %}
{% if gateway.supportsUpdate() %}
<li><a href="/gateways/{{gateway.shortName}}/update">update()</a></li>
{% endif %}
{% if gateway.supportsUnstore() %}
<li><a href="/gateways/{{gateway.shortName}}/unstore">unstore()</a></li>
{% endif %}
Expand Down
10 changes: 10 additions & 0 deletions src/Omnipay/Common/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ public function supportsUnstore()
return method_exists($this, 'unstore');
}

/**
* Supports Update
*
* @return boolean True if this gateway supports the update() method
*/
public function supportsUpdate()
{
return method_exists($this, 'update');
}

/**
* Create and initialize a request object using existing parameters from this gateway
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Omnipay/Stripe/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public function store(array $parameters = array())
return $this->createRequest('\Omnipay\Stripe\Message\StoreRequest', $parameters);
}

public function update(array $parameters = array())
{
return $this->createRequest('\Omnipay\Stripe\Message\UpdateRequest', $parameters);
}

public function unstore(array $parameters = array())
{
return $this->createRequest('\Omnipay\Stripe\Message\UnstoreRequest', $parameters);
Expand Down
40 changes: 40 additions & 0 deletions src/Omnipay/Stripe/Message/UpdateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Omnipay package.
*
* (c) Adrian Macneil <adrian@adrianmacneil.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Omnipay\Stripe\Message;

/**
* Stripe Update Request
*/
class UpdateRequest extends PurchaseRequest
{
public function getData()
{
$data = array();
$data['description'] = $this->getDescription();

if ($this->getCardToken()) {
$data['card'] = $this->getCardToken();
} elseif ($this->getCard()) {
$data['card'] = $this->getCardData();
$data['email'] = $this->getCard()->getEmail();
}

$this->validate('cardReference');

return $data;
}

public function getEndpoint()
{
return $this->endpoint.'/customers/'.$this->getCardReference();
}
}
28 changes: 28 additions & 0 deletions tests/Omnipay/Stripe/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function setUp()
$this->storeOptions = array(
'card' => $this->getValidCard(),
);

$this->updateOptions = array(
'cardReference' => 'cus_1MZSEtqSghKx99',
);

$this->unstoreOptions = array(
'cardReference' => 'cus_1MZSEtqSghKx99',
Expand Down Expand Up @@ -113,6 +117,30 @@ public function testStoreFailure()
$this->assertNull($response->getCardReference());
$this->assertSame('You must provide an integer value for \'exp_year\'.', $response->getMessage());
}

public function testUpdateSuccess()
{
$this->setMockHttpResponse('UpdateSuccess.txt');
$response = $this->gateway->store($this->storeOptions)->send();

$this->assertTrue($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertNull($response->getTransactionReference());
$this->assertSame('cus_1MZeNih5LdKxDq', $response->getCardReference());
$this->assertNull($response->getMessage());
}

public function testUpdateFailure()
{
$this->setMockHttpResponse('UpdateFailure.txt');
$response = $this->gateway->store($this->storeOptions)->send();

$this->assertFalse($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertNull($response->getTransactionReference());
$this->assertNull($response->getCardReference());
$this->assertSame('No such customer: cus_1MZeNih5LdKxDq', $response->getMessage());
}

public function testUnstoreSuccess()
{
Expand Down
24 changes: 24 additions & 0 deletions tests/Omnipay/Stripe/Message/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ public function testStoreFailure()
$this->assertNull($response->getCardReference());
$this->assertSame('You must provide an integer value for \'exp_year\'.', $response->getMessage());
}

public function testUpdateSuccess()
{
$httpResponse = $this->getMockHttpResponse('UpdateSuccess.txt');
$response = new Response($this->getMockRequest(), $httpResponse->json());

$this->assertTrue($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertNull($response->getTransactionReference());
$this->assertSame('cus_1MZeNih5LdKxDq', $response->getCardReference());
$this->assertNull($response->getMessage());
}

public function testUpdateFailure()
{
$httpResponse = $this->getMockHttpResponse('UpdateFailure.txt');
$response = new Response($this->getMockRequest(), $httpResponse->json());

$this->assertFalse($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertNull($response->getTransactionReference());
$this->assertNull($response->getCardReference());
$this->assertSame('No such customer: cus_1MZeNih5LdKxDq', $response->getMessage());
}

public function testUnstoreSuccess()
{
Expand Down
17 changes: 17 additions & 0 deletions tests/Omnipay/Stripe/Mock/UpdateFailure.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
HTTP/1.1 404 Not Found
Server: nginx
Date: Tue, 26 Feb 2013 16:32:51 GMT
Content-Type: application/json;charset=utf-8
Content-Length: 131
Connection: keep-alive
Access-Control-Max-Age: 300
Access-Control-Allow-Credentials: true
Cache-Control: no-cache, no-store

{
"error": {
"type": "invalid_request_error",
"message": "No such customer: cus_1MZeNih5LdKxDq",
"param": "id"
}
}
23 changes: 23 additions & 0 deletions tests/Omnipay/Stripe/Mock/UpdateSuccess.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 26 Feb 2013 16:11:12 GMT
Content-Type: application/json;charset=utf-8
Content-Length: 694
Connection: keep-alive
Access-Control-Max-Age: 300
Access-Control-Allow-Credentials: true
Cache-Control: no-cache, no-store

{
"object": "customer",
"created": 1365771516,
"id": "cus_1MZeNih5LdKxDq",
"livemode": false,
"description": "fdsa",
"active_card": null,
"email": null,
"delinquent": false,
"subscription": null,
"discount": null,
"account_balance": 0
}