Skip to content

Commit d00a0a2

Browse files
committed
Add Stripe authorize/capture. Closes #56
1 parent dad0dce commit d00a0a2

21 files changed

+765
-214
lines changed

src/Omnipay/Common/Message/AbstractRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function getCard()
134134

135135
public function setCard($value)
136136
{
137-
if (!$value instanceof CreditCard) {
137+
if ($value && !$value instanceof CreditCard) {
138138
$value = new CreditCard($value);
139139
}
140140

src/Omnipay/Stripe/Gateway.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ public function setApiKey($value)
4444
return $this->setParameter('apiKey', $value);
4545
}
4646

47+
public function authorize(array $parameters = array())
48+
{
49+
return $this->createRequest('\Omnipay\Stripe\Message\AuthorizeRequest', $parameters);
50+
}
51+
52+
public function capture(array $parameters = array())
53+
{
54+
return $this->createRequest('\Omnipay\Stripe\Message\CaptureRequest', $parameters);
55+
}
56+
4757
public function purchase(array $parameters = array())
4858
{
4959
return $this->createRequest('\Omnipay\Stripe\Message\PurchaseRequest', $parameters);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 Abstract Request
16+
*/
17+
abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
18+
{
19+
protected $endpoint = 'https://api.stripe.com/v1';
20+
21+
public function getApiKey()
22+
{
23+
return $this->getParameter('apiKey');
24+
}
25+
26+
public function setApiKey($value)
27+
{
28+
return $this->setParameter('apiKey', $value);
29+
}
30+
31+
abstract public function getEndpoint();
32+
33+
public function getHttpMethod()
34+
{
35+
return 'POST';
36+
}
37+
38+
public function send()
39+
{
40+
// don't throw exceptions for 4xx errors
41+
$this->httpClient->getEventDispatcher()->addListener(
42+
'request.error',
43+
function ($event) {
44+
if ($event['response']->isClientError()) {
45+
$event->stopPropagation();
46+
}
47+
}
48+
);
49+
50+
$httpRequest = $this->httpClient->createRequest(
51+
$this->getHttpMethod(),
52+
$this->getEndpoint(),
53+
null,
54+
$this->getData()
55+
);
56+
$httpResponse = $httpRequest
57+
->setHeader('Authorization', 'Basic '.base64_encode($this->getApiKey().':'))
58+
->send();
59+
60+
return $this->response = new Response($this, $httpResponse->json());
61+
}
62+
63+
protected function getCardData()
64+
{
65+
$this->getCard()->validate();
66+
67+
$data = array();
68+
$data['number'] = $this->getCard()->getNumber();
69+
$data['exp_month'] = $this->getCard()->getExpiryMonth();
70+
$data['exp_year'] = $this->getCard()->getExpiryYear();
71+
$data['cvc'] = $this->getCard()->getCvv();
72+
$data['name'] = $this->getCard()->getName();
73+
$data['address_line1'] = $this->getCard()->getAddress1();
74+
$data['address_line2'] = $this->getCard()->getAddress2();
75+
$data['address_city'] = $this->getCard()->getCity();
76+
$data['address_zip'] = $this->getCard()->getPostcode();
77+
$data['address_state'] = $this->getCard()->getState();
78+
$data['address_country'] = $this->getCard()->getCountry();
79+
80+
return $data;
81+
}
82+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 Authorize Request
16+
*/
17+
class AuthorizeRequest extends AbstractRequest
18+
{
19+
public function getData()
20+
{
21+
$this->validate('amount', 'currency');
22+
23+
$data = array();
24+
$data['amount'] = $this->getAmount();
25+
$data['currency'] = strtolower($this->getCurrency());
26+
$data['description'] = $this->getDescription();
27+
$data['capture'] = 'false';
28+
29+
if ($this->getCardReference()) {
30+
$data['customer'] = $this->getCardReference();
31+
} elseif ($this->getCardToken()) {
32+
$data['card'] = $this->getCardToken();
33+
} elseif ($this->getCard()) {
34+
$data['card'] = $this->getCardData();
35+
} else {
36+
// one of cardReference, cardToken, or card is required
37+
$this->validate('card');
38+
}
39+
40+
return $data;
41+
}
42+
43+
public function getEndpoint()
44+
{
45+
return $this->endpoint.'/charges';
46+
}
47+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 Capture Request
16+
*/
17+
class CaptureRequest extends AbstractRequest
18+
{
19+
public function getData()
20+
{
21+
$this->validate('transactionReference');
22+
23+
$data = array();
24+
25+
if ($amount = $this->getAmount()) {
26+
$data['amount'] = $amount;
27+
}
28+
29+
return $data;
30+
}
31+
32+
public function getEndpoint()
33+
{
34+
return $this->endpoint.'/charges/'.$this->getTransactionReference().'/capture';
35+
}
36+
}

src/Omnipay/Stripe/Message/CreateCardRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Stripe Create Credit Card Request
1616
*/
17-
class CreateCardRequest extends PurchaseRequest
17+
class CreateCardRequest extends AbstractRequest
1818
{
1919
public function getData()
2020
{

src/Omnipay/Stripe/Message/DeleteCardRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Stripe Delete Credit Card Request
1616
*/
17-
class DeleteCardRequest extends PurchaseRequest
17+
class DeleteCardRequest extends AbstractRequest
1818
{
1919
public function getData()
2020
{

src/Omnipay/Stripe/Message/PurchaseRequest.php

Lines changed: 3 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -11,100 +11,16 @@
1111

1212
namespace Omnipay\Stripe\Message;
1313

14-
use Omnipay\Common\Message\AbstractRequest;
15-
1614
/**
1715
* Stripe Purchase Request
1816
*/
19-
class PurchaseRequest extends AbstractRequest
17+
class PurchaseRequest extends AuthorizeRequest
2018
{
21-
protected $endpoint = 'https://api.stripe.com/v1';
22-
23-
public function getApiKey()
24-
{
25-
return $this->getParameter('apiKey');
26-
}
27-
28-
public function setApiKey($value)
29-
{
30-
return $this->setParameter('apiKey', $value);
31-
}
32-
33-
protected function getCardData()
34-
{
35-
$this->getCard()->validate();
36-
37-
$data = array();
38-
$data['number'] = $this->getCard()->getNumber();
39-
$data['exp_month'] = $this->getCard()->getExpiryMonth();
40-
$data['exp_year'] = $this->getCard()->getExpiryYear();
41-
$data['cvc'] = $this->getCard()->getCvv();
42-
$data['name'] = $this->getCard()->getName();
43-
$data['address_line1'] = $this->getCard()->getAddress1();
44-
$data['address_line2'] = $this->getCard()->getAddress2();
45-
$data['address_city'] = $this->getCard()->getCity();
46-
$data['address_zip'] = $this->getCard()->getPostcode();
47-
$data['address_state'] = $this->getCard()->getState();
48-
$data['address_country'] = $this->getCard()->getCountry();
49-
50-
return $data;
51-
}
52-
5319
public function getData()
5420
{
55-
$this->validate('amount', 'currency');
56-
57-
$data = array();
58-
$data['amount'] = $this->getAmount();
59-
$data['currency'] = strtolower($this->getCurrency());
60-
$data['description'] = $this->getDescription();
61-
62-
if ($this->getCardReference()) {
63-
$data['customer'] = $this->getCardReference();
64-
} elseif ($this->getCardToken()) {
65-
$data['card'] = $this->getCardToken();
66-
} elseif ($this->getCard()) {
67-
$data['card'] = $this->getCardData();
68-
} else {
69-
// one of cardReference, cardToken, or card is required
70-
$this->validate('card');
71-
}
21+
$data = parent::getData();
22+
$data['capture'] = 'true';
7223

7324
return $data;
7425
}
75-
76-
public function getHttpMethod()
77-
{
78-
return 'POST';
79-
}
80-
81-
public function getEndpoint()
82-
{
83-
return $this->endpoint.'/charges';
84-
}
85-
86-
public function send()
87-
{
88-
// don't throw exceptions for 4xx errors
89-
$this->httpClient->getEventDispatcher()->addListener(
90-
'request.error',
91-
function ($event) {
92-
if ($event['response']->isClientError()) {
93-
$event->stopPropagation();
94-
}
95-
}
96-
);
97-
98-
$httpRequest = $this->httpClient->createRequest(
99-
$this->getHttpMethod(),
100-
$this->getEndpoint(),
101-
null,
102-
$this->getData()
103-
);
104-
$httpResponse = $httpRequest
105-
->setHeader('Authorization', 'Basic '.base64_encode($this->getApiKey().':'))
106-
->send();
107-
108-
return $this->response = new Response($this, $httpResponse->json());
109-
}
11026
}

src/Omnipay/Stripe/Message/RefundRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Stripe Refund Request
1616
*/
17-
class RefundRequest extends PurchaseRequest
17+
class RefundRequest extends AbstractRequest
1818
{
1919
public function getData()
2020
{

src/Omnipay/Stripe/Message/UpdateCardRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Stripe Update Credit Card Request
1616
*/
17-
class UpdateCardRequest extends PurchaseRequest
17+
class UpdateCardRequest extends AbstractRequest
1818
{
1919
public function getData()
2020
{

0 commit comments

Comments
 (0)