cocos2d-x与Android混编实现游戏支付功能

来源:互联网 发布:文华财经数据保存 编辑:程序博客网 时间:2024/05/05 06:32

      游戏支付平台付实际上就是买卖双方交易过程中的“第三方中介”,也可以说是“技术中介”。这里我选择豌豆荚SDK平台,实现游戏支付功能。


      支付流程:

  1. 配置游戏服务器端回调地址URL
  2. 游戏客户端使用申请好的Appkey_id,secretkey
  3. 游戏客户端使用doLogin方法调用豌豆荚登录
  4. 登录成功(onSuccess)后,游戏客户端提交用户信息(uid,nick,token)到游戏服务器
  5. 游戏服务器提交uid,token到豌豆荚服务器进行验证,验证通过后游戏完成登录
  6. 如果有角色创建,游戏客户端调用createRole方法创建角色,注册到豌豆荚服务器,可选调用
  7. 游戏客户端创建订单,设置游戏订单号(order.out_trade_no),发起支付(pay)
  8. 玩家完成支付,返回游戏客户端
  9. 只有支付成功的订单,豌豆荚服务器通知游戏服务器回调地址,包含order.out_trade_no
  10. 游戏服务器端验证回调信息中的签名,使用RsaTest项目中的公钥既可,验证通过后发放游戏道给游戏客户端并返回success字符串,处理失败返回fail字符串
  11. 游戏客户端开始使用道。

      注释:




      有关加入豌豆荚SDK具体操作可以参考《技术文档》。

      大家先看下,我的目录结构:


                               


      是不是和上次博文类似呢?就是cocos2d-x与Android混编实现换“头像图片”!对了,基本实现思路都是一样的,cocos2d-x 控件触发调用android方法实现的。

 因它和上篇博文步骤一样,就不多讲了,到时直接上传代码给大家参考吧。


      支付功能类:PayDemo.java

package com.wandoujia.wdpaydemo.wdj;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Looper;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import com.wandoujia.sdk.plugin.paydef.LoginCallBack;import com.wandoujia.sdk.plugin.paydef.PayCallBack;import com.wandoujia.sdk.plugin.paydef.User;import com.wandoujia.sdk.plugin.paydef.WandouAccount;import com.wandoujia.sdk.plugin.paydef.WandouOrder;import com.wandoujia.sdk.plugin.paydef.WandouPay;import com.wandoujia.sdk.plugin.paysdkimpl.PayConfig;import com.wandoujia.sdk.plugin.paysdkimpl.WandouAccountImpl;import com.wandoujia.sdk.plugin.paysdkimpl.WandouPayImpl;import com.wandoujia.wandoujiapaymentplugin.utils.MSG;public class PayDemo extends Activity implements OnClickListener {    private static final String TAG = "PayDemo";    final String appkey_id = "100000000";    // 开发者 安全秘钥    final String secretkey = "99b4efb45d49338573a00be7a1431511";    private WandouAccount account = new WandouAccountImpl();    private WandouPay wandoupay = new WandouPayImpl();    @Override    protected void onCreate(Bundle savedInstanceState) {        Log.e(TAG, "start onCreate~~~" + android.os.Build.VERSION.RELEASE);        super.onCreate(savedInstanceState);        //在onCreate中调用PayConfig.init初始化        PayConfig.init(this, appkey_id, secretkey);        gameLayout();    }    @Override    protected void onDestroy() {        super.onDestroy();        Log.e(TAG, "start onDestroy~~~" + android.os.Build.VERSION.RELEASE);    }    public void gameLayout() {        setContentView(R.layout.activity_main);        ((Button) findViewById(R.id.pay)).setOnClickListener(this);        ((Button) findViewById(R.id.login_button)).setOnClickListener(this);        ((Button) findViewById(R.id.logout_button)).setOnClickListener(this);    }    public String textString(int id) {        return ((TextView) findViewById(id)).getText().toString();    }    public void setText(final int id, final String str) {        // Request UI update on main thread.        new Handler(Looper.getMainLooper()).post(new Runnable() {            @Override            public void run() {                ((TextView) findViewById(id)).setText(str);            }        });    }    @Override    public void onClick(View v) {        int viewId = v.getId();        if (R.id.pay == viewId) {            Log.w(TAG, "doPayment!");            float money = Float.parseFloat(textString(R.id.money));            long moneyInFen = (long) (money * 100);            WandouOrder order =                    new WandouOrder(textString(R.id.subject), textString(R.id.desc), moneyInFen);            // 设置游戏订单号,最长50个字符            order.setOut_trade_no("GameOrderIdMaxLenth50");            // 触发支付            wandoupay.pay(PayDemo.this, order, new PayCallBack() {                @Override                public void onSuccess(User user, WandouOrder order) {                    Log.w("DemoPay", "onSuccess:" + order);                    setText(R.id.orderInfo, user.getNick() + " 支付成功!" + order);                }                @Override                public void onError(User user, WandouOrder order) {                    Log.w("DemoPay", "onError:" + order);                    setText(R.id.orderInfo, user.getNick() + " 支付失败!" + order);                }            });        } else if (R.id.login_button == viewId) {          Log.i(TAG, "PaySdk init.");            // PayConfig.init(this, appkey_id, secretkey);            Log.w(TAG, "doLogin!");            //触发登录            account.doLogin(PayDemo.this, new LoginCallBack() {                @Override                public void onSuccess(User user, int type) {                    Log.w("login", "success:+" + user);                    setText(R.id.account, user.toString());                    // 豌豆荚账户UID                    Long uid = user.getUid();                    // 豌豆荚账户昵称                    String nick = user.getNick();                    // 豌豆荚账户登录验证 Token ,15分钟内有效                    String token = user.getToken();                    // 1.请把uid,token 提交游戏服务器                    // 2.游戏服务器收到uid,token后提交给豌豆荚服务器验证                    // 3.验证通过后,游戏服务器生成一个 cookie 给游戏客户端使用                    // 4.游戏客户端得到游戏的cookie 与游戏服务器进行交互通信,保证身份验证安全                }                @Override                public void onError(int returnCode, String info) {                    // 请不要在这里重新调用 doLogin                    // 游戏界面上应该留有一个登录按钮,来触发 doLogin登录                    setText(R.id.account, "Demo中登陆失败:" + MSG.trans(info));                    Log.e(TAG, MSG.trans(info));                }            });        } else if (R.id.logout_button == viewId) {            Log.w(TAG, "doLogout!");            // doLogout无需处理回掉方法            account.doLogout(getApplicationContext(), null);            // Do the subsequent directly after doLogut was called.            // i.e. Finish your application.            // Then when launching your app next time, user will see the login UI again.        }    }}
最终运行效果:








代码:http://download.csdn.net/detail/my183100521/7138085


0 0
原创粉丝点击