Skip to content

Commit

Permalink
Merge pull request #85 from pedro-stanaka/feature/connect-api-create-…
Browse files Browse the repository at this point in the history
…tokens

Create Tokens - Connect API
  • Loading branch information
delatbabel authored Jul 1, 2017
2 parents 2c0df1d + ccf9051 commit f5d5ef9
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/Message/CreateTokenRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php


namespace Omnipay\Stripe\Message;

use Omnipay\Common\Exception\InvalidRequestException;

/**
* Message which creates a new card token, or in a Connect API
* workflow can be used to share clients between the platform and
* the connected accounts.
*
* Creates a single use token that wraps the details of a credit card.
* This token can be used in place of a credit card dictionary with any API method.
* These tokens can only be used once: by creating a new charge object, or attaching them to a customer.
*
* In most cases, you should create tokens client-side using Checkout, Elements, or our mobile libraries,
* instead of using the API.
*
* @link https://stripe.com/docs/api#create_card_token
*/
class CreateTokenRequest extends AbstractRequest
{
/**
* @inheritdoc
*
* @param \Omnipay\Common\CreditCard $value Credit card object
* @return \Omnipay\Common\Message\AbstractRequest $this
*/
public function setCard($value)
{
return parent::setCard($value);
}

/**
* The id of the customer with format cus_<identifier>.
* <strong>Only use this if you are using Connect API</strong>
*
* @param string $customer The id of the customer
*/
public function setCustomer($customer)
{
$this->setParameter('customer', $customer);
}

/**
* Get the raw data array for this message. The format of this varies from gateway to
* gateway, but will usually be either an associative array, or a SimpleXMLElement.
* @return mixed
* @throws InvalidRequestException
*/
public function getData()
{
$data = array();

if ($this->getParameter('customer')) {
$data['customer'] = $this->getParameter('customer');
} elseif ($this->getParameter('card')) {
$data['card'] = $this->getParameter('card');
} else {
throw new InvalidRequestException("You must pass either the card or the customer");
}

return $data;
}

/**
* @inheritdoc
*
* @return string The endpoint for the create token request.
*/
public function getEndpoint()
{
return $this->endpoint . '/tokens';
}
}
81 changes: 81 additions & 0 deletions tests/Message/CreateTokenRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Created by PhpStorm.
* User: pedro
* Date: 18/06/17
* Time: 21:30
*/

namespace Omnipay\Stripe\Message;


use Omnipay\Tests\TestCase;

class CreateTokenRequestTest extends TestCase
{
/**
* @var CreateTokenRequest $request
*/
private $request;

public function setUp()
{
parent::setUp();
$this->request = new CreateTokenRequest($this->getHttpClient(), $this->getHttpRequest());

$this->request->setCustomer('cus_example123');
$this->request->setConnectedStripeAccountHeader('acct_12oh2oi3');
}

public function testEndpoint()
{
$this->assertSame('https://api.stripe.com/v1/tokens', $this->request->getEndpoint());
}

/**
* @expectedException \Omnipay\Common\Exception\InvalidRequestException
* @expectedExceptionMessage You must pass either the card or the customer
*/
public function testGetDataInvalid()
{
$this->request->setCustomer(null);
$this->request->setCard(null);

$this->request->getData();
}

public function getDataWithCard()
{
$card = $this->getValidCard();
$this->request->setCard($card);

$data = $this->request->getData();

$this->assertSame($card['number'], $data['card']['number']);
}

public function testResponseFailure()
{
$this->setMockHttpResponse('CreateTokenFailure.txt');
$response = $this->request->send();

$this->assertFalse($response->isSuccessful());

$this->assertNull($response->getTransactionReference());
}

public function testResponseSuccess()
{
$this->setMockHttpResponse('CreateTokenSuccess.txt');
$response = $this->request->send();

$data = $response->getData();
$this->assertTrue($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertNull($response->getTransactionReference());
$this->assertSame('tok_1AWDl1JqXiFraDuL2xOKEXKy', $data['id']);
$this->assertSame('token', $data['object']);
}


}
22 changes: 22 additions & 0 deletions tests/Mock/CreateTokenFailure.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
HTTP/1.1 400 Bad Request
Server: nginx
Date: Mon, 19 Jun 2017 00:29:43 GMT
Content-Type: application/json
Content-Length: 140
Connection: keep-alive
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, HEAD, OPTIONS, DELETE
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 300
Cache-Control: no-cache, no-store
Request-Id: req_Arxhy7yn9a1MsY
Stripe-Account: acct_19jzyNJqXiFraDuL
Stripe-Version: 2015-04-07

{
"error": {
"type": "invalid_request_error",
"message": "No such customer: cus_ArtrMQYPgb0QaUasd",
"param": "customer"
}
}
50 changes: 50 additions & 0 deletions tests/Mock/CreateTokenSuccess.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 19 Jun 2017 00:28:43 GMT
Content-Type: application/json
Content-Length: 798
Connection: keep-alive
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, HEAD, OPTIONS, DELETE
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 300
Cache-Control: no-cache, no-store
Request-Id: req_ArxgXN0W9YsXss
Stripe-Account: acct_14901h0a0fh01293
Stripe-Version: 2015-04-07
Strict-Transport-Security: max-age=31556926; includeSubDomains

{
"id": "tok_1AWDl1JqXiFraDuL2xOKEXKy",
"object": "token",
"card": {
"id": "card_1AWDl1JqXiFraDuL8KJpKlxe",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"currency": "usd",
"cvc_check": null,
"dynamic_last4": null,
"exp_month": 8,
"exp_year": 2019,
"fingerprint": "OfIzYIod5lokCObt",
"funding": "credit",
"last4": "4242",
"metadata": {},
"name": null,
"tokenization_method": null
},
"client_ip": "179.24.124.12",
"created": 1497832123,
"livemode": false,
"type": "card",
"used": false
}

0 comments on commit f5d5ef9

Please sign in to comment.