def test_save_stripe_customer_stripe_error(stripe_m, graphql_client3, user_no_stripe): user_email = user_no_stripe.email customer = MagicMock() customer.id = 'customer_id' stripe_m.Customer.create.side_effect = StripeError('stripe error!', code='error_code') token = 'stripe_token' query = """ mutation saveUser ($input: SaveUserInput!) { saveUser(input: $input) {clientMutationId} } """ variables = { 'input': { 'clientMutationId': 'test_id', 'firstName': 'new_test_name', 'stripeToken': token } } # Test res = graphql_client3.post(query, variables) # Check stripe_m.Customer.create.assert_called_once_with( source=token, email=user_email ) assert res['errors'][0]['message'] == 'stripe error!'
def test_process_payment_with_error(mocked_payment_intent, stripe_plugin, payment_stripe_for_checkout, channel_USD): mocked_payment_intent.side_effect = StripeError( message="stripe-error", json_body={"error": "body"}) plugin = stripe_plugin() payment_info = create_payment_information(payment_stripe_for_checkout, ) response = plugin.process_payment(payment_info, None) assert response.is_success is False assert response.action_required is True assert response.kind == TransactionKind.ACTION_TO_CONFIRM assert response.amount == payment_info.amount assert response.currency == payment_info.currency assert response.transaction_id == "" assert response.error == "stripe-error" assert response.raw_response == {"error": "body"} assert response.action_required_data == {"client_secret": None, "id": None} api_key = plugin.config.connection_params["secret_api_key"] mocked_payment_intent.assert_called_once_with( api_key=api_key, amount=price_to_minor_unit(payment_info.amount, payment_info.currency), currency=payment_info.currency, capture_method=AUTOMATIC_CAPTURE_METHOD, metadata={ "channel": channel_USD.slug, "payment_id": payment_info.graphql_payment_id, }, receipt_email=payment_stripe_for_checkout.checkout.email, stripe_version=STRIPE_API_VERSION, )
def test_cant_retrieve_customer(self, customer, sync_customer): customer.retrieve.side_effect = StripeError() transaction_details = {"customer_id": "bar"} encoded = jwt.encode(transaction_details, settings.OCTOBAT_PRIVATE_KEY) data = {"transactionDetails": encoded.decode("utf-8")} self.login(username=self.user.username, password="******") resp = self.client.post(reverse("pinax_stripe_subscription_create"), data, follow=True) customer.retrieve.assert_called_with("bar") self.assertRedirects(resp, reverse("pinax_stripe_subscription_list")) self.assertContains(resp, "Unable to communicate with stripe")
def test_retrieve_payment_intent_stripe_returns_error(mocked_payment_intent): api_key = "api_key" payment_intent_id = "id1234" expected_error = StripeError(message="stripe-error") mocked_payment_intent.retrieve.side_effect = expected_error _, error = retrieve_payment_intent(api_key, payment_intent_id) mocked_payment_intent.retrieve.assert_called_with( payment_intent_id, api_key=api_key, stripe_version=STRIPE_API_VERSION) assert error == expected_error
def test_create_payment_intent_returns_error(mocked_payment_intent): api_key = "api_key" mocked_payment_intent.create.side_effect = StripeError( json_body={"error": "stripe-error"}) intent, error = create_payment_intent(api_key, Decimal(10), "USD") mocked_payment_intent.create.assert_called_with( api_key=api_key, amount="1000", currency="USD", capture_method=AUTOMATIC_CAPTURE_METHOD, stripe_version=STRIPE_API_VERSION, ) assert intent is None assert error
def test_charge_unknown_stripe_error(self, charge_create_mocked): stripe_error_json_body = {"error": {"type": "api_error"}} charge_create_mocked.side_effect = StripeError( json_body=stripe_error_json_body) with self.assertRaises(SystemExit): out = StringIO() sys.stdout = out call_command("charge_stripe") self.charge.refresh_from_db() self.assertFalse(self.success_signal_was_called) self.assertFalse(self.exception_signal_was_called) self.assertFalse(self.charge.is_charged) self.assertFalse(self.charge.charge_attempt_failed) self.assertDictEqual(self.charge.stripe_response, stripe_error_json_body) self.assertIn("Exception happened", out.getvalue())
def test_get_or_create_customer_failed_create(mocked_customer): expected_error = StripeError(message="stripe-error") mocked_customer.create.side_effect = expected_error api_key = "123" customer_email = "*****@*****.**" customer = get_or_create_customer( api_key=api_key, customer_email=customer_email, customer_id=None, ) assert customer is None mocked_customer.create.assert_called_with( email=customer_email, api_key=api_key, stripe_version=STRIPE_API_VERSION)
def test_make_donation_stripe_exception(self, charge_create, customer_create): customer_create.return_value.id = 'xxxx' charge_create.side_effect = StripeError('Payment failed') form = PaymentForm( data={ 'amount': 100, 'stripe_token': 'xxxx', 'token_type': 'card', 'interval': 'onetime', 'receipt_email': '*****@*****.**', }) self.assertTrue(form.is_valid()) with self.assertRaisesMessage(StripeError, 'Payment failed'): donation = form.make_donation() self.assertIsNone(donation) # Hero shouldn't be created. self.assertFalse(DjangoHero.objects.exists())
def test_refund_payment_intent_returns_error(mocked_refund): api_key = "api_key" payment_intent_id = "id1234" amount = price_to_minor_unit(Decimal("10.0"), "USD") expected_error = StripeError(message="stripe-error") mocked_refund.create.side_effect = expected_error _, error = refund_payment_intent(api_key=api_key, payment_intent_id=payment_intent_id, amount_to_refund=amount) mocked_refund.create.assert_called_with( payment_intent=payment_intent_id, amount=amount, api_key=api_key, stripe_version=STRIPE_API_VERSION, ) assert error == expected_error
def test_list_customer_payment_methods_failed_to_fetch(mocked_payment_method): api_key = "123" customer_id = "c_customer_id" expected_error = StripeError(message="stripe-error") mocked_payment_method.list.side_effect = expected_error payment_method, error = list_customer_payment_methods( api_key=api_key, customer_id=customer_id) assert payment_method is None assert isinstance(error, StripeError) mocked_payment_method.list.assert_called_with( api_key=api_key, customer=customer_id, type="card", stripe_version=STRIPE_API_VERSION, )
def test_charge_stripe_error(self, charge_create_mocked): stripe_error_json_body = { "error": { "code": "resource_missing", "doc_url": "https://stripe.com/docs/error-codes/resource-missing", "message": "No such customer: cus_ESrgXHlDA3E7mQ", "param": "customer", "type": "invalid_request_error", } } charge_create_mocked.side_effect = StripeError( json_body=stripe_error_json_body) call_command("charge_stripe") self.charge.refresh_from_db() self.assertFalse(self.success_signal_was_called) self.assertTrue(self.exception_signal_was_called) self.assertFalse(self.charge.is_charged) self.assertTrue(self.charge.charge_attempt_failed) self.assertDictEqual(self.charge.stripe_response, stripe_error_json_body)
def test_confirm_payment_incorrect_payment_intent( mocked_intent_retrieve, stripe_plugin, payment_stripe_for_checkout ): gateway_response = { "id": "evt_1Ip9ANH1Vac4G4dbE9ch7zGS", } payment_intent_id = "payment-intent-id" payment = payment_stripe_for_checkout payment.transactions.create( is_success=True, action_required=False, kind=TransactionKind.ACTION_TO_CONFIRM, token=payment_intent_id, gateway_response=gateway_response, amount=payment.total, currency=payment.currency, ) mocked_intent_retrieve.side_effect = StripeError(message="stripe-error") payment_info = create_payment_information( payment_stripe_for_checkout, payment_token=payment_intent_id ) plugin = stripe_plugin() with warnings.catch_warnings(record=True): response = plugin.confirm_payment(payment_info, None) assert response.is_success is False assert response.action_required is False assert response.kind == TransactionKind.AUTH assert response.amount == payment.total assert response.currency == payment.currency assert response.transaction_id == "" assert response.error == "stripe-error"
def test_process_payment_with_customer_and_payment_method_raises_error( mocked_payment_intent, mocked_customer_create, stripe_plugin, payment_stripe_for_checkout, channel_USD, customer_user, ): customer = Mock() mocked_customer_create.return_value = customer payment_intent = Mock() stripe_error_object = StripeError(message="Card declined", json_body={"error": "body"}) stripe_error_object.error = StripeError() stripe_error_object.code = "card_declined" stripe_error_object.error.payment_intent = payment_intent mocked_payment_intent.side_effect = stripe_error_object client_secret = "client-secret" dummy_response = { "id": "evt_1Ip9ANH1Vac4G4dbE9ch7zGS", } payment_intent_id = "payment-intent-id" payment_intent.id = payment_intent_id payment_intent.client_secret = client_secret payment_intent.last_response.data = dummy_response plugin = stripe_plugin(auto_capture=True) payment_stripe_for_checkout.checkout.user = customer_user payment_stripe_for_checkout.checkout.email = customer_user.email payment_info = create_payment_information( payment_stripe_for_checkout, customer_id=None, store_source=True, additional_data={ "payment_method_id": "pm_ID", "off_session": True }, ) response = plugin.process_payment(payment_info, None) assert response.is_success is False assert response.action_required is True assert response.kind == TransactionKind.ACTION_TO_CONFIRM assert response.amount == payment_info.amount assert response.currency == payment_info.currency assert not response.transaction_id assert response.error == "Card declined" assert response.raw_response == {"error": "body"} assert response.action_required_data == {"client_secret": None, "id": None} api_key = plugin.config.connection_params["secret_api_key"] mocked_payment_intent.assert_called_once_with( api_key=api_key, amount=price_to_minor_unit(payment_info.amount, payment_info.currency), currency=payment_info.currency, capture_method=AUTOMATIC_CAPTURE_METHOD, customer=customer, payment_method="pm_ID", confirm=True, off_session=True, metadata={ "channel": channel_USD.slug, "payment_id": payment_info.graphql_payment_id, }, receipt_email=payment_stripe_for_checkout.checkout.email, stripe_version=STRIPE_API_VERSION, ) mocked_customer_create.assert_called_once_with( api_key="secret_key", email=customer_user.email, stripe_version=STRIPE_API_VERSION, )
def charge_create(**kwargs): raise StripeError(message='Foo')
def test_intermittent_stripe_error(): expected = jsonify({"message": "something"}), 503 error = StripeError("something") actual = intermittent_stripe_error(error) assert actual[0].json == expected[0].json assert actual[1] == expected[1]
def test_plan_exists(self, plan_create_mock): plan_create_mock.side_effect = StripeError("Plan already exists.") sync_plans() self.assertTrue("ERROR: Plan already exists.", sys.stdout.getvalue().strip())
def raise_stripe_error(*args, **kwargs): raise StripeError(message='test error')
def raise_stripe_error(*args, **kwargs): raise StripeError(message='test error', json_body={'error': { 'param': 'stripeToken', }})
def test_invoke_webhook_handlers_event_with_raise_stripe_error(self): event = self._create_event(FAKE_EVENT_TRANSFER_CREATED) self.call_handlers.side_effect = StripeError("Boom!") with self.assertRaises(StripeError): event.invoke_webhook_handlers()
def test_charges(self, charge_create_mocked): self.success_signal_was_called = False self.exception_signal_was_called = False def success_handler(sender, instance, **kwargs): self.success_signal_was_called = True def exception_handler(sender, instance, **kwargs): self.exception_signal_was_called = True stripe_charge_succeeded.connect(success_handler) stripe_charge_card_exception.connect(exception_handler) data = { "customer_id": "cus_AlSWz1ZQw7qG2z", "currency": "usd", "amount": 100, "description": "ABC" } charge_create_mocked.return_value = stripe.Charge(id="AA1") StripeCustomer.objects.create(user=self.user, stripe_customer_id="bum", stripe_js_response='"aa"') StripeCustomer.objects.create(user=self.user, stripe_customer_id=data["customer_id"], stripe_js_response='"foo"') customer = StripeCustomer.objects.create( user=self.user, stripe_customer_id=data["customer_id"], stripe_js_response='"foo"') self.assertTrue( customer, StripeCustomer.get_latest_active_customer_for_user(self.user)) charge = StripeCharge.objects.create(user=self.user, amount=data["amount"], customer=customer, description=data["description"]) manual_charge = StripeCharge.objects.create( user=self.user, amount=data["amount"], customer=customer, description=data["description"]) self.assertFalse(charge.is_charged) # test in case of an API error charge_create_mocked.side_effect = StripeError() with self.assertRaises(SystemExit): out = StringIO() sys.stdout = out call_command('charge_stripe') self.assertFalse(self.success_signal_was_called) charge.refresh_from_db() self.assertFalse(charge.is_charged) self.assertIn('Exception happened', out.getvalue()) charge_create_mocked.reset_mock() charge_create_mocked.side_effect = CardError(message="a", param="b", code="c") # test regular case call_command("charge_stripe") self.assertTrue(self.exception_signal_was_called) charge.refresh_from_db() self.assertFalse(charge.is_charged) self.assertTrue(charge.charge_attempt_failed) charge_create_mocked.reset_mock() charge_create_mocked.side_effect = None # test regular case # reset charge charge.charge_attempt_failed = False charge.save() call_command("charge_stripe") self.assertTrue(self.success_signal_was_called) charge.refresh_from_db() self.assertTrue(charge.is_charged) manual_charge.refresh_from_db() self.assertFalse(manual_charge.is_charged) self.assertEqual(charge.stripe_response["id"], "AA1") charge_create_mocked.assert_called_with( amount=charge.amount, currency=data["currency"], customer=data["customer_id"], description=data["description"]) # manual call manual_charge.charge() self.assertTrue(manual_charge.is_charged)
def test_server_stripe_error(): expected = jsonify({"message": "something", "code": "500"}), 500 error = StripeError("something", code="500") actual = server_stripe_error(error) assert actual[0].json == expected[0].json assert actual[1] == expected[1]