def test_not_ok_response_raises_payment_error(self, get): get.side_effect = [ mock.MagicMock(vendor_tx_code='v1', tx_id='123', tx_auth_num='', security_key='xx', currency='GBP'), mock.MagicMock(vendor_tx_code='v2', tx_id='124', tx_auth_num='999', security_key='xy')] with mock.patch('oscar_sagepay.gateway.refund') as refund: refund.return_value = mock.MagicMock(is_ok=False) with pytest.raises(payment_exceptions.PaymentError): facade.refund(tx_id='123')
def test_calls_gateway_with_correct_data(self, get): get.return_value = mock.MagicMock( vendor_tx_code='v2', tx_id='124', tx_auth_num='999', security_key='xy', currency='GBP') with mock.patch('oscar_sagepay.gateway.refund') as refund: facade.refund(tx_id='123') kwargs = refund.call_args[1] assert kwargs['previous_txn'].vendor_tx_code == 'v2' assert kwargs['previous_txn'].tx_id == '124' assert kwargs['previous_txn'].tx_auth_num == '999' assert kwargs['previous_txn'].security_key == 'xy' assert kwargs['currency'] == 'GBP'
def test_multiple_transactions(): # Authenticate transaction authenticate_tx_id = facade.authenticate( AMT, CURRENCY, BANKCARD, SHIPPING_ADDRESS, BILLING_ADDRESS) # Authorise (in two parts) facade.authorise( tx_id=authenticate_tx_id, amount=D('8.00')) auth_tx_id = facade.authorise( tx_id=authenticate_tx_id, amount=D('2.00')) # Refund last authorise facade.refund(auth_tx_id)
def post(self, request, number): order = shortcuts.get_object_or_404( order_models.Order, number=number) # Grab first AUTHORISE txn to refund txns = models.RequestResponse.objects.filter( reference=order.number, tx_type=gateway.TXTYPE_AUTHORISE, status='OK') txn = txns[0] url = reverse('dashboard:order-detail', kwargs={ 'number': order.number}) response = http.HttpResponseRedirect(url) try: new_tx_id = facade.refund(txn.tx_id, order_number=order.number) except exceptions.PaymentError as e: messages.error( request, ( "We were unable to refund your order: %s") % e) return response # Update payment source source = order.sources.all()[0] source.refund(source.amount_debited, reference=new_tx_id) messages.success( request, "Transaction authorised: TX ID %s" % new_tx_id) return response
def post(self, request, number): order = shortcuts.get_object_or_404(order_models.Order, number=number) # Grab first AUTHORISE txn to refund txns = models.RequestResponse.objects.filter( reference=order.number, tx_type=gateway.TXTYPE_AUTHORISE, status='OK') txn = txns[0] url = reverse('dashboard:order-detail', kwargs={'number': order.number}) response = http.HttpResponseRedirect(url) try: new_tx_id = facade.refund(txn.tx_id, order_number=order.number) except exceptions.PaymentError as e: messages.error(request, ("We were unable to refund your order: %s") % e) return response # Update payment source source = order.sources.all()[0] source.refund(source.amount_debited, reference=new_tx_id) messages.success(request, "Transaction authorised: TX ID %s" % new_tx_id) return response
def test_raises_payment_error_for_missing_related_txn(self, get): # Second call will raise exception get.side_effect = models.RequestResponse.DoesNotExist with pytest.raises(payment_exceptions.PaymentError): facade.refund(tx_id='123')
def test_raises_payment_error_for_unknown_tx_id(self, get): get.side_effect = models.RequestResponse.DoesNotExist with pytest.raises(payment_exceptions.PaymentError): facade.refund(tx_id='123')