Skip to content

Commit f5d5ef9

Browse files
authored
Merge pull request #85 from pedro-stanaka/feature/connect-api-create-tokens
Create Tokens - Connect API
2 parents 2c0df1d + ccf9051 commit f5d5ef9

File tree

4 files changed

+229
-0
lines changed

4 files changed

+229
-0
lines changed

src/Message/CreateTokenRequest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
4+
namespace Omnipay\Stripe\Message;
5+
6+
use Omnipay\Common\Exception\InvalidRequestException;
7+
8+
/**
9+
* Message which creates a new card token, or in a Connect API
10+
* workflow can be used to share clients between the platform and
11+
* the connected accounts.
12+
*
13+
* Creates a single use token that wraps the details of a credit card.
14+
* This token can be used in place of a credit card dictionary with any API method.
15+
* These tokens can only be used once: by creating a new charge object, or attaching them to a customer.
16+
*
17+
* In most cases, you should create tokens client-side using Checkout, Elements, or our mobile libraries,
18+
* instead of using the API.
19+
*
20+
* @link https://stripe.com/docs/api#create_card_token
21+
*/
22+
class CreateTokenRequest extends AbstractRequest
23+
{
24+
/**
25+
* @inheritdoc
26+
*
27+
* @param \Omnipay\Common\CreditCard $value Credit card object
28+
* @return \Omnipay\Common\Message\AbstractRequest $this
29+
*/
30+
public function setCard($value)
31+
{
32+
return parent::setCard($value);
33+
}
34+
35+
/**
36+
* The id of the customer with format cus_<identifier>.
37+
* <strong>Only use this if you are using Connect API</strong>
38+
*
39+
* @param string $customer The id of the customer
40+
*/
41+
public function setCustomer($customer)
42+
{
43+
$this->setParameter('customer', $customer);
44+
}
45+
46+
/**
47+
* Get the raw data array for this message. The format of this varies from gateway to
48+
* gateway, but will usually be either an associative array, or a SimpleXMLElement.
49+
* @return mixed
50+
* @throws InvalidRequestException
51+
*/
52+
public function getData()
53+
{
54+
$data = array();
55+
56+
if ($this->getParameter('customer')) {
57+
$data['customer'] = $this->getParameter('customer');
58+
} elseif ($this->getParameter('card')) {
59+
$data['card'] = $this->getParameter('card');
60+
} else {
61+
throw new InvalidRequestException("You must pass either the card or the customer");
62+
}
63+
64+
return $data;
65+
}
66+
67+
/**
68+
* @inheritdoc
69+
*
70+
* @return string The endpoint for the create token request.
71+
*/
72+
public function getEndpoint()
73+
{
74+
return $this->endpoint . '/tokens';
75+
}
76+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: pedro
5+
* Date: 18/06/17
6+
* Time: 21:30
7+
*/
8+
9+
namespace Omnipay\Stripe\Message;
10+
11+
12+
use Omnipay\Tests\TestCase;
13+
14+
class CreateTokenRequestTest extends TestCase
15+
{
16+
/**
17+
* @var CreateTokenRequest $request
18+
*/
19+
private $request;
20+
21+
public function setUp()
22+
{
23+
parent::setUp();
24+
$this->request = new CreateTokenRequest($this->getHttpClient(), $this->getHttpRequest());
25+
26+
$this->request->setCustomer('cus_example123');
27+
$this->request->setConnectedStripeAccountHeader('acct_12oh2oi3');
28+
}
29+
30+
public function testEndpoint()
31+
{
32+
$this->assertSame('https://api.stripe.com/v1/tokens', $this->request->getEndpoint());
33+
}
34+
35+
/**
36+
* @expectedException \Omnipay\Common\Exception\InvalidRequestException
37+
* @expectedExceptionMessage You must pass either the card or the customer
38+
*/
39+
public function testGetDataInvalid()
40+
{
41+
$this->request->setCustomer(null);
42+
$this->request->setCard(null);
43+
44+
$this->request->getData();
45+
}
46+
47+
public function getDataWithCard()
48+
{
49+
$card = $this->getValidCard();
50+
$this->request->setCard($card);
51+
52+
$data = $this->request->getData();
53+
54+
$this->assertSame($card['number'], $data['card']['number']);
55+
}
56+
57+
public function testResponseFailure()
58+
{
59+
$this->setMockHttpResponse('CreateTokenFailure.txt');
60+
$response = $this->request->send();
61+
62+
$this->assertFalse($response->isSuccessful());
63+
64+
$this->assertNull($response->getTransactionReference());
65+
}
66+
67+
public function testResponseSuccess()
68+
{
69+
$this->setMockHttpResponse('CreateTokenSuccess.txt');
70+
$response = $this->request->send();
71+
72+
$data = $response->getData();
73+
$this->assertTrue($response->isSuccessful());
74+
$this->assertFalse($response->isRedirect());
75+
$this->assertNull($response->getTransactionReference());
76+
$this->assertSame('tok_1AWDl1JqXiFraDuL2xOKEXKy', $data['id']);
77+
$this->assertSame('token', $data['object']);
78+
}
79+
80+
81+
}

tests/Mock/CreateTokenFailure.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
HTTP/1.1 400 Bad Request
2+
Server: nginx
3+
Date: Mon, 19 Jun 2017 00:29:43 GMT
4+
Content-Type: application/json
5+
Content-Length: 140
6+
Connection: keep-alive
7+
Access-Control-Allow-Credentials: true
8+
Access-Control-Allow-Methods: GET, POST, HEAD, OPTIONS, DELETE
9+
Access-Control-Allow-Origin: *
10+
Access-Control-Max-Age: 300
11+
Cache-Control: no-cache, no-store
12+
Request-Id: req_Arxhy7yn9a1MsY
13+
Stripe-Account: acct_19jzyNJqXiFraDuL
14+
Stripe-Version: 2015-04-07
15+
16+
{
17+
"error": {
18+
"type": "invalid_request_error",
19+
"message": "No such customer: cus_ArtrMQYPgb0QaUasd",
20+
"param": "customer"
21+
}
22+
}

tests/Mock/CreateTokenSuccess.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
HTTP/1.1 200 OK
2+
Server: nginx
3+
Date: Mon, 19 Jun 2017 00:28:43 GMT
4+
Content-Type: application/json
5+
Content-Length: 798
6+
Connection: keep-alive
7+
Access-Control-Allow-Credentials: true
8+
Access-Control-Allow-Methods: GET, POST, HEAD, OPTIONS, DELETE
9+
Access-Control-Allow-Origin: *
10+
Access-Control-Max-Age: 300
11+
Cache-Control: no-cache, no-store
12+
Request-Id: req_ArxgXN0W9YsXss
13+
Stripe-Account: acct_14901h0a0fh01293
14+
Stripe-Version: 2015-04-07
15+
Strict-Transport-Security: max-age=31556926; includeSubDomains
16+
17+
{
18+
"id": "tok_1AWDl1JqXiFraDuL2xOKEXKy",
19+
"object": "token",
20+
"card": {
21+
"id": "card_1AWDl1JqXiFraDuL8KJpKlxe",
22+
"object": "card",
23+
"address_city": null,
24+
"address_country": null,
25+
"address_line1": null,
26+
"address_line1_check": null,
27+
"address_line2": null,
28+
"address_state": null,
29+
"address_zip": null,
30+
"address_zip_check": null,
31+
"brand": "Visa",
32+
"country": "US",
33+
"currency": "usd",
34+
"cvc_check": null,
35+
"dynamic_last4": null,
36+
"exp_month": 8,
37+
"exp_year": 2019,
38+
"fingerprint": "OfIzYIod5lokCObt",
39+
"funding": "credit",
40+
"last4": "4242",
41+
"metadata": {},
42+
"name": null,
43+
"tokenization_method": null
44+
},
45+
"client_ip": "179.24.124.12",
46+
"created": 1497832123,
47+
"livemode": false,
48+
"type": "card",
49+
"used": false
50+
}

0 commit comments

Comments
 (0)