def testExecute(request): p = PaymentManager() if 'campaign' in request.GET.keys(): campaign_id = request.GET['campaign'] campaign = Campaign.objects.get(id=int(campaign_id)) else: campaign = None output = '' if campaign: result = p.execute_campaign(campaign) for t in result: output += str(t) logger.info(str(t)) else: transactions = Transaction.objects.filter(status=TRANSACTION_STATUS_ACTIVE) for t in transactions: # Note, set this to 1-5 different receivers with absolute amounts for each receiver_list = [{'email':TEST_RECEIVERS[0], 'amount':float(t.amount) * 0.80}, {'email':TEST_RECEIVERS[1], 'amount':float(t.amount) * 0.20}] p.execute_transaction(t, receiver_list) output += str(t) logger.info(str(t)) return HttpResponse(output)
def pledge_to_work_with_cc(self, username, password, work_id, card, preapproval_amount='10', premium_id='150'): # how much of test.campaigntest.test_relaunch can be done here? self.assertTrue(self.client.login(username=username, password=password)) # Pro Web 2.0 Mashups self.work = Work.objects.get(id=work_id) r = self.client.get("/work/%s/" % (self.work.id)) r = self.client.get("/work/%s/" % self.work.id) self.assertEqual(r.status_code, 200) # go to pledge page r = self.client.get("/pledge/%s" % self.work.id, data={}, follow=True) self.assertEqual(r.status_code, 200) # submit to pledge page r = self.client.post("/pledge/%s/" % self.work.id, data={'preapproval_amount': preapproval_amount, 'premium_id':premium_id}, follow=True) self.assertEqual(r.status_code, 200) # Should now be on the fund page pledge_fund_path = r.request.get('PATH_INFO') self.assertTrue(pledge_fund_path.startswith('/payment/fund')) # pull out the transaction info t_id = int(pledge_fund_path.replace('/payment/fund/','')) # r.content holds the page content # create a stripe token to submit to form sc = StripeClient() stripe_token = sc.create_token(card=card) # track start time and end time of these stipe interactions so that we can limit the window of Events to look for # time0 = time.time() <--- this method was brittle because of clock skew and latency time0 = stripe_token['created'] r = self.client.post(pledge_fund_path, data={'stripe_token':stripe_token.id}, follow=True) # where are we now? self.assertEqual(r.request.get('PATH_INFO'), '/fund/complete/') self.assertEqual(r.status_code, 200) # dig up the transaction and charge it pm = PaymentManager() transaction = Transaction.objects.get(id=t_id) # catch any exception and pass it along charge_exception = None try: self.assertTrue(pm.execute_transaction(transaction, ())) except Exception as e: charge_exception = e # retrieve events from this period events = list(sc._all_objs('Event', created={'gte': time0})) return (events, charge_exception)
def recharge_failed_transactions(self): """When a new Account is saved, check whether this is the new active account for a user. If so, recharge any outstanding failed transactions """ transactions_to_recharge = self.user.transaction_set.filter( (Q(status=TRANSACTION_STATUS_FAILED) | Q(status=TRANSACTION_STATUS_ERROR)) & Q(campaign__status='SUCCESSFUL')).all() if transactions_to_recharge: from regluit.payment.manager import PaymentManager pm = PaymentManager() for transaction in transactions_to_recharge: # check whether we are still within the window to recharge if (now() - transaction.deadline_or_now) < datetime.timedelta( settings.RECHARGE_WINDOW): logger.info( "Recharging transaction {0} w/ status {1}".format( transaction.id, transaction.status)) pm.execute_transaction(transaction, [])