美文网首页
调用微信支付接口

调用微信支付接口

作者: 我去买个橘子o | 来源:发表于2019-07-15 19:42 被阅读0次
# -*- coding: utf-8 -*-

from datetime import datetime, date
from django.shortcuts import render
from django.http import HttpResponse
from django.utils import translation
from models import OrderRecord
from circle_models import CircleFace, CircleAccount
from task import get_new_sn, timeline_subscribe, timeline_unsubscribe
import json
import threading
import uuid
from django.views.decorators.csrf import csrf_exempt
from wechat_pay import *

@csrf_exempt
def wxpay_notify(request):
    wxpay_server = Wxpay_server_pub()
    wxpay_server.saveData(request.body)
    post_data = wxpay_server.getData()
    if 'result_code' in post_data and 'return_code' in post_data and 'transaction_id' in post_data:
        if post_data['result_code'] == Wxpay_server_pub.SUCCESS and post_data['return_code'] == Wxpay_server_pub.SUCCESS:
            transaction_id = post_data['transaction_id']
            out_trade_no = post_data['out_trade_no']
            total_fee = post_data['total_fee']
            time_end = post_data['time_end']

            order_query_pub = OrderQuery_pub()
            order_query_pub.setParameter('out_trade_no', out_trade_no)
            order_query_pub.setParameter('transaction_id', transaction_id)
            order_result = order_query_pub.getResult()
            if order_result['return_code'] == Wxpay_server_pub.SUCCESS and order_result['result_code'] == Wxpay_server_pub.SUCCESS and order_result['trade_state'] == Wxpay_server_pub.SUCCESS:
                result_list = OrderRecord.objects(order_id=out_trade_no)
                if len(result_list) > 0:
                    order_item = result_list[0]                              
                    order_item.transaction_id = transaction_id
                    order_item.total_fee = total_fee
                    order_item.time_end = time_end
                    order_item.save()

                    result_list = CircleAccount.objects(sn=order_item.product_id)
                    if len(result_list) > 0:
                        circle_account_item = result_list[0]
                        circle_account_item.valid = True
                        circle_account_item.pay_time = datetime.now()
                        circle_account_item.save()

    wxpay_server_pub = Wxpay_server_pub()
    wxpay_server_pub.setReturnParameter('return_code', Wxpay_server_pub.SUCCESS)
    wxpay_server_pub.setReturnParameter('return_msg', 'OK')
    return_xml = wxpay_server_pub.returnXml()
    return HttpResponse(return_xml, content_type="application/xml")

@csrf_exempt
def pay_callback(request):
    wxpay_server = Wxpay_server_pub()
    wxpay_server.saveData(request.body)
    post_data = wxpay_server.getData()

    order_record = OrderRecord()
    order_record.order_id = str(uuid.uuid1()).replace('-', '')
    order_record.appid = post_data['appid']
    order_record.openid = post_data['openid']
    order_record.mch_id = post_data['mch_id']
    order_record.is_subscribe = post_data['is_subscribe']
    order_record.nonce_str = post_data['nonce_str']
    order_record.product_id = post_data['product_id']
    order_record.sign = post_data['sign']
    order_record.created_time = datetime.now()
    order_record.save()

    unified_order = UnifiedOrder_pub()
    unified_order.setParameter('body', u'Pebble表盘-“银河石”终身使用权')
    unified_order.setParameter('out_trade_no', order_record.order_id)
    unified_order.setParameter('total_fee', '1000')
    unified_order.setParameter('notify_url', WxPayConf_pub.NOTIFY_URL)
    unified_order.setParameter('trade_type', 'NATIVE')
    unified_order.setParameter('product_id', order_record.product_id)
    unified_order.setParameter('openid', order_record.openid)

    prepay_id = unified_order.getPrepayId()

    wxpay_server_pub = Wxpay_server_pub()
    wxpay_server_pub.setReturnParameter('return_code', Wxpay_server_pub.SUCCESS)
    wxpay_server_pub.setReturnParameter('appid', WxPayConf_pub.APPID)
    wxpay_server_pub.setReturnParameter('mch_id', WxPayConf_pub.MCHID)
    wxpay_server_pub.setReturnParameter('nonce_str', wxpay_server_pub.createNoncestr())
    wxpay_server_pub.setReturnParameter('prepay_id', prepay_id)
    wxpay_server_pub.setReturnParameter('result_code', Wxpay_server_pub.SUCCESS)
    wxpay_server_pub.setReturnParameter('sign', wxpay_server_pub.getSign(wxpay_server_pub.returnParameters))
    return_xml = wxpay_server_pub.returnXml()

    return HttpResponse(return_xml, content_type="application/xml")

相关文章

  • 【微信】支付

    公众号支付 调用微信js支付接口的页面,必须接口域名授权。在公众平台中设置,最多可以授权3个。 使用微信公众号支付...

  • JSAPI 简介

    JSAPI 简介 JSAPI支付是指商户通过调用微信支付提供的JSAPI接口,在支付场景中调起微信支付模块完成收款...

  • 调用微信支付接口

  • 小程序-微信支付功能

    概述 先调用“统一下单”接口,在微信支付服务后台生成预支付交易单,返回正确的预支付交易后,再调起微信支付接口,发起...

  • h5微信支付

    html引入微信js接口 请求支付的接口 先进行无论是分享还是支付的微信配置 jsApiList用来存放需要的调用...

  • 微信公众号怎么发红包?

    公众号可以通过调用微信支付中的现金红包接口,进行派发红包,只需开通微信支付,可接入现金红包,通过现金红包接口,公众...

  • 【融职培训】Web前端学习 第11章 微信开发3 微信签名规则

    一,概述 为了让微信接口的调用更加安全,微信服务器要对支付接口和JS-SDK接口进行加密和校验。因此开发调取微信支...

  • 微信公众号支付

    微信公众号支付使用的是JSAPI支付,后端调用微信支付demo中统一下单的接口获取支付参数返回给前端,在公众号中直...

  • python3实现微信支付和提现

    微信支付--V3接口 小程序支付 前提:前端获取微信用户的临时code值给到后端,后端根据code值调用微信API...

  • iOS微信支付——APP调用微信支付接口

    一、准备工作 1.必须通过微信审核的微信APPID 不然就没法实现微信支付功能 2.查看iOS微信支付官方文档去集...

网友评论

      本文标题:调用微信支付接口

      本文链接:https://www.haomeiwen.com/subject/xauokctx.html