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 Feb 25, 2019
1 parent 570d47d commit 7e54379
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
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
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
100 changes: 100 additions & 0 deletions tests/Stripe/PaymentMethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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();
$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([
"customer" => "cus_123"
]);
$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 testIsDeletable()
{
$resource = PaymentMethod::retrieve(self::TEST_RESOURCE_ID);
$this->expectsRequest(
'delete',
'/v1/payment_methods/' . self::TEST_RESOURCE_ID
);
$resource->delete();
$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->finalizePaymentMethod();
$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->markUncollectible();
$this->assertInstanceOf("Stripe\\PaymentMethod", $resource);
$this->assertSame($resource, $resource);
}
}

0 comments on commit 7e54379

Please sign in to comment.