def test_refund_gateway_error(payment_txn_captured, monkeypatch): monkeypatch.setattr("saleor.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_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_refund_for_manual_payment(payment_txn_captured): payment_txn_captured.gateway = CustomPaymentChoices.MANUAL transaction = gateway.refund( payment=payment_txn_captured, amount=PARTIAL_REFUND_AMOUNT ) payment_txn_captured.refresh_from_db() assert payment_txn_captured.charge_status == ChargeStatus.PARTIALLY_REFUNDED assert transaction.amount == PARTIAL_REFUND_AMOUNT assert transaction.kind == TransactionKind.REFUND assert transaction.currency == "USD"
def test_full_refund_payment(mock_payment_interface, payment_txn_captured): capture_transaction = payment_txn_captured.transactions.get() PAYMENT_DATA = create_payment_information( payment=payment_txn_captured, amount=FULL_REFUND_AMOUNT, payment_token=capture_transaction.token, ) mock_payment_interface.refund_payment.return_value = FULL_REFUND_RESPONSE transaction = gateway.refund(payment=payment_txn_captured) mock_payment_interface.refund_payment.assert_called_once_with( USED_GATEWAY, PAYMENT_DATA) payment_txn_captured.refresh_from_db() assert payment_txn_captured.charge_status == ChargeStatus.FULLY_REFUNDED assert transaction.amount == FULL_REFUND_AMOUNT assert transaction.kind == TransactionKind.REFUND assert transaction.currency == "usd" assert transaction.gateway_response == RAW_RESPONSE
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."