-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/payment-intent' into develop
- Loading branch information
Showing
5 changed files
with
229 additions
and
0 deletions.
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,19 @@ | ||
<?php | ||
|
||
namespace Bulldog\Strype\Contracts\Resources; | ||
|
||
use Bulldog\Strype\Contracts\ResourceInterface; | ||
use Bulldog\Strype\Contracts\Traits\UpdateInterface; | ||
use Bulldog\Strype\Contracts\Traits\ListAllInterface; | ||
use Bulldog\Strype\Contracts\Traits\RetrieveInterface; | ||
|
||
interface PaymentIntentInterface extends ResourceInterface, RetrieveInterface, UpdateInterface, ListAllInterface | ||
{ | ||
public function create(array $allowedSourceTypes, int $amount, array $arguments = [], string $key = null, string $currency = 'usd'): PaymentIntentInterface; | ||
|
||
public function confirm(string $id, array $arguments = []): PaymentIntentInterface; | ||
|
||
public function capture(string $id, array $arguments = []): PaymentIntentInterface; | ||
|
||
public function cancel(string $id, array $arguments = []): PaymentIntentInterface; | ||
} |
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,112 @@ | ||
<?php | ||
|
||
namespace Bulldog\Strype\Resources; | ||
|
||
use Bulldog\Strype\Resource; | ||
use Bulldog\Strype\Traits\Update; | ||
use Bulldog\Strype\Traits\ListAll; | ||
use Bulldog\Strype\Traits\Retrieve; | ||
use Bulldog\Strype\Contracts\Resources\PaymentIntentInterface; | ||
|
||
/** | ||
* A PaymentIntent tracks the process of collecting a payment from your customer. | ||
* We recommend that you create exactly one PaymentIntent for each order or customer | ||
* session in your system. You can reference the PaymentIntent later to see the | ||
* history of payment attempts for a particular session. | ||
* | ||
* @see https://stripe.com/docs/api/payment_intents | ||
*/ | ||
class PaymentIntent extends Resource implements PaymentIntentInterface | ||
{ | ||
use Retrieve, Update, ListAll; | ||
|
||
/** | ||
* Creates a PaymentIntent object. | ||
* | ||
* @see https://stripe.com/docs/api/payment_intents/create | ||
* | ||
* @param array $allowedSourceTypes | ||
* @param int $amount | ||
* @param array $arguments | ||
* @param string $key | ||
* @param string $currency | ||
* | ||
* @return PaymentIntentInterface | ||
*/ | ||
public function create(array $allowedSourceTypes, int $amount, array $arguments = [], string $key = null, string $currency = 'usd'): PaymentIntentInterface | ||
{ | ||
$arguments['allowed_source_types'] = $allowedSourceTypes; | ||
$arguments['amount'] = $amount; | ||
$arguments['currency'] = $currency; | ||
|
||
$this->stripe('create', $arguments, $key); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Confirm that your customer intends to pay with current or provided source. | ||
* Upon confirmation, the PaymentIntent will attempt to initiate a payment. | ||
* | ||
* @see https://stripe.com/docs/api/payment_intents/confirm | ||
* | ||
* @param string $id | ||
* @param array $arguments | ||
* | ||
* @return PaymentIntentInterface | ||
*/ | ||
public function confirm(string $id, array $arguments = []): PaymentIntentInterface | ||
{ | ||
$this->stripe('retrieve', $id); | ||
$this->response->confirm($arguments); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Capture the funds of an existing uncaptured PaymentIntent where | ||
* required_action="requires_capture". | ||
* | ||
* @see https://stripe.com/docs/api/payment_intents/capture | ||
* | ||
* @param string $id | ||
* @param array $arguments | ||
* | ||
* @return PaymentIntentInterface | ||
*/ | ||
public function capture(string $id, array $arguments = []): PaymentIntentInterface | ||
{ | ||
$this->stripe('retrieve', $id); | ||
$this->response->capture($arguments); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* A PaymentIntent object can be canceled when it is in one of these statues: | ||
* requires_source, requires_capture, requires_confirmation, or | ||
* requires_source_action. | ||
* | ||
* @see https://stripe.com/docs/api/payment_intents/cancel | ||
* | ||
* @param string $id | ||
* @param array $arguments | ||
* | ||
* @return PaymentIntentInterface | ||
*/ | ||
public function cancel(string $id, array $arguments = []): PaymentIntentInterface | ||
{ | ||
$this->stripe('retrieve', $id); | ||
$this->response->cancel($arguments); | ||
|
||
return $this; | ||
} | ||
|
||
protected function stripe(string $method, $arguments, string $idempotencyKey = null): void | ||
{ | ||
$this->response = \Stripe\PaymentIntent::{$method}($arguments, [ | ||
'idempotency_key' => $idempotencyKey, | ||
]); | ||
$this->setProperties(); | ||
} | ||
} |
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,89 @@ | ||
<?php | ||
|
||
namespace Strype; | ||
|
||
class PaymentIntentTest extends TestCase | ||
{ | ||
public function testCreatePaymentIntent() | ||
{ | ||
$pi = $this->strype->paymentIntent()->create( | ||
['card'], | ||
9999 | ||
); | ||
|
||
$this->assertEquals('payment_intent', $pi->object); | ||
$this->assertEquals('succeeded', $pi->status); | ||
} | ||
|
||
public function testRetrievePaymentIntent() | ||
{ | ||
$pi = $this->strype->paymentIntent()->create( | ||
['card'], | ||
9999 | ||
); | ||
|
||
$response = $pi->retrieve($pi->id); | ||
$this->assertEquals('payment_intent', $response->object); | ||
$this->assertEquals('succeeded', $response->status); | ||
} | ||
|
||
public function testUpdatePaymentIntent() | ||
{ | ||
$pi = $this->strype->paymentIntent()->create( | ||
['card'], | ||
9999 | ||
); | ||
|
||
$response = $pi->update($pi->id, [ | ||
'description' => 'description', | ||
]); | ||
$this->assertEquals('payment_intent', $response->object); | ||
$this->assertEquals('succeeded', $response->status); | ||
$this->assertEquals('description', 'description'); | ||
} | ||
|
||
public function testConfirmPaymentIntent() | ||
{ | ||
$pi = $this->strype->paymentIntent()->create( | ||
['card'], | ||
9999 | ||
); | ||
|
||
$response = $pi->confirm($pi->id); | ||
$this->assertEquals('payment_intent', $response->object); | ||
$this->assertEquals('succeeded', $response->status); | ||
$this->assertEquals('description', 'description'); | ||
} | ||
|
||
public function testCapturePaymentIntent() | ||
{ | ||
$pi = $this->strype->paymentIntent()->create( | ||
['card'], | ||
9999 | ||
); | ||
|
||
$response = $pi->capture($pi->id); | ||
$this->assertEquals('payment_intent', $response->object); | ||
$this->assertEquals('succeeded', $response->status); | ||
$this->assertEquals('description', 'description'); | ||
} | ||
|
||
public function testCancelPaymentIntent() | ||
{ | ||
$pi = $this->strype->paymentIntent()->create( | ||
['card'], | ||
9999 | ||
); | ||
|
||
$response = $pi->cancel($pi->id); | ||
$this->assertEquals('payment_intent', $response->object); | ||
$this->assertEquals('succeeded', $response->status); | ||
$this->assertEquals('description', 'description'); | ||
} | ||
|
||
public function testListAllPaymentIntent() | ||
{ | ||
$pi = $this->strype->paymentIntent()->listAll(['limit' => 1]); | ||
$this->assertCount(1, $pi->data); | ||
} | ||
} |