Skip to content

Commit

Permalink
Merge branch 'feature/payment-intent' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
levidurfee committed Nov 30, 2018
2 parents 254b308 + ca3dd35 commit 38d5d70
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 0 deletions.
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
<testsuite name="General">
<file>tests/GeneralTest.php</file>
</testsuite>
<testsuite name="PaymentIntent">
<file>tests/PaymentIntentTest.php</file>
</testsuite>
</testsuites>
<filter>
<whitelist>
Expand Down
19 changes: 19 additions & 0 deletions src/Contracts/Resources/PaymentIntentInterface.php
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;
}
112 changes: 112 additions & 0 deletions src/Resources/PaymentIntent.php
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();
}
}
6 changes: 6 additions & 0 deletions src/Strype.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Bulldog\Strype\Resources\InvoiceItem;
use Bulldog\Strype\Resources\UsageRecord;
use Bulldog\Strype\Resources\Subscription;
use Bulldog\Strype\Resources\PaymentIntent;
use Bulldog\Strype\Resources\SubscriptionItem;

/**
Expand Down Expand Up @@ -132,4 +133,9 @@ public function usageRecord()
{
return new UsageRecord();
}

public function paymentIntent()
{
return new PaymentIntent();
}
}
89 changes: 89 additions & 0 deletions tests/PaymentIntentTest.php
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);
}
}

0 comments on commit 38d5d70

Please sign in to comment.