def test_refund_gateway_error(payment_txn_captured, monkeypatch): monkeypatch.setattr('remote_works.payment.gateways.dummy.dummy_success', lambda: False) payment = payment_txn_captured payment.charge_status = ChargeStatus.FULLY_CHARGED payment.captured_amount = Decimal('80.00') payment.save() with pytest.raises(PaymentError): gateway_refund(payment=payment, amount=Decimal('80.00')) payment.refresh_from_db() txn = payment.transactions.last() assert txn.kind == TransactionKind.REFUND assert not txn.is_success assert txn.payment == payment assert payment.charge_status == ChargeStatus.FULLY_CHARGED assert payment.captured_amount == Decimal('80.00')
def test_gateway_refund_failed(mock_get_payment_gateway, payment_txn_captured, gateway_params, settings, dummy_response): txn = payment_txn_captured.transactions.first() payment = payment_txn_captured captured_before = payment.captured_amount txn.is_success = False dummy_response['kind'] = TransactionKind.REFUND dummy_response['is_success'] = False mock_refund = Mock(return_value=dummy_response) mock_get_payment_gateway.return_value = (Mock(refund=mock_refund), gateway_params) with pytest.raises(PaymentError) as exc: gateway_refund(payment, Decimal('10.00')) exc.value.message == EXAMPLE_ERROR payment.refresh_from_db() assert payment.captured_amount == captured_before
def test_refund_failed(initial_captured_amount, refund_amount, initial_charge_status, payment_dummy): payment = payment_dummy payment.charge_status = initial_charge_status payment.captured_amount = Decimal(initial_captured_amount) payment.save() with pytest.raises(PaymentError): txn = gateway_refund(payment=payment, amount=Decimal(refund_amount)) assert txn is None
def test_gateway_refund_partial_refund(mock_get_payment_gateway, payment_txn_captured, gateway_params, settings, dummy_response): payment = payment_txn_captured amount = payment.total * Decimal('0.5') txn = payment_txn_captured.transactions.first() txn.amount = amount txn.currency = settings.DEFAULT_CURRENCY dummy_response['kind'] = TransactionKind.REFUND dummy_response['amount'] = amount mock_refund = Mock(return_value=dummy_response) mock_get_payment_gateway.return_value = (Mock(refund=mock_refund), gateway_params) gateway_refund(payment, amount) payment.refresh_from_db() assert payment.charge_status == ChargeStatus.PARTIALLY_REFUNDED assert payment.captured_amount == payment.total - amount
def test_gateway_refund(mock_get_payment_gateway, payment_txn_captured, gateway_params, dummy_response): txn = payment_txn_captured.transactions.first() payment = payment_txn_captured amount = payment.total dummy_response['kind'] = TransactionKind.REFUND mock_refund = Mock(return_value=dummy_response) mock_get_payment_gateway.return_value = (Mock(refund=mock_refund), gateway_params) payment_info = create_payment_information(payment, txn.token, amount) gateway_refund(payment, amount) mock_get_payment_gateway.assert_called_once_with(payment.gateway) mock_refund.assert_called_once_with(payment_information=payment_info, connection_params=gateway_params) payment.refresh_from_db() assert payment.charge_status == ChargeStatus.FULLY_REFUNDED assert not payment.captured_amount
def test_refund_success(initial_captured_amount, refund_amount, final_captured_amount, final_charge_status, active_after, payment_txn_captured): payment = payment_txn_captured payment.charge_status = ChargeStatus.FULLY_CHARGED payment.captured_amount = initial_captured_amount payment.save() txn = gateway_refund(payment=payment, amount=Decimal(refund_amount)) assert txn.kind == TransactionKind.REFUND assert txn.is_success assert txn.payment == payment assert payment.charge_status == final_charge_status assert payment.captured_amount == final_captured_amount assert payment.is_active == active_after
def test_gateway_refund_errors(payment_txn_captured): payment = payment_txn_captured with pytest.raises(PaymentError) as exc: gateway_refund(payment, Decimal('1000000')) assert exc.value.message == 'Cannot refund more than captured' with pytest.raises(PaymentError) as exc: gateway_refund(payment, Decimal('0')) assert exc.value.message == 'Amount should be a positive number.' payment.charge_status = ChargeStatus.NOT_CHARGED with pytest.raises(PaymentError) as exc: gateway_refund(payment, Decimal('1')) assert exc.value.message == 'This payment cannot be refunded.'