Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the PaymentIntent resource #436

Merged
merged 1 commit into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
153 changes: 153 additions & 0 deletions tests/api_resources/test_payment_intent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
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)