cocos2d-js android django微信支付
APP微信支付1. 使用django生成预支付订单wxpay.pyviews.pyurl.pycocos2d-js 拉起生成预订单封装http请求Weixinpay.jsAndroid 拉起微信支付导入微信sdk以及阿里fastjsonAppActivity.javaWXPayEntryActivity.javaAndroidManifest.xml支付成功django回调url.pyAPP支付流
·
APP微信支付
1. 使用django生成预支付订单
wxpay.py
# wxpay.py
import hashlib
import time
from urllib.parse import quote
import requests
import xmltodict
from pay import settings
class WeiXinPay(object):
def __init__(self):
self.APPID = settings.WX_APPID
self.MCHID = settings.WXPAY_MCHID
self.NOTIFY_URL = 'https://****/weixin_notify/' # 改为自己的回调url
self.TRADE_TYPE = 'APP'
self.APIKEY = settings.WXPAY_APIKEY
self.url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'
self.error = None
self.params = None
def get_parameter(self, order_id, body, total_fee, spbill_create_ip):
self.params = {
'appid': self.APPID,
'mch_id': self.MCHID,
'nonce_str': self.getNonceStr(),
'body': body,
'out_trade_no': str(order_id),
'total_fee': str(int(total_fee)),
'spbill_create_ip': spbill_create_ip,
'trade_type': self.TRADE_TYPE,
'notify_url': self.NOTIFY_URL,
}
return self.params
def getNonceStr(self, length=32):
"""生成随机字符串"""
import random
chars = "abcdefghijklmnopqrstuvwxyz0123456789"
strs = []
for x in range(length):
strs.append(chars[random.randrange(0, len(chars))])
return "".join(strs)
def key_value_url(self, value, urlencode):
"""
将键值对转为 key1=value1&key2=value2
对参数按照key=value的格式,并按照参数名ASCII字典序排序
"""
slist = sorted(value)
buff = []
for k in slist:
v = quote(value[k]) if urlencode else value[k]
buff.append("{0}={1}".format(k, v))
return "&".join(buff)
def get_sign(self, params):
"""
生成sign
拼接API密钥
"""
stringA = self.key_value_url(params, False)
stringSignTemp = stringA + '&key=' + self.APIKEY
sign = (hashlib.md5(stringSignTemp.encode("utf-8")).hexdigest()).upper()
params['sign'] = sign
return params
def get_req_xml(self):
"""
拼接XML
"""
self.get_sign(self.params)
xml = "<xml>"
for k, v in self.params.items():
xml += '<' + k + '>' + v + '</' + k + '>'
xml += "</xml>"
return xml.encode("utf-8")
def get_prepay_id(self):
"""
请求获取prepay_id
"""
xml = self.get_req_xml()
respone = requests.post(self.url, xml, headers={'Content-Type': 'application/xml'})
msg = respone.text.encode('ISO-8859-1').decode('utf-8')
xmlresp = xmltodict.parse(msg)
if xmlresp['xml']['return_code'] == 'SUCCESS':
if xmlresp['xml']['result_code'] == 'SUCCESS':
prepay_id = xmlresp['xml']['prepay_id']
self.params['prepay_id'] = prepay_id
self.params['package'] = "Sign=WXPay"
self.params['timestamp'] = str(int(time.time()))
return self.params
else:
return 'failure'
else:
return 'failure'
def re_finall(self):
"""得到prepay_id后再次签名,返回给终端参数
"""
self.get_prepay_id()
if self.error:
return
sign_again_params = {
'appid': self.params['appid'],
'noncestr': self.params['nonce_str'],
'package': self.params['package'],
'partnerid': self.params['mch_id'],
'timestamp': self.params['timestamp'],
'prepayid': self.params['prepay_id']
}
self.get_sign(sign_again_params)
sign_again_params['sign'] = sign_again_params['sign']
return sign_again_params
def get_notifypay(self, data):
dictdata = dict(data)
_dictdata = dict(dictdata['xml'])
success = self.get_sign(_dictdata)
if success:
success.pop("sign", None)
success.pop("sign_type", None)
return success
else:
return None
@staticmethod
def xml_to_dict(params):
"""
拼接XML
"""
if not isinstance(params, dict):
return None
xml = "<xml>"
for k, v in params.items():
# v = v.encode('utf8')
# k = k.encode('utf8')
xml += '<' + k + '>' + v + '</' + k + '>'
xml += "</xml>"
return xml
views.py
import random
import arrow
from rest_framework.response import Response
from rest_framework.views import APIView
from pay.wxpay import WeiXinPay
class Payment(APIView):
def order_number(self):
num = random.randint(100000,999999)
order_id = str(arrow.now().timestamp) + str(num)
return order_id
def post(self,request):
total_fee = int(request.data.get('total_fee',201))
body = '支付TEST'
spbill_create_ip = '127.0.0.1'
order_id = self.order_number()
pay = WeiXinPay()
parameter_dict = pay.get_parameter(order_id, body, total_fee, spbill_create_ip)
response = pay.re_finall()
return Response(response)
url.py
path('payment/', Payment.as_view()),
cocos2d-js 拉起生成预订单
封装http请求
//封装http请求
function httpPostTest(url, data, callback) {
var url = 'http://39.108.86.11/' + url;
var xhr = new XMLHttpRequest();
xhr.responseType = "text";
xhr.open('POST', url);
//在java拿token
let token = jsb.reflection.callStaticMethod("com/tf/y/utils/SPUtil", "getStr", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", "token", "None");
//封装请求头
xhr.setRequestHeader("Authorization", "Bearer" + " " + token);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(data));
xhr.onload = function(e) {};
xhr.onreadystatechange = function(e) {
if (xhr.readyState == 4 && xhr.status == 200) {
var xhrRes = (new Function("return " + xhr.responseText))();
callback(xhrRes);
}
};
};
Weixinpay.js
function weixinpay() {
data = {'total_fee':201}
httpPostTest('payment/', data, function(res) {
// res 是预付款订单 将res转为str
// 关于支付的所有打印为 pay
cc.log('============pay========',JSON.stringify(res))
// 将预支付订单信息传给java 拉起支付
jsb.reflection.callStaticMethod("com/tf/y/AppActivity", "WeiXinPay", "(Ljava/lang/String;)V",JSON.stringify(res))
})
}
Android 拉起微信支付
导入微信sdk以及阿里fastjson
dependencies{
···
···
compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
compile 'com.alibaba:fastjson:1.2.54'
}
AppActivity.java
// com.tf.y 就是cocos运行出android 生成的 包名(org.cocos2d.javascript) 可以根据微信开放平台的包名修改
package com.tf.y;
import android.util.Log;
import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.alibaba.fastjson.JSONObject;
import java.util.Timer;
import java.util.TimerTask;
public class AppActivity extends Cocos2dxActivity {
private static String APPID = ""; // 在微信申请的appid
public static IWXAPI api;
//中间部分是 onCreate 就省略了
···
public static void WeiXinPay(String str){
Log.d("pay","开启微信支付");
api = WXAPIFactory.createWXAPI(getActivity(),APPID,true);
PayReq request = new PayReq();
// str 是django生成的预支付订单 js传来的时候是string 需要转换为json
JSONObject jsonObject = JSONObject.parseObject(str);
api.registerApp(APPID);
request.appId = jsonObject.getString("appid");
request.partnerId = jsonObject.getString("partnerid");
request.prepayId= jsonObject.getString("prepayid");
request.packageValue = "Sign=WXPay";
request.nonceStr= jsonObject.getString("noncestr");
request.timeStamp= jsonObject.getString("timestamp");
request.sign= jsonObject.getString("sign");
api.sendReq(request);
}
// 调取cocos
public static void callJsWxPAy(final int value){
// paymentResult cocos中的函数 随便写在一个位置就行
// value 返回的状态码
// 0 成功
// -1 签名错误
// -2 用户取消
final String exes = "paymentResult('"+ value + "')";
TimerTask task = new TimerTask(){
public void run(){
//execute the task
Cocos2dxGLSurfaceView.getInstance().queueEvent(new Runnable() {
@Override
public void run() {
Cocos2dxJavascriptJavaBridge.evalString("cc.log('微信调取js----java log')");
Cocos2dxJavascriptJavaBridge.evalString(exes);
}
});
}
};
Timer timer = new Timer();
timer.schedule(task, 100);
}
}
WXPayEntryActivity.java
// 需要在 微信平台填写的包名下新建 wxapi/WXPayEntryActivity.java
// com.tf.y 必须和微信平台写的包名一样
package com.tf.y.wxapi;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tf.y.AppActivity;
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
private IWXAPI wxAPI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wxAPI = WXAPIFactory.createWXAPI(this, "<appid>");
wxAPI.handleIntent(getIntent(), this);
}
@Override
protected void onNewIntent(Intent intent){
super.onNewIntent(intent);
setIntent(intent);
wxAPI.handleIntent(intent, this);
}
@Override
public void onReq(BaseReq baseReq) {}
// 微信回调执行的方法
@Override
public void onResp(BaseResp baseRes) {
Log.i("ansen", "微信支付回调 返回错误码:"+baseRes.errCode+" 错误名称:"+baseRes.errStr);
// callJsWxPAy 是调用cocos的方法 在上面有AppActivity.java
AppActivity.callJsWxPAy(baseRes.errCode);
finish();
}
}
AndroidManifest.xml
<activity
android:name="com.tf.y.wxapi.WXPayEntryActivity"
android:exported="true"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="<你的appid>"/>
</intent-filter>
</activity>
支付成功django回调
# 在生成预支付订单的时候上面设置了回调
from django.http import HttpResponse, JsonResponse
from rest_framework.views import APIView
import xml.etree.ElementTree as et
class WxpayNotify(APIView):
def post(self,request):
_xml = request.body
xml = str(_xml, encoding="utf-8")
tree = et.fromstring(xml)
return_code = tree.find("return_code").text
err_resp = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[Error]]></return_msg></xml>"
success_resp = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>"
if return_code == 'FAIL':
return HttpResponse(err_resp, content_type='text/xml', status=200)
elif return_code == 'SUCCESS':
order_id = tree.find("out_trade_no").text
wx_order = tree.find("transaction_id").text
time_end = tree.find("time_end").text
# 根据具体业务写就好
return HttpResponse(success_resp, content_type='text/xml', status=200)
url.py
path('weixin_notify/', WxpayNotify.as_view()), # 微信支付回调
更多推荐
已为社区贡献1条内容
所有评论(0)