def test_multiple_deposit_gidx(self): # Test Transaction.deposit_gidx mulitple times with the same merchant_transaction_id cash_trans = CashTransaction(self.user) mti = 'fake_transaction_id' cash_trans.deposit_gidx(10, mti) # Should be 1 transaction gidx_transactions = GidxTransaction.objects.filter(merchant_transaction_id=mti) self.assertEqual(gidx_transactions.count(), 1) # Now try again with the same merchant_transaction_id and it should not allow another # transcation to be made cash_trans.deposit_gidx(10, mti) gidx_transactions = GidxTransaction.objects.filter(merchant_transaction_id=mti) self.assertEqual(gidx_transactions.count(), 1) self.user.delete()
def make_web_cashier_payment_detail_request(self, user, transaction_id, session_id): """ A task that will make a request to GIDX for transaction details, and will handle the response appropriately (adding funds, etc). :param self: :param user: :param transaction_id: :param session_id: :return: """ payment_detail_request = WebCashierPaymentDetailRequest( user=user, merchant_transaction_id=transaction_id, merchant_session_id=session_id, ) payment_detail_response = payment_detail_request.send() transaction_id = payment_detail_response.json['MerchantTransactionID'] logger.info('Payment detail received: %s' % payment_detail_response.json) # The response contains payments... if payment_detail_request.has_payments(): payments = payment_detail_request.get_cash_payments() for payment in payments: # was it a credit (deposit) or debit (withdraw)? payment_type = payment['PaymentAmountType'] # Create a gidx cash transaction which will save the transaction to the db and # update the user's balance. trans = CashTransaction(user) # # It is a DEPOSIT if payment_type.lower() == 'credit': if payment['PaymentStatusMessage'] == 'Failed': logger.warning( 'Deposit payment was not a success, not adding funds. %s' % payment) elif payment['PaymentStatusMessage'] == 'Complete': # Deposit the amount into their account trans.deposit_gidx(payment['PaymentAmount'], transaction_id) else: raise Exception( 'Unknown PaymentStatusMessage from GIDX payment detail response: %s' % (payment)) # # It is a PAYOUT elif payment_type.lower() == 'debit': existing_transaction = GidxTransaction.objects.filter( merchant_transaction_id=transaction_id) # There is already more than one transaction with this ID, we should be # notified of this. if existing_transaction.count() > 1: # Send some useful information to Sentry. client.context.merge({ 'extra': { 'response_json': payment_detail_response.json, 'merchant_transaction_id': transaction_id, } }) client.captureMessage( "More than one transaction for a merchant_transaction_id was found." ) client.context.clear() # The withdraw failed - We need to issue a refund if a withdraw was already # made (it probalby was). if payment['PaymentStatusMessage'].lower() == 'failed': # We had a previous withdraw transaction, put the money back into the user's # balance. if existing_transaction.count() > 0: logger.info( 'Withdraw was not a success, refunding previous withdraw. %s' % payment) trans.deposit_gidx(payment['PaymentAmount'], "%s--REFUND" % transaction_id) else: logger.warning( 'No transaction exists for this failed withdraw attempt: %s' % (payment_detail_response.json)) # Withdraw was a success! We need to withdraw if we haven't already. elif payment['PaymentStatusMessage'].lower() == 'complete': # They've never had a transaction to reduce their balance! we need to do it now. if existing_transaction.count() == 0: logger.info( 'No withdraw transaction exists, creating one now. %s' % (payment_detail_response.json)) trans.withdraw_gidx(payment['PaymentAmount'], transaction_id) else: logger.info( 'A withdraw transaction exists, NOT creating one now. %s' % (payment_detail_response.json)) else: raise Exception( 'Unknown PaymentStatusMessage from GIDX payment detail response: %s' % (payment)) else: raise Exception( 'Unknown PaymentAmountType from GIDX payment detail response: %s' % (payment)) else: logger.warning('Payment Detail contained no payment info! %s' % payment_detail_response.json) return payment_detail_request