-
Notifications
You must be signed in to change notification settings - Fork 863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for the PaymentIntent resource #487
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace Stripe; | ||
|
||
/** | ||
* Class PaymentIntent | ||
* | ||
* @property string $id | ||
* @property string $object | ||
* @property string[] $allowed_source_types | ||
* @property int $amount | ||
* @property int $amount_capturable | ||
* @property int $amount_received | ||
* @property string $application | ||
* @property int $application_fee | ||
* @property int $canceled_at | ||
* @property string $capture_method | ||
* @property Collection $charges | ||
* @property string $client_secret | ||
* @property int $created | ||
* @property string $currency | ||
* @property string $customer | ||
* @property string $description | ||
* @property bool $livemode | ||
* @property StripeObject $metadata | ||
* @property mixed $next_source_action | ||
* @property string $on_behalf_of | ||
* @property string $receipt_email | ||
* @property string $return_url | ||
* @property mixed $shipping | ||
* @property string $source | ||
* @property string $statement_descriptor | ||
* @property string $status | ||
* @property mixed $transfer_data | ||
* @property string $transfer_group | ||
* | ||
* @package Stripe | ||
*/ | ||
class PaymentIntent extends ApiResource | ||
{ | ||
|
||
const OBJECT_NAME = "payment_intent"; | ||
|
||
use ApiOperations\All; | ||
use ApiOperations\Create; | ||
use ApiOperations\Retrieve; | ||
use ApiOperations\Update; | ||
|
||
/** | ||
* This is a special case because the payment intents endpoint has an | ||
* underscore in it. The parent `className` function strips underscores. | ||
* | ||
* @return string The name of the class. | ||
*/ | ||
public static function className() | ||
{ | ||
return 'payment_intent'; | ||
} | ||
|
||
/** | ||
* @param array|null $params | ||
* @param array|string|null $options | ||
* | ||
* @return PaymentIntent The canceled payment intent. | ||
*/ | ||
public function cancel($params = null, $options = null) | ||
{ | ||
$url = $this->instanceUrl() . '/cancel'; | ||
list($response, $opts) = $this->_request('post', $url); | ||
$this->refreshFrom($response, $opts); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param array|null $params | ||
* @param array|string|null $options | ||
* | ||
* @return PaymentIntent The captured payment intent. | ||
*/ | ||
public function capture($params = null, $options = null) | ||
{ | ||
$url = $this->instanceUrl() . '/capture'; | ||
list($response, $opts) = $this->_request('post', $url); | ||
$this->refreshFrom($response, $opts); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param array|null $params | ||
* @param array|string|null $options | ||
* | ||
* @return PaymentIntent The confirmed payment intent. | ||
*/ | ||
public function confirm($params = null, $options = null) | ||
{ | ||
$url = $this->instanceUrl() . '/confirm'; | ||
list($response, $opts) = $this->_request('post', $url); | ||
$this->refreshFrom($response, $opts); | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
namespace Stripe; | ||
|
||
class PaymentIntentTest extends TestCase | ||
{ | ||
const TEST_RESOURCE_ID = 'pi_123'; | ||
|
||
// stripe-mock does not support /v1/payment_intents yet so we stub it | ||
// and create a fixture for it | ||
public function createFixture() | ||
{ | ||
$base = [ | ||
'id' => self::TEST_RESOURCE_ID, | ||
'object' => 'payment_intent', | ||
'metadata' => [], | ||
]; | ||
return PaymentIntent::constructFrom( | ||
$base, | ||
new Util\RequestOptions() | ||
); | ||
} | ||
|
||
public function testIsListable() | ||
{ | ||
$this->stubRequest( | ||
'get', | ||
'/v1/payment_intents', | ||
[], | ||
null, | ||
false, | ||
[ | ||
"object" => "list", | ||
"data" => [ | ||
$this->createFixture() | ||
] | ||
] | ||
); | ||
$resources = PaymentIntent::all(); | ||
$this->assertTrue(is_array($resources->data)); | ||
$this->assertInstanceOf("Stripe\\PaymentIntent", $resources->data[0]); | ||
} | ||
|
||
public function testIsRetrievable() | ||
{ | ||
$this->stubRequest( | ||
'get', | ||
'/v1/payment_intents/' . self::TEST_RESOURCE_ID, | ||
[], | ||
null, | ||
false, | ||
$this->createFixture() | ||
); | ||
$resource = PaymentIntent::retrieve(self::TEST_RESOURCE_ID); | ||
$this->assertInstanceOf("Stripe\\PaymentIntent", $resource); | ||
} | ||
|
||
public function testIsCreatable() | ||
{ | ||
$params = [ | ||
"allowed_source_types" => ["card"], | ||
"amount" => 100, | ||
"currency" => "usd", | ||
]; | ||
|
||
$this->stubRequest( | ||
'post', | ||
'/v1/payment_intents', | ||
$params, | ||
null, | ||
false, | ||
$this->createFixture() | ||
); | ||
$resource = PaymentIntent::create($params); | ||
$this->assertInstanceOf("Stripe\\PaymentIntent", $resource); | ||
} | ||
|
||
public function testIsSaveable() | ||
{ | ||
$params = [ | ||
"metadata" => ["key" => "value"], | ||
]; | ||
|
||
$resource = $this->createFixture(); | ||
$resource->metadata["key"] = "value"; | ||
$this->stubRequest( | ||
'post', | ||
'/v1/payment_intents/' . $resource->id, | ||
$params, | ||
null, | ||
false, | ||
$this->createFixture() | ||
); | ||
$resource->save(); | ||
$this->assertInstanceOf("Stripe\\PaymentIntent", $resource); | ||
} | ||
|
||
public function testIsUpdatable() | ||
{ | ||
$params = [ | ||
"metadata" => ["key" => "value"], | ||
]; | ||
|
||
$this->stubRequest( | ||
'post', | ||
'/v1/payment_intents/' . self::TEST_RESOURCE_ID, | ||
$params, | ||
null, | ||
false, | ||
$this->createFixture() | ||
); | ||
$resource = PaymentIntent::update(self::TEST_RESOURCE_ID, $params); | ||
$this->assertInstanceOf("Stripe\\PaymentIntent", $resource); | ||
} | ||
|
||
public function testIsCancelable() | ||
{ | ||
$resource = $this->createFixture(); | ||
$this->stubRequest( | ||
'post', | ||
'/v1/payment_intents/' . $resource->id . '/cancel', | ||
[], | ||
null, | ||
false, | ||
$this->createFixture() | ||
); | ||
$resource->cancel(); | ||
$this->assertInstanceOf("Stripe\\PaymentIntent", $resource); | ||
} | ||
|
||
public function testIsCapturable() | ||
{ | ||
$resource = $this->createFixture(); | ||
$this->stubRequest( | ||
'post', | ||
'/v1/payment_intents/' . $resource->id . '/capture', | ||
[], | ||
null, | ||
false, | ||
$this->createFixture() | ||
); | ||
$resource->capture(); | ||
$this->assertInstanceOf("Stripe\\PaymentIntent", $resource); | ||
} | ||
|
||
public function testIsConfirmable() | ||
{ | ||
$resource = $this->createFixture(); | ||
$this->stubRequest( | ||
'post', | ||
'/v1/payment_intents/' . $resource->id . '/confirm', | ||
[], | ||
null, | ||
false, | ||
$this->createFixture() | ||
); | ||
$resource->confirm(); | ||
$this->assertInstanceOf("Stripe\\PaymentIntent", $resource); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor but I spent 20 minutes on figuring out that I was missing this again. I wonder if we can/should use
OBJECT_NAME
instead.