def test_issue_credit(self): """ Tests issuing credit with Paypal processor """ refund = self.create_refund(self.processor_name) order = refund.order basket = order.basket amount = refund.total_credit_excl_tax currency = refund.currency source = order.sources.first() transaction_id = 'PAY-REFUND-1' paypal_refund = paypalrestsdk.Refund({'id': transaction_id}) payment = paypalrestsdk.Payment({ 'transactions': [ Resource({'related_resources': [Resource({'sale': paypalrestsdk.Sale({'id': 'PAY-SALE-1'})})]}) ] }) with mock.patch.object(paypalrestsdk.Payment, 'find', return_value=payment): with mock.patch.object(paypalrestsdk.Sale, 'refund', return_value=paypal_refund): actual_transaction_id = self.processor.issue_credit(order.number, order.basket, source.reference, amount, currency) self.assertEqual(actual_transaction_id, transaction_id) # Verify PaymentProcessorResponse created self.assert_processor_response_recorded(self.processor.NAME, transaction_id, {'id': transaction_id}, basket)
def refund_payment(paypal_transaction): db.session.begin_nested() sale = paypalrestsdk.Sale()\ .find(paypal_transaction.paypal_transaction_id) try: refund = sale.refund() if refund.success(): transaction = paypal_transaction.transaction reverse_transaction = transaction_commons.reverse_transaction( transaction) parent_transaction = PaypalTransaction.query\ .filter_by(transaction_no=refund.parent_payment)\ .first() paypal_transaction = PaypalTransaction( paypal_transaction_id=refund.id, paypal_transaction_type_code='REFUND', state=refund.state, create_time=dateutil.parser.parse(refund.create_time), update_time=dateutil.parser.parse(refund.update_time), date_created=db.func.now()) if parent_transaction is not None: paypal_transaction.parent_transaction_id = parent_transaction.paypal_transaction_id db.session.add(paypal_transaction) db.session.commit() except IntegrityError as e: db.session.rollback() except PaypalTransactionException as e: db.session.rollback()
def test_issue_credit(self): """ Tests issuing credit with Paypal processor """ refund = self.create_refund(self.processor_name) order = refund.order basket = order.basket amount = refund.total_credit_excl_tax currency = refund.currency source = order.sources.first() transaction_id = 'PAY-REFUND-1' paypal_refund = paypalrestsdk.Refund({'id': transaction_id}) payment = paypalrestsdk.Payment({ 'transactions': [ Resource({'related_resources': [Resource({'sale': paypalrestsdk.Sale({'id': 'PAY-SALE-1'})})]}) ] }) with mock.patch.object(paypalrestsdk.Payment, 'find', return_value=payment): with mock.patch.object(paypalrestsdk.Sale, 'refund', return_value=paypal_refund): self.processor.issue_credit(source, amount, currency) # Verify PaymentProcessorResponse created self.assert_processor_response_recorded(self.processor.NAME, transaction_id, {'id': transaction_id}, basket) # Verify Source updated self.assertEqual(source.amount_refunded, amount) # Verify PaymentEvent created paid_type = PaymentEventType.objects.get(code='refunded') order = basket.order_set.first() payment_event = order.payment_events.first() self.assert_valid_payment_event_fields(payment_event, amount, paid_type, self.processor.NAME, transaction_id)
def test_issue_credit_error(self): """ Tests issue credit fails in case of erroneous response or exceptions """ refund = self.create_refund(self.processor_name) order = refund.order basket = order.basket amount = refund.total_credit_excl_tax currency = refund.currency source = order.sources.first() transaction_id = 'PAY-REFUND-FAIL-1' expected_response = {'debug_id': transaction_id} paypal_refund = paypalrestsdk.Refund({'error': expected_response}) payment = paypalrestsdk.Payment({ 'transactions': [ Resource({'related_resources': [Resource({'sale': paypalrestsdk.Sale({'id': 'PAY-SALE-1'})})]}) ] }) # Test general exception with mock.patch.object(paypalrestsdk.Payment, 'find', return_value=payment): with mock.patch.object(paypalrestsdk.Sale, 'refund', side_effect=ValueError): self.assertRaises(GatewayError, self.processor.issue_credit, order, source.reference, amount, currency) self.assertEqual(source.amount_refunded, 0) # Test error response with mock.patch.object(paypalrestsdk.Payment, 'find', return_value=payment): with mock.patch.object(paypalrestsdk.Sale, 'refund', return_value=paypal_refund): self.assertRaises(GatewayError, self.processor.issue_credit, order, source.reference, amount, currency) # Verify PaymentProcessorResponse created self.assert_processor_response_recorded(self.processor.NAME, transaction_id, expected_response, basket)