quick-cocos2d-x 实现在lua里面完成android支付宝的接入
quick-cocos2d-x 实现在lua里面完成android支付宝的接入
一、支付宝注册是很麻烦的一个过程,本文就不解释了,想了解的去官网看下注册流程。然后下载他们的sdk-WS_SECURE_PAY_SDK。
二、开始集成。
1、在eclipse里面导入alipay的工程库 WS_SECURE_PAY_SDK/paydemo/Android_SDK/alipay_lib
然后在自己的项目里面引用一下。
2、写自己的java类。keys.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
/* * * 提示:如何获取安全校验码和合作身份者id * 1.用您的签约支付宝账号登录支付宝网站(www.alipay.com) * 2.点击“商家服务”(https://b.alipay.com/order/myorder.htm) * 3.点击“查询合作者身份(pid)”、“查询安全校验码(key)” */ package xxxx.xx; // // 请参考 Android平台安全支付服务(msp)应用开发接口(4.2 RSA算法签名)部分,并使用压缩包中的openssl RSA密钥生成工具,生成一套RSA公私钥。 // 这里签名时,只需要使用生成的RSA私钥。 // Note: 为安全起见,使用RSA私钥进行签名的操作过程,应该尽量放到商家服务器端去进行。 public final class Keys { //合作身份者id,以2088开头的16位纯数字 public static final String DEFAULT_PARTNER = "208880102xxxxxxx" ; //收款支付宝账号 public static final String DEFAULT_SELLER = "" ; //商户私钥,自助生成 public static final String PRIVATE = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBALNoLrQtQa399WliyKE4BH7NBG/naFOEspnVFYsXCqmj0IBePtcD/NuM5oKxl1wy3gWRTndeIoVlSZY2j8otkxbKv+94E45DDV3UYoi/iIM4S7eWhe4PcEiauFJsySv4ND3UGzTHt21l89sP0LlbPQiPpJNL2EXyBoT9fwPfhjsdhfBAAECgYEAiTKU9MjfP3XE5VDb6R5zoZybVhB7YdNK2gVwMivuxYg2/NQV7kxoZGW+KhgxjhqAW3WhsftVHvo0aiFTmON9ZVoM+sZeQpE490yyE/ZgFziN3fI6xHM0UMzFj0IBSl47cdLDCKEdJO7Azg3fX4E3y57tTFNhaXVL1Ib1qDbND0ECQQDmM6bqkgm89PWUKp3XvdkTXjdLUmVIpxdm8/K+PySxO28ClSHpKo8qjZs+P4Dyf/ld1Hp9OouRqLHA/QWp5IopAkEAx4NCMQv9QgJBqss0+oHEHrX+43nJFe9Jh+ndrTVTmkNVIC6ENCPhD/Z5HbgtPXpQjVpPvqPKv7cNN7xsHxWZTQJAKMi/6tEizoprs+t0+ZMWEXtguoSHkz6fQ8s2l4QiYRARk9iOIHw8zyI1ferMUqpfqMMLTIKB1bWYgIOTbE0kOQJBAJyThZoKZ7qrHLAWhEBpIOpdOGl7xXVictmoj2GE7VSurRrxhq2IzvOBInJRfae224rjqpvZ3Z9x4HyyckQ+5i0CQQCUZWxGXzCILTSre9aHa58X2FiGkJgiCuok/CZMkcd6KUUoVJ7G9iNAduDpgsxenuAv8zatHY+UdpkSXZWaklhZ" ; public static final String PUBLIC = "MIGfMA0GCSqGSIbhfjadskhfUAA4GNADCBiQKBgQCzaC60LUGt/fVpYsihOAR+zQRv52hThLKZ1RWLFwqpo9CAXj7XA/zbjOaCsZdcMt4FkU53XiKFZUmWNo/KLZMWyr/veBOOQw1d1GKIv4iDOEu3loXuD3BImrhSbMkr+DQ91Bs0x7dtZfPbD9C5Wz0Ij6STS9hF8gaE/X8D/K0PVQIDAQAB" ; } |
Alipay.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
public class QLALiPay { public static final String TAG = "alipay-sdk" ; private static final int RQF_PAY = 1 ; private static final int RQF_LOGIN = 2 ; private static Cocos2dxActivity context; private static int luaCallbackFunction = - 999 ; public static void setContext(Cocos2dxActivity context_) { context = context_; } public static boolean payAli( final String pid, final String sub, final String body, final int total, int luaCallbackFunction_) { luaCallbackFunction = luaCallbackFunction_; String info = getNewOrderInfo(pid,sub,body,total); String sign = Rsa.sign(info, Keys.PRIVATE); sign = URLEncoder.encode(sign); info += "&sign=\"" + sign + "\"&" + getSignType(); Log.i( "ExternalPartner" , "start pay" ); // start the pay. Log.i(TAG, "info = " + info); Cocos2dxLuaJavaBridge.callLuaFunctionWithString(luaCallbackFunction_, "hahahahahah" ); final String orderInfo = info; new Thread() { public void run() { AliPay alipay = new AliPay(context, mHandler); //设置为沙箱模式,不设置默认为线上环境 //alipay.setSandBox(true); String result = alipay.pay(orderInfo); Log.i(TAG, "result = " + result); Message msg = new Message(); msg.what = RQF_PAY; msg.obj = result; mHandler.sendMessage(msg); // 处理返回结果 } }.start(); return true ; } private static String getSignType() { return "sign_type=\"RSA\"" ; } public static void log(String msg){ Log.d(TAG, msg); } static Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case RQF_PAY: case RQF_LOGIN: { Result result = new Result((String) msg.obj); Toast.makeText(context, result.getResult(), Toast.LENGTH_SHORT).show(); Cocos2dxLuaJavaBridge.callLuaFunctionWithString(luaCallbackFunction, (String) msg.obj); Cocos2dxLuaJavaBridge.releaseLuaFunction(luaCallbackFunction); } break ; default : break ; } }; }; private static String getNewOrderInfo( final String pid, final String sub, final String body, final int total) { StringBuilder sb = new StringBuilder(); sb.append( "partner=\"" ); sb.append(Keys.DEFAULT_PARTNER); sb.append( "\"&out_trade_no=\"" ); sb.append(pid); //getOutTradeNo() sb.append( "\"&subject=\"" ); sb.append(sub); sb.append( "\"&body=\"" ); sb.append(body); sb.append( "\"&total_fee=\"" ); sb.append(total); //total sb.append( "\"¬ify_url=\"" ); // 网址需要做URL编码 sb.append(URLEncoder.encode( "http://html" )); sb.append( "\"&service=\"mobile.securitypay.pay" ); sb.append( "\"&_input_charset=\"UTF-8" ); sb.append( "\"&return_url=\"" ); sb.append(URLEncoder.encode( "http://m.alipay.com" )); sb.append( "\"&payment_type=\"1" ); sb.append( "\"&seller_id=\"" ); sb.append(Keys.DEFAULT_SELLER); // 如果show_url值为空,可不传 // sb.append("\"&show_url=\""); sb.append( "\"&it_b_pay=\"1m" ); sb.append( "\"" ); return new String(sb); } private static String getOutTradeNo() { SimpleDateFormat format = new SimpleDateFormat( "MMddHHmmss" ); Date date = new Date(); String key = format.format(date); java.util.Random r = new java.util.Random(); key += r.nextInt(); key = key.substring( 0 , 15 ); Log.d(TAG, "outTradeNo: " + key); return key; } } |
3、在lua里面调用Alipay的静态方法payAli,把你需要的参数传到payAli的方法里面,包括回调函数。在java里面通
Cocos2dxLuaJavaBridge.callLuaFunctionWithString来回调lua的方法,然后在lua里面告诉服务器玩家的支付结果,服务器也需要去验证一次支付,服务器端就不说了。
-- 获取android版billid function buyProxy.getALiPayBill( vo ) if isNet() then f.showLoading() local function alipCallBack( dataString ) buyProxy.alipayTimes = 0 if type(dataString) == "string" then buyProxy.aliPayResult(dataString,vo) end end local function back( returnData ) if device.platform == "android" then local args = { tostring(returnData.data.billid), "天天弹钢琴", "购买钻石 X"..vo.diamond, vo.money, alipCallBack } buyProxy.alipayBillId = returnData.data.billid local sig = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;II)Z" local AliPayClass = "com/funhappy/game/piano/QLALiPay" local ok, ret = luaj.callStaticMethod(AliPayClass, "payAli", args,sig) --没有返回默认nil if ok then print("getIsValid,ret:",ret) return ret else print("shareEvent error code = ", ret) end end end local msg = { productid = vo.pID} NetUtil.postHttp(GAME_SERVER.."c=alipay&m=get_billid",back,msg) else f.showAlert("网络不稳定,请检查您的网络") end end