本文整理汇总了Python中djstripe.models.Charge类的典型用法代码示例。如果您正苦于以下问题:Python Charge类的具体用法?Python Charge怎么用?Python Charge使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Charge类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_calculate_refund_amount_full_refund
def test_calculate_refund_amount_full_refund(self):
charge = Charge(
stripe_id="ch_111111",
customer=self.customer,
amount=decimal.Decimal("500.00")
)
self.assertEqual(charge._calculate_refund_amount(), 50000)
示例2: test_sync_from_stripe_data_no_customer
def test_sync_from_stripe_data_no_customer(
self, charge_retrieve_mock, balance_transaction_retrieve_mock, default_account_mock
):
from djstripe.settings import STRIPE_SECRET_KEY
default_account_mock.return_value = self.account
fake_charge_copy = deepcopy(FAKE_CHARGE)
fake_charge_copy.pop("customer", None)
# remove invoice since it requires a customer
fake_charge_copy.pop("invoice", None)
Charge.sync_from_stripe_data(fake_charge_copy)
assert Charge.objects.count() == 1
charge = Charge.objects.get()
assert charge.customer is None
charge_retrieve_mock.assert_not_called()
balance_transaction_retrieve_mock.assert_called_once_with(
api_key=STRIPE_SECRET_KEY, expand=[], id=FAKE_BALANCE_TRANSACTION["id"]
)
self.assert_fks(
charge,
expected_blank_fks={
"djstripe.Account.business_logo",
"djstripe.Charge.customer",
"djstripe.Charge.dispute",
"djstripe.Charge.invoice",
"djstripe.Charge.transfer",
"djstripe.Customer.coupon",
"djstripe.Plan.product",
},
)
示例3: test_calculate_refund_above_max_refund
def test_calculate_refund_above_max_refund(self):
charge = Charge(
id="ch_111111", customer=self.customer, amount=decimal.Decimal("500.00")
)
self.assertEqual(
charge._calculate_refund_amount(amount=decimal.Decimal("600.00")), 50000
)
示例4: test__attach_objects_hook_no_destination_account
def test__attach_objects_hook_no_destination_account(
self, mock_account, mock_payment_method, mock_charge_account, mock_charge_source
):
"""Test that _attach_objects_hook works as expected when there is no destination account."""
charge = Charge(
amount=50,
currency="usd",
id="ch_test",
status=ChargeStatus.failed,
captured=False,
paid=False,
)
mock_cls = create_autospec(spec=Charge, spec_set=True)
mock_cls._stripe_object_destination_to_account.return_value = False
mock_data = {"source": {"object": "foo"}}
mock_payment_method._get_or_create_source.return_value = ("bar", "unused")
charge._attach_objects_hook(cls=mock_cls, data=mock_data)
# expect the attributes to be set appropriately
self.assertEqual(mock_account.get_default_account.return_value, charge.account)
self.assertEqual(
mock_payment_method._get_or_create_source.return_value[0], charge.source
)
# expect the appropriate calls to be made
mock_cls._stripe_object_destination_to_account.assert_called_once_with(
target_cls=mock_account, data=mock_data
)
mock_payment_method._get_or_create_source.assert_called_once_with(
data=mock_data["source"], source_type=mock_data["source"]["object"]
)
示例5: test__attach_objects_hook_missing_source_data
def test__attach_objects_hook_missing_source_data(
self, mock_account, mock_payment_method, mock_charge_account, mock_charge_source
):
"""Make sure we handle the case where the source data is empty or insufficient."""
charge = Charge(
amount=50,
currency="usd",
id="ch_test",
status=ChargeStatus.failed,
captured=False,
paid=False,
)
mock_cls = create_autospec(spec=Charge, spec_set=True)
# Empty data dict works for this test since we only look up the source key and
# everything else is mocked.
mock_data = {}
starting_source = charge.source
charge._attach_objects_hook(cls=mock_cls, data=mock_data)
# source shouldn't be touched
self.assertEqual(starting_source, charge.source)
mock_payment_method._get_or_create_source.assert_not_called()
# try again with a source key, but no object sub key.
mock_data = {"source": {"foo": "bar"}}
charge._attach_objects_hook(cls=mock_cls, data=mock_data)
# source shouldn't be touched
self.assertEqual(starting_source, charge.source)
mock_payment_method._get_or_create_source.assert_not_called()
示例6: test_sync_from_stripe_data_no_customer
def test_sync_from_stripe_data_no_customer(self, default_account_mock):
default_account_mock.return_value = self.account
fake_charge_copy = deepcopy(FAKE_CHARGE)
fake_charge_copy.pop("customer", None)
Charge.sync_from_stripe_data(fake_charge_copy)
assert Charge.objects.count() == 1
charge = Charge.objects.get()
assert charge.customer is None
示例7: test_calculate_refund_amount_partial_refund
def test_calculate_refund_amount_partial_refund(self):
charge = Charge(
stripe_id="ch_111111",
customer=self.customer,
amount=decimal.Decimal("500.00")
)
self.assertEquals(
charge.calculate_refund_amount(amount=decimal.Decimal("300.00")),
30000
)
示例8: test_str
def test_str(self):
charge = Charge(
amount=50,
currency="usd",
id="ch_test",
status=ChargeStatus.failed,
captured=False,
paid=False,
)
self.assertEqual(str(charge), "$50.00 USD (Uncaptured)")
charge.captured = True
self.assertEqual(str(charge), "$50.00 USD (Failed)")
charge.status = ChargeStatus.succeeded
charge.dispute = Dispute()
self.assertEqual(str(charge), "$50.00 USD (Disputed)")
charge.dispute = None
charge.refunded = True
charge.amount_refunded = 50
self.assertEqual(str(charge), "$50.00 USD (Refunded)")
charge.refunded = False
self.assertEqual(str(charge), "$50.00 USD (Partially refunded)")
charge.amount_refunded = 0
self.assertEqual(str(charge), "$50.00 USD")
示例9: test_sync_from_stripe_data_max_amount
def test_sync_from_stripe_data_max_amount(
self,
default_account_mock,
subscription_retrieve_mock,
product_retrieve_mock,
invoice_retrieve_mock,
charge_retrieve_mock,
balance_transaction_retrieve_mock,
):
default_account_mock.return_value = self.account
fake_charge_copy = deepcopy(FAKE_CHARGE)
# https://support.stripe.com/questions/what-is-the-maximum-amount-i-can-charge-with-stripe
fake_charge_copy.update({"amount": 99999999})
charge = Charge.sync_from_stripe_data(fake_charge_copy)
self.assertEqual(Decimal("999999.99"), charge.amount)
self.assertEqual(True, charge.paid)
self.assertEqual(False, charge.refunded)
self.assertEqual(True, charge.captured)
self.assertEqual(False, charge.disputed)
self.assertEqual(0, charge.amount_refunded)
charge_retrieve_mock.assert_not_called()
self.assert_fks(
charge,
expected_blank_fks={
"djstripe.Account.business_logo",
"djstripe.Charge.dispute",
"djstripe.Charge.transfer",
"djstripe.Customer.coupon",
},
)
示例10: test_capture_charge
def test_capture_charge(
self, balance_transaction_retrieve_mock, charge_retrieve_mock, default_account_mock
):
default_account_mock.return_value = self.account
fake_charge_no_invoice = deepcopy(FAKE_CHARGE)
fake_charge_no_invoice.update({"invoice": None})
charge_retrieve_mock.return_value = fake_charge_no_invoice
charge, created = Charge._get_or_create_from_stripe_object(fake_charge_no_invoice)
self.assertTrue(created)
captured_charge = charge.capture()
self.assertTrue(captured_charge.captured)
self.assert_fks(
charge,
expected_blank_fks={
"djstripe.Account.business_logo",
"djstripe.Charge.dispute",
"djstripe.Charge.invoice",
"djstripe.Charge.transfer",
"djstripe.Customer.coupon",
"djstripe.Plan.product",
},
)
示例11: sync_charges
def sync_charges(apps, schema_editor):
# This is okay, since we're only doing a forward migration.
from djstripe.models import Charge
from djstripe.context_managers import stripe_temporary_api_version
with stripe_temporary_api_version("2016-03-07"):
if Charge.objects.count():
print("syncing charges. This may take a while.")
for charge in tqdm(Charge.objects.all(), desc="Sync", unit=" charges"):
try:
Charge.sync_from_stripe_data(charge.api_retrieve())
except InvalidRequestError:
tqdm.write("There was an error while syncing charge ({charge_id}).".format(charge_id=charge.stripe_id))
print("Charge sync complete.")
示例12: test_refund_charge
def test_refund_charge(self, charge_retrieve_mock, default_account_mock):
default_account_mock.return_value = self.account
fake_charge_no_invoice = deepcopy(FAKE_CHARGE)
fake_charge_no_invoice.update({"invoice": None})
charge_retrieve_mock.return_value = fake_charge_no_invoice
charge, created = Charge._get_or_create_from_stripe_object(fake_charge_no_invoice)
self.assertTrue(created)
charge.refund()
refunded_charge, created2 = Charge._get_or_create_from_stripe_object(fake_charge_no_invoice)
self.assertFalse(created2)
self.assertEqual(refunded_charge.refunded, True)
self.assertEqual(refunded_charge.amount_refunded, decimal.Decimal("22.00"))
示例13: test_refund_charge
def test_refund_charge(
self, charge_retrieve_mock, balance_transaction_retrieve_mock, default_account_mock
):
default_account_mock.return_value = self.account
fake_charge_no_invoice = deepcopy(FAKE_CHARGE)
fake_charge_no_invoice.update({"invoice": None})
charge_retrieve_mock.return_value = fake_charge_no_invoice
charge, created = Charge._get_or_create_from_stripe_object(fake_charge_no_invoice)
self.assertTrue(created)
self.assert_fks(
charge,
expected_blank_fks={
"djstripe.Account.business_logo",
"djstripe.Charge.dispute",
"djstripe.Charge.invoice",
"djstripe.Charge.transfer",
"djstripe.Customer.coupon",
},
)
charge.refund()
refunded_charge, created2 = Charge._get_or_create_from_stripe_object(
fake_charge_no_invoice
)
self.assertFalse(created2)
self.assertEqual(refunded_charge.refunded, True)
self.assertEqual(refunded_charge.amount_refunded, decimal.Decimal("22.00"))
self.assert_fks(
refunded_charge,
expected_blank_fks={
"djstripe.Account.business_logo",
"djstripe.Charge.dispute",
"djstripe.Charge.invoice",
"djstripe.Charge.transfer",
"djstripe.Customer.coupon",
},
)
示例14: test_sync_from_stripe_data_refunded
def test_sync_from_stripe_data_refunded(
self,
subscription_retrieve_mock,
product_retrieve_mock,
invoice_retrieve_mock,
charge_retrieve_mock,
balance_transaction_retrieve_mock,
default_account_mock,
):
from djstripe.settings import STRIPE_SECRET_KEY
default_account_mock.return_value = self.account
fake_charge_copy = deepcopy(FAKE_CHARGE_REFUNDED)
charge = Charge.sync_from_stripe_data(fake_charge_copy)
self.assertEqual(Decimal("22"), charge.amount)
self.assertEqual(True, charge.paid)
self.assertEqual(True, charge.refunded)
self.assertEqual(True, charge.captured)
self.assertEqual(False, charge.disputed)
self.assertEqual("VideoDoc consultation for ivanp0001 berkp0001", charge.description)
self.assertEqual(charge.amount, charge.amount_refunded)
charge_retrieve_mock.assert_not_called()
# We expect two calls - for charge and then for charge.refunds
balance_transaction_retrieve_mock.assert_has_calls(
[
call(api_key=STRIPE_SECRET_KEY, expand=[], id=FAKE_BALANCE_TRANSACTION["id"]),
call(
api_key=STRIPE_SECRET_KEY, expand=[], id=FAKE_BALANCE_TRANSACTION_REFUND["id"]
),
]
)
refunds = list(charge.refunds.all())
self.assertEqual(len(refunds), 1)
refund = refunds[0]
self.assertEqual(refund.id, FAKE_REFUND["id"])
self.assertNotEqual(charge.balance_transaction.id, refund.balance_transaction.id)
self.assertEqual(charge.balance_transaction.id, FAKE_BALANCE_TRANSACTION["id"])
self.assertEqual(refund.balance_transaction.id, FAKE_BALANCE_TRANSACTION_REFUND["id"])
self.assert_fks(
charge,
expected_blank_fks={
"djstripe.Account.business_logo",
"djstripe.Charge.dispute",
"djstripe.Charge.transfer",
"djstripe.Customer.coupon",
},
)
示例15: test_sync_from_stripe_data_unsupported_source
def test_sync_from_stripe_data_unsupported_source(self, default_account_mock):
default_account_mock.return_value = self.account
fake_charge_copy = deepcopy(FAKE_CHARGE)
fake_charge_copy.update({"source": {"id": "test_id", "object": "unsupported"}})
charge = Charge.sync_from_stripe_data(fake_charge_copy)
self.assertEqual("test_id", charge.source_stripe_id)
self.assertEqual("unsupported", charge.source_type)
self.assertEqual(charge.source, PaymentMethod.objects.get(id="test_id"))