遊戲支付平台付實際上就是買賣雙方交易過程中的“第三方中介”,也可以說是“技術中介”。這裡我選擇豌豆莢SDK平台,實現遊戲支付功能。
支付流程:
配置遊戲伺服器端回調地址URL遊戲用戶端使用申請好的Appkey_id,secretkey遊戲用戶端使用doLogin方法調用豌豆莢登入登入成功(onSuccess)後,遊戲用戶端提交使用者資訊(uid,nick,token)到遊戲伺服器遊戲伺服器提交uid,token到豌豆莢伺服器進行驗證,驗證通過後遊戲完成登入如果有角色建立,遊戲用戶端調用createRole方法建立角色,註冊到豌豆莢伺服器,可選調用遊戲用戶端建立訂單,設定遊戲訂單號(order.out_trade_no),發起支付(pay)玩家完成支付,返回遊戲用戶端只有支付成功的訂單,豌豆莢伺服器通知遊戲伺服器回調地址,包含order.out_trade_no遊戲伺服器端驗證回調資訊中的簽名,使用RsaTest項目中的公開金鑰既可,驗證通過後發放遊戲道給遊戲用戶端並返回success字串,處理失敗返回fail字串遊戲用戶端開始使用道。
注釋:
有關加入豌豆莢SDK具體操作可以參考《技術文檔》。
大家先看下,我的目錄結構:
<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD48cD4gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDxpbWcgc3JjPQ=="http://www.2cto.com/uploadfile/Collfiles/20140403/201404030913357.jpg" alt="\" />
是不是和上次博文類似呢?就是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