Skip to content

Commit

Permalink
Add support for the PaymentMethod resource
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed Mar 18, 2019
1 parent 570d47d commit 39bfbdb
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ php:

env:
global:
- STRIPE_MOCK_VERSION=0.44.0
- STRIPE_MOCK_VERSION=0.49.0
matrix:
- AUTOLOAD=1
- AUTOLOAD=0
Expand Down
1 change: 1 addition & 0 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
require(dirname(__FILE__) . '/lib/OrderItem.php');
require(dirname(__FILE__) . '/lib/OrderReturn.php');
require(dirname(__FILE__) . '/lib/PaymentIntent.php');
require(dirname(__FILE__) . '/lib/PaymentMethod.php');
require(dirname(__FILE__) . '/lib/Payout.php');
require(dirname(__FILE__) . '/lib/Person.php');
require(dirname(__FILE__) . '/lib/Plan.php');
Expand Down
4 changes: 2 additions & 2 deletions lib/PaymentIntent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @property int $amount_capturable
* @property int $amount_received
* @property string $application
* @property int $application_fee
* @property int $application_fee_amount
* @property int $canceled_at
* @property string $cancellation_reason
* @property string $capture_method
Expand All @@ -27,9 +27,9 @@
* @property StripeObject $metadata
* @property mixed $next_action
* @property string $on_behalf_of
* @property string $payment_method
* @property string[] $payment_method_types
* @property string $receipt_email
* @property string $return_url
* @property string $review
* @property mixed $shipping
* @property string $source
Expand Down
60 changes: 60 additions & 0 deletions lib/PaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Stripe;

/**
* Class PaymentMethod
*
* @property string $id
* @property string $object
* @property mixed $billing_details
* @property mixed $card
* @property mixed $card_present
* @property int $created
* @property string $customer
* @property mixed $ideal
* @property bool $livemode
* @property StripeObject $metadata
* @property mixed $sepa_debit
* @property string $type
*
* @package Stripe
*/
class PaymentMethod extends ApiResource
{

const OBJECT_NAME = "payment_method";

use ApiOperations\All;
use ApiOperations\Create;
use ApiOperations\Retrieve;
use ApiOperations\Update;

/**
* @param array|null $params
* @param array|string|null $opts
*
* @return PaymentMethod The attached payment method.
*/
public function attach($params = null, $opts = null)
{
$url = $this->instanceUrl() . '/attach';
list($response, $opts) = $this->_request('post', $url, $params, $opts);
$this->refreshFrom($response, $opts);
return $this;
}

/**
* @param array|null $params
* @param array|string|null $opts
*
* @return PaymentMethod The detached payment method.
*/
public function detach($params = null, $opts = null)
{
$url = $this->instanceUrl() . '/detach';
list($response, $opts) = $this->_request('post', $url, $params, $opts);
$this->refreshFrom($response, $opts);
return $this;
}
}
1 change: 1 addition & 0 deletions lib/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public static function convertToStripeObject($resp, $opts)
\Stripe\OrderItem::OBJECT_NAME => 'Stripe\\OrderItem',
\Stripe\OrderReturn::OBJECT_NAME => 'Stripe\\OrderReturn',
\Stripe\PaymentIntent::OBJECT_NAME => 'Stripe\\PaymentIntent',
\Stripe\PaymentMethod::OBJECT_NAME => 'Stripe\\PaymentMethod',
\Stripe\Payout::OBJECT_NAME => 'Stripe\\Payout',
\Stripe\Person::OBJECT_NAME => 'Stripe\\Person',
\Stripe\Plan::OBJECT_NAME => 'Stripe\\Plan',
Expand Down
94 changes: 94 additions & 0 deletions tests/Stripe/PaymentMethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Stripe;

class PaymentMethodTest extends TestCase
{
const TEST_RESOURCE_ID = 'pm_123';

public function testIsListable()
{
$this->expectsRequest(
'get',
'/v1/payment_methods'
);
$resources = PaymentMethod::all([
'customer' => 'cus_123',
'type' => 'card',
]);
$this->assertTrue(is_array($resources->data));
$this->assertInstanceOf("Stripe\\PaymentMethod", $resources->data[0]);
}

public function testIsRetrievable()
{
$this->expectsRequest(
'get',
'/v1/payment_methods/' . self::TEST_RESOURCE_ID
);
$resource = PaymentMethod::retrieve(self::TEST_RESOURCE_ID);
$this->assertInstanceOf("Stripe\\PaymentMethod", $resource);
}

public function testIsCreatable()
{
$this->expectsRequest(
'post',
'/v1/payment_methods'
);
$resource = PaymentMethod::create([
'type' => 'card',
]);
$this->assertInstanceOf("Stripe\\PaymentMethod", $resource);
}

public function testIsSaveable()
{
$resource = PaymentMethod::retrieve(self::TEST_RESOURCE_ID);
$resource->metadata["key"] = "value";
$this->expectsRequest(
'post',
'/v1/payment_methods/' . $resource->id
);
$resource->save();
$this->assertInstanceOf("Stripe\\PaymentMethod", $resource);
}

public function testIsUpdatable()
{
$this->expectsRequest(
'post',
'/v1/payment_methods/' . self::TEST_RESOURCE_ID
);
$resource = PaymentMethod::update(self::TEST_RESOURCE_ID, [
"metadata" => ["key" => "value"],
]);
$this->assertInstanceOf("Stripe\\PaymentMethod", $resource);
}

public function testCanAttach()
{
$resource = PaymentMethod::retrieve(self::TEST_RESOURCE_ID);
$this->expectsRequest(
'post',
'/v1/payment_methods/' . $resource->id . '/attach'
);
$resource = $resource->attach([
'customer' => 'cus_123',
]);
$this->assertInstanceOf("Stripe\\PaymentMethod", $resource);
$this->assertSame($resource, $resource);
}

public function testCanDetach()
{
$resource = PaymentMethod::retrieve(self::TEST_RESOURCE_ID);
$this->expectsRequest(
'post',
'/v1/payment_methods/' . $resource->id . '/detach'
);
$resource = $resource->detach();
$this->assertInstanceOf("Stripe\\PaymentMethod", $resource);
$this->assertSame($resource, $resource);
}
}
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require_once(__DIR__ . '/StripeMock.php');

define("MOCK_MINIMUM_VERSION", "0.44.0");
define("MOCK_MINIMUM_VERSION", "0.49.0");

if (\Stripe\StripeMock::start()) {
register_shutdown_function('\Stripe\StripeMock::stop');
Expand Down

0 comments on commit 39bfbdb

Please sign in to comment.