def alipay_return(request,type,id,template="accounts/alipay_return.html"): if request.method == "GET": ext_ctx={"pay_type":type,"pay_status":"success"} alipay = Alipay() valid, infos = alipay.validate(request) if valid: trade_status = infos['trade_status'] total_fee = infos['total_fee'] ext_ctx["money"]=total_fee if trade_status == 'TRADE_SUCCESS' or trade_status == "TRADE_FINISHED": record,to_process=process_record_or_no(infos) if to_process: profile=request.user.profile profile.money += record.money profile.save() # msg = Message("success", "您成功充值了%s元!" % total_fee) if type=="pay" and id and record: #logging.info("to checkin:%s,uid:%d,profile money:%g" % (id,record.user.id,profile.money)) composition.views.checkin(request,id,user=record.user) else: raise Exception else: ext_ctx["pay_status"]="fail" #msg = Message("error", "支付未完成!") else: ext_ctx["pay_status"]="fail" return render_to_response(template,ext_ctx,RequestContext(request))
def alipay_notify(request,type,id): if request.method == "POST": try: logging.info("access type:%s,id:%s now:%s " % (type, id, str(datetime.datetime.now()))) alipay = Alipay() valid, infos = alipay.validate(request) if valid: trade_status = infos['trade_status'] logging.info("trade:%s,id:%s" % (trade_status, id)) if trade_status == 'TRADE_SUCCESS' or trade_status == "TRADE_FINISHED": record, to_process = process_record_or_no(infos) logging.info("to process:%s" % to_process) if to_process: profile = record.user.profile profile.money += record.money profile.save() logging.info("user money:%g,record money:%g" % (profile.money, record.money)) # msg = Message("success", "您成功充值了%s元!" % total_fee) if type == "pay" and id and record: composition.views.checkin(request,id,user=record.user) else : logging.info("false to checkin ") else: pass #msg = Message("error", "支付未完成!") logging.info("success") return HttpResponse("success") except: printinfo("error") logging.info("fail") return HttpResponse("fail")
def charge(request,**param): request_url='/' param=param or {"type":"charge","id":str(request.user.id)} if request.method=="POST": seller_email = settings.ALIPAY_SELLER_EMAIL; subject = settings.SITE_NAME+_(param["type"]) body = request.user.username+"@"+settings.SITE_NAME+_(param["type"]) price = request.POST.get('price', '') record=Record() record.user=request.user record.detail=u'支付宝支付' out_trade_no = "-".join([datetime.datetime.now().strftime("%Y%m%d%H%M%s"),price]) record.out_trade_no=out_trade_no record.save() payment_type ='2' # Maybe security problem here notify_url =return_url= settings.SITE_URL notify_url+=reverse("alipay_notify",None,None,param) return_url+=reverse("alipay_return",None,None,param) try: total_fee = Decimal(price) except InvalidOperation: total_fee = None if seller_email and subject and out_trade_no and total_fee and notify_url and payment_type: #user = request.user alipay = Alipay() request_url = alipay.create_direct_pay_by_user_url(seller_email, subject, body, out_trade_no, total_fee, notify_url,return_url, payment_type) return HttpResponseRedirect(request_url) template = loader.get_template("accounts/charge.html") context=RequestContext(request) return HttpResponse(template.render(context))