Skip to content

Commit

Permalink
Add support for the PaymentIntent resource
Browse files Browse the repository at this point in the history
This is gated so for now the tests are stubbed.
  • Loading branch information
remi-stripe authored and ob-stripe committed Jun 28, 2018
1 parent 735511c commit 4c44062
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 0 deletions.
1 change: 1 addition & 0 deletions stripe/api_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from stripe.api_resources.login_link import LoginLink
from stripe.api_resources.order import Order
from stripe.api_resources.order_return import OrderReturn
from stripe.api_resources.payment_intent import PaymentIntent
from stripe.api_resources.payout import Payout
from stripe.api_resources.plan import Plan
from stripe.api_resources.product import Product
Expand Down
33 changes: 33 additions & 0 deletions stripe/api_resources/payment_intent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from __future__ import absolute_import, division, print_function

from stripe import util
from stripe.api_resources.abstract import CreateableAPIResource
from stripe.api_resources.abstract import UpdateableAPIResource
from stripe.api_resources.abstract import ListableAPIResource


class PaymentIntent(CreateableAPIResource, UpdateableAPIResource,
ListableAPIResource):
OBJECT_NAME = 'payment_intent'

@classmethod
def class_name(cls):
return 'payment_intent'

def cancel(self, idempotency_key=None, **params):
url = self.instance_url() + '/cancel'
headers = util.populate_headers(idempotency_key)
self.refresh_from(self.request('post', url, params, headers))
return self

def capture(self, idempotency_key=None, **params):
url = self.instance_url() + '/capture'
headers = util.populate_headers(idempotency_key)
self.refresh_from(self.request('post', url, params, headers))
return self

def confirm(self, idempotency_key=None, **params):
url = self.instance_url() + '/confirm'
headers = util.populate_headers(idempotency_key)
self.refresh_from(self.request('post', url, params, headers))
return self
1 change: 1 addition & 0 deletions stripe/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def load_object_classes():
api_resources.LoginLink.OBJECT_NAME: api_resources.LoginLink,
api_resources.Order.OBJECT_NAME: api_resources.Order,
api_resources.OrderReturn.OBJECT_NAME: api_resources.OrderReturn,
api_resources.PaymentIntent.OBJECT_NAME: api_resources.PaymentIntent,
api_resources.Payout.OBJECT_NAME: api_resources.Payout,
api_resources.Plan.OBJECT_NAME: api_resources.Plan,
api_resources.Product.OBJECT_NAME: api_resources.Product,
Expand Down
149 changes: 149 additions & 0 deletions tests/api_resources/test_payment_intent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
from __future__ import absolute_import, division, print_function

import stripe
from tests.helper import StripeTestCase


TEST_RESOURCE_ID = 'pi_123'


class PaymentIntentTest(StripeTestCase):
FIXTURE = {
'id': TEST_RESOURCE_ID,
'object': 'payment_intent',
'metadata': {},
}

def test_is_listable(self):
self.stub_request(
'get',
'/v1/payment_intents',
{
'object': 'list',
'data': [self.FIXTURE],
}
)

resources = stripe.PaymentIntent.list()
self.assert_requested(
'get',
'/v1/payment_intents'
)
self.assertIsInstance(resources.data, list)
self.assertIsInstance(resources.data[0], stripe.PaymentIntent)

def test_is_retrievable(self):
self.stub_request(
'get',
'/v1/payment_intents/%s' % TEST_RESOURCE_ID,
self.FIXTURE
)

resource = stripe.PaymentIntent.retrieve(TEST_RESOURCE_ID)
self.assert_requested(
'get',
'/v1/payment_intents/%s' % TEST_RESOURCE_ID
)
self.assertIsInstance(resource, stripe.PaymentIntent)

def test_is_creatable(self):
self.stub_request(
'post',
'/v1/payment_intents',
self.FIXTURE
)

resource = stripe.PaymentIntent.create(
allowed_source_types=['card'],
amount='1234',
currency='amount'
)
self.assert_requested(
'post',
'/v1/payment_intents',
)
self.assertIsInstance(resource, stripe.PaymentIntent)

def test_is_modifiable(self):
self.stub_request(
'post',
'/v1/payment_intents/%s' % TEST_RESOURCE_ID,
self.FIXTURE
)

resource = stripe.PaymentIntent.modify(
TEST_RESOURCE_ID,
metadata={'key': 'value'}
)
self.assert_requested(
'post',
'/v1/payment_intents/%s' % TEST_RESOURCE_ID,
{'metadata': {'key': 'value'}}
)
self.assertIsInstance(resource, stripe.PaymentIntent)

def test_is_saveable(self):
resource = stripe.PaymentIntent.construct_from(self.FIXTURE, stripe.api_key)

self.stub_request(
'post',
'/v1/payment_intents/%s' % TEST_RESOURCE_ID,
self.FIXTURE
)

resource.metadata['key'] = 'value'
resource.save()
self.assert_requested(
'post',
'/v1/payment_intents/%s' % resource.id,
{'metadata': {'key': 'value'}}
)
self.assertIsInstance(resource, stripe.PaymentIntent)

def test_can_cancel(self):
resource = stripe.PaymentIntent.construct_from(self.FIXTURE, stripe.api_key)

self.stub_request(
'post',
'/v1/payment_intents/%s/cancel' % TEST_RESOURCE_ID,
self.FIXTURE
)

resource.cancel()
self.assert_requested(
'post',
'/v1/payment_intents/%s/cancel' % TEST_RESOURCE_ID
)
self.assertIsInstance(resource, stripe.PaymentIntent)

def test_can_capture(self):
resource = stripe.PaymentIntent.construct_from(self.FIXTURE, stripe.api_key)

self.stub_request(
'post',
'/v1/payment_intents/%s/capture' % TEST_RESOURCE_ID,
self.FIXTURE
)

resource.capture()
self.assert_requested(
'post',
'/v1/payment_intents/%s/capture' % TEST_RESOURCE_ID
)
self.assertIsInstance(resource, stripe.PaymentIntent)

def test_can_confirm(self):
resource = stripe.PaymentIntent.construct_from(self.FIXTURE, stripe.api_key)

self.stub_request(
'post',
'/v1/payment_intents/%s/confirm' % TEST_RESOURCE_ID,
self.FIXTURE
)

resource.confirm()
self.assert_requested(
'post',
'/v1/payment_intents/%s/confirm' % TEST_RESOURCE_ID
)
self.assertIsInstance(resource, stripe.PaymentIntent)

0 comments on commit 4c44062

Please sign in to comment.