Ejemplo n.º 1
0
def asks_for_money(request):
    if request.method == 'GET':
        alipay_form = PaymentForm(auto_id=False)
        context = {
            "alipay_form": alipay_form,
        }
        return render_to_response("pay/payment.html",
                                  context,
                                  context_instance=RequestContext(request))
    # POST method
    site = Site.objects.get_current()
    pay_url = 'http://%s/' % (site.domain)
    form = PaymentForm(request.POST, auto_id=False)
    if not form.is_valid():
        context = {
            "alipay_form": form,
        }
        return render_to_response("pay/payment.html",
                                  context,
                                  context_instance=RequestContext(request))
    # alipay form
    item_name = ("Django Alipay example")
    alipay_dict = {
        # base
        "_input_charset":
        'utf-8',
        'notify_url':
        '%salipay/ptn/' % pay_url,
        'return_url':
        '%salipay/return/' % pay_url,
        # business
        # 'seller_email': '',
        'out_trade_no':
        uuid.uuid4().hex,
        'subject':
        item_name,
        'price':
        form.cleaned_data['price'],
        'quantity':
        form.cleaned_data['quantity'],
        # direct pay
        # 'extra_common_param': '%s|%d'% (form.cleaned_data['price'], form.cleaned_data['quantity']),
        # partner trade
        'body':
        '%s|%d' % (form.cleaned_data['price'], form.cleaned_data['quantity']),
    }
    alipay_form = AliPayPartnerTradeForm(auto_id=False, initial=alipay_dict)
    context = {
        'payment_title': ('AliPay Payment'),
        'item_name': item_name,
        'form': form,
        'action': alipay_form.get_action(),
        'paid_form': alipay_form,
    }
    data = get_form_data(alipay_form)
    alipay_form['sign'].field.initial = make_sign(data)
    return render_to_response("pay/preview.html",
                              context,
                              context_instance=RequestContext(request))
Ejemplo n.º 2
0
def alipay_send_goods(trade_obj):
    """Send Goods to buyer"""
    sign_type = 'MD5'
    site = Site.objects.get_current()
    alipay_dict = {
            'sign_type': sign_type,
            'trade_no': trade_obj.trade_no,
            'logistics_name': '%s Express'% site.name,
            'invoice_no': trade_obj.out_trade_no,
            'transport_type': 'EXPRESS',
            }
    form = AliPaySendGoodsForm(auto_id=False, initial=alipay_dict)
    data = get_form_data(form)
    data['sign'] = make_sign(data)
    return urllib2.urlopen(form.get_action(),urlencode(data)).read()
Ejemplo n.º 3
0
def alipay_send_goods(trade_obj):
    """Send Goods to buyer"""
    sign_type = 'MD5'
    site = Site.objects.get_current()
    alipay_dict = {
        'sign_type': sign_type,
        'trade_no': trade_obj.trade_no,
        'logistics_name': '%s Express' % site.name,
        'invoice_no': trade_obj.out_trade_no,
        'transport_type': 'EXPRESS',
    }
    form = AliPaySendGoodsForm(auto_id=False, initial=alipay_dict)
    data = get_form_data(form)
    data['sign'] = make_sign(data)
    return urllib2.urlopen(form.get_action(), urlencode(data)).read()
Ejemplo n.º 4
0
def asks_for_money(request):
    if request.method == 'GET':
        alipay_form = PaymentForm(auto_id=False)
        context = {
                "alipay_form": alipay_form,
                }
        return render_to_response("pay/payment.html", context, context_instance=RequestContext(request))
    # POST method
    site = Site.objects.get_current()
    pay_url = 'http://%s/'%(site.domain)
    form = PaymentForm(request.POST, auto_id=False)
    if not form.is_valid():
        context = {
                "alipay_form": form,
                }
        return render_to_response("pay/payment.html", context, context_instance=RequestContext(request))
    # alipay form
    item_name = ("Django Alipay example")
    alipay_dict = {
            # base
            "_input_charset": 'utf-8',
            'notify_url': '%salipay/ptn/'% pay_url,
            'return_url': '%salipay/return/'% pay_url,
            # business
            #'seller_email': '',
            'out_trade_no': uuid.uuid4().hex,
            'subject': item_name,
            'price': form.cleaned_data['price'],
            'quantity': form.cleaned_data['quantity'],
            # direct pay
            #'extra_common_param': '%s|%d'% (form.cleaned_data['price'], form.cleaned_data['quantity']),
            # partner trade
            'body': '%s|%d'% (form.cleaned_data['price'], form.cleaned_data['quantity']),
            }
    alipay_form = AliPayPartnerTradeForm(auto_id=False, initial=alipay_dict)
    context = {
            'payment_title': ('AliPay Payment'),
            'item_name': item_name,
            'form': form,
            'action': alipay_form.get_action(),
            'paid_form': alipay_form,
            }
    data = get_form_data(alipay_form)
    alipay_form['sign'].field.initial = make_sign(data)
    return render_to_response("pay/preview.html", context, context_instance=RequestContext(request))
Ejemplo n.º 5
0
 def clean_sign(self):
     data = get_form_data(self)
     sign = make_sign(data)
     if sign != data.get('sign'):
         self._errors['sign'] = self.error_class(['sign error!'])
     return sign
Ejemplo n.º 6
0
 def clean_sign(self):
     data = get_form_data(self)
     sign = make_sign(data)
     if sign != data.get('sign'):
         self._errors['sign'] = self.error_class(['sign error!'])
     return sign
Ejemplo n.º 7
0
def pay_by_alipay(request):
    print 111
    data = {'status': 400}
    request_params = getattr(request, request.method)
    # print request_params
    # form = PaymentForm(request_params)
    # print form

    signed_request = request_params.get('signed_request', '')
    signed_request = signed_request.split('.')
    if len(signed_request) == 2:
        sign, pay_data = signed_request[0], signed_request[1]
    else:
        return json_response(data)
    pay_data = decrypt(pay_data, settings.PRIVATE_KEY)
    print pay_data

    pay_data = pay_data.split('&')
    pay_data.sort()
    pay_data = '&'.join(pay_data)

    sign_cal = hashlib.md5('%s%s' % (pay_data.decode('utf-8').encode('utf-8'), settings.PRIVATE_KEY)).hexdigest()
    print sign, sign_cal
    #if sign_cal != sign:
    #    return json_response(data)
    params = Param('%s' % pay_data)
    print params

    total_fee = params.get_param('total_fee')[0]
    domain_buy = params.get_param('domain_buy')[0]
    service = params.get_param('service')[0]
    user_id = params.get_param('user_id')[0]
    out_trade_no = params.get_param('out_trade_no')[0]
    subject = params.get_param('subject')[0]
    body = params.get_param('body')[0]
    print body

    # total_fee = form.cleaned_data.get('total_fee')
    # domain_buy = form.cleaned_data.get('domain_buy')
    # service = form.cleaned_data.get('service')
    # user_id = form.cleaned_data.get('user_id')
    # out_trade_no = form.cleaned_data.get('out_trade_no')
    # subject = form.cleaned_data.get('subject')
    # body = form.cleaned_data.get('body')

    # alipay form
    alipay_dict = {
            "_input_charset": 'utf-8',
            'notify_url': '%s/alipay/nofify-async/?out_trade_no=123456' % settings.DOMAIN,
            'return_url': '%s/alipay/return/?out_trade_no=%s' % (settings.DOMAIN,
            	                                                 out_trade_no),
            'sign_type': 'MD5',
            # 'sign': '',
            # 'error_notify_url': '',
            'out_trade_no': out_trade_no,
            'subject': subject,
            # 'buyer_id': '',
            # 'seller_account_name': '',
            # 'buyer_account_name': '',
            'seller_email': settings.ALIPAY_SELLER_EMAIL,
            # 'price': '',
            'total_fee': 0.01,
            # 'quantity': '',
            # 超时时间m-分钟,h-小时,d-天,1c-当天(无论交易何时创建,都在0点关闭)。
            # 该参数数值不接受小数点, 如1.5h,可转换为 90m
            # 'it_b_pay': '1h',
            'body': body,
            # 'show_url': '',
            # 'discount': '',
            # 'need_ctu_check': 'Y',
            # 'royalty_type': '',
            # 'royalty_parameters': '',
            'anti_phishing_key': 'AABBCDDEG',
            # 'exter_invoke_ip': '',
            'extra_common_param': 'This is a test request',
            # 'extend_param': '',
            'default_login': '******',
            # 'product_type': '',
            # 'token': '',
            }
    alipay_form = AliPayDirectPayForm(auto_id=False, initial=alipay_dict)
    form_data = get_form_data(alipay_form)
    alipay_form['sign'].field.initial = make_sign(form_data)
    # data = get_form_data(alipay_form)
    request_url = alipay_form.get_action()
    for key, value in form_data.items():
        if isinstance(value, unicode):
            v = value.encode(alipay_form.initial['_input_charset'])
        else:
            v = value.decode('utf-8').encode(alipay_form.initial['_input_charset']) if hasattr(value, 'decode') else value
        try:
            v = urllib2.quote(v)
        except AttributeError:
            pass
        request_url += "&%s=%s" % (key, v)
    print request_url
    data['pay_url'] = request_url
    data['status'] = 200
    print data
    return json_response(data)