遊戲接入百度雲平臺,玩家可使用百度賬號登入遊戲,並進行相應的充值操作,其中充值方式有:
<1>百鑽;
<2>信用卡;
<3>儲蓄卡;
<4>支付寶;
<5>銀聯
工具/原料
方法/步驟
在百度開發中心註冊並提交應用,獲取APP_ID和API Key的值,在接入SDK時需要用到;
將百度SDK匯入到Eclipse IDE中,在遊戲專案引入該型別為library的百度SDK
配置AndroidManifest.xml檔案
android:name="com.unionpay.upomp.bypay.activity.SplashActivity"
android:screenOrientation="portrait" />
android:name="com.unionpay.upomp.bypay.activity.CardMainActivity"
android:screenOrientation="portrait" />
android:name="com.unionpay.upomp.bypay.activity.PayMainActivity"
android:screenOrientation="portrait" />
android:name="com.unionpay.upomp.bypay.activity.PayItActivity"
android:screenOrientation="portrait" />
android:name="com.unionpay.upomp.bypay.activity.PayResultActivity"
android:screenOrientation="portrait" />
android:name="com.unionpay.upomp.bypay.activity.AccountMainActivity"
android:screenOrientation="portrait" />
android:name="com.unionpay.upomp.bypay.activity.LoginActivity"
android:screenOrientation="portrait" />
android:name="com.unionpay.upomp.bypay.activity.RegisterActivity"
android:screenOrientation="portrait" />
android:name="com.unionpay.upomp.bypay.activity.GetpassActivity"
android:screenOrientation="portrait" />
android:name="com.unionpay.upomp.bypay.activity.EditpassActivity"
android:screenOrientation="portrait" />
android:name="com.unionpay.upomp.bypay.activity.EditinfoActivity"
android:screenOrientation="portrait" />
android:name="com.unionpay.upomp.bypay.activity.AddCardActivity"
android:screenOrientation="portrait" />
android:name="com.unionpay.upomp.bypay.activity.UserProtocalActivity"
android:screenOrientation="portrait" />
android:name="com.unionpay.upomp.bypay.activity.CheckCardBalanceActivity"
android:screenOrientation="portrait" />
android:name="com.unionpay.upomp.bypay.activity.CardInfoActivity"
android:screenOrientation="portrait" />
android:name="com.unionpay.upomp.bypay.activity.CardBankResultActivity"
android:screenOrientation="portrait" />
android:name="com.baidu.android.pay.ui.WebViewActivity"/>
android:name="com.baidu.android.pay.ui.SupportedCreditsActivity" />
android:name="com.baidu.android.pay.ui.game.BindCreditAcitvity" />
android:name="com.baidu.android.pay.ui.game.BindDebitFirstActivity" />
android:name="com.baidu.android.pay.ui.game.BindDebitSecondAcitvity"/>
android:name="com.baidu.android.pay.ui.game.BondCardPayActivity"/>
android:name="com.baidu.android.pay.ui.game.PayResultActivity" />
android:name="com.baidu.android.pay.ui.game.SurplusPayActivity"/>
android:name="com.baidu.android.pay.ui.SampleLoginActivity" />
android:name="com.baidu.android.pay.ui.DynamicLoginActivity" />
android:name="com.baidu.android.pay.ui.SocialLoginAcitivity" />
android:name="com.baidu.android.pay.ui.FillSecurePWD" />
新增許可權
配置元資料
android:name="BaiduMobAd_CHANNEL"
android:value="baidu" />
android:name="BaiduMobAd_STAT_ID"
android:value="fcce056759" />
android:name="BaiduMobAd_EXCEPTION_LOG"
android:value="true" />
android:name="BaiduMobAd_SEND_STRATEGY"
android:value="APP_START" />
android:name="BaiduMobAd_TIME_INTERVAL"
android:value="2" />
android:name="BaiduMobAd_ONLY_WIFI"
android:value="false" />
android:name="BaiduMobAd_CELL_LOCATION"
android:value="true" />
百度雲開放平臺SDK的使用者登入類,I114gBoxBaiduLoginActivity.java,其程式碼內容如下:
package com.i114gbox.aglieguy.op.baidu;
/**
* 百度雲平臺登入
*
* @author SJC
*
*/
public class I114gBoxBaiduLoginActivity extends I114gBoxActivity {
private static String TAG = "I114gBoxBaiduLoginActivity";
public int ret;
public Button payButton;
public CheckBox is_third;
private Context ctx = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
I114gBoxLogUtils.d(TAG, "The onCreate method execute.");
super.onCreate(savedInstanceState);
I114gBoxCollectActivityUtils.getInstance().addActivity(this);// 收集Activity
ctx = this;
SharedPreferences preferences = getSharedPreferences("mycfg",
MODE_PRIVATE);
int storedPreference = preferences.getInt("mykey", 0);
if (storedPreference == 1) {// 橫屏
// 強制為橫屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {// 豎屏
// 強制為豎屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
// SDK初始化
SDKApi.init(I114gBoxBaiduLoginActivity.this, storedPreference,
I114gBoxBaiduPayConfig.appid);
// 顯示介面檢視
showView(storedPreference);
}
/**
* 簽名拼寫
*
* @param appid
* @param appKey
* @param CurrentTime
* @return
*/
public String Sign(String appid, String appKey, String CurrentTime) {
StringBuilder sb = new StringBuilder();
sb.append(appid).append(appKey).append(CurrentTime);
String unSignValue = sb.toString();
return unSignValue;
}
/**
* 簽名拼寫
*
* @param appid
* @param appKey
* @param CurrentTime
* @return
*/
public String Sign_Login_Callback_Ex(String appid, String appKey,
String CurrentTime, String token) {
StringBuilder sb = new StringBuilder();
// 按照升序排列
sb.append(appid).append(appKey).append(CurrentTime).append(token);
String unSignValue = sb.toString();
Log.e("tag", "unSignValue:" + unSignValue);
return unSignValue;
}
/**
* 簽名拼寫
*
* @param appid
* @param appKey
* @param CurrentTime
* @return
*/
public String Sign_Login_Callback(String appid, String appKey,
String CurrentTime, String userName, long userID) {
StringBuilder sb = new StringBuilder();
// 按照升序排列
sb.append(appid).append(appKey).append(CurrentTime).append(userName)
.append(userID);
String unSignValue = sb.toString();
Log.e("tag", "unSignValue:" + unSignValue);
return unSignValue;
}
// MD5加密,32位
public static String MD5(String str) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
e.printStackTrace();
return "";
}
char[] charArray = str.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++) {
byteArray[i] = (byte) charArray[i];
}
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
/** 顯示介面檢視 **/
public void showView(int sdkType) {
setContentView(I114gBoxResourceUtils.getLayoutId(ctx,
"activity_baidu_login"));
is_third = (CheckBox) findViewById(I114gBoxResourceUtils.getId(ctx,
"cb_baidu_login_support_third"));
// 簡化模式登入
findViewById(
I114gBoxResourceUtils.getId(ctx, "btn_baidu_login_simplified"))
.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
boolean flag = is_third.isChecked();
final String time = System.currentTimeMillis() + "";
final String sign = MD5(Sign(
I114gBoxBaiduPayConfig.appid,
I114gBoxBaiduPayConfig.appkey, time));
// 呼叫簡化模式登入
SDKApi.loginUI(I114gBoxBaiduLoginActivity.this,
I114gBoxBaiduPayConfig.appid, time, sign,
new ILoginCallback() {
@Override
public void onCallBack(int retcode,
String userName, long uid,
String sign) {
I114gBoxLogUtils.i(TAG, "簡化模式登入==="
+ "userName:" + userName + " "
+ "uid:" + uid + " " + "sign:"
+ sign);
if (retcode == ILoginCallback.RETCODE_SUCCESS) {// 當簡化模式登入成功時
String orderEnc = MD5(Sign_Login_Callback(
I114gBoxBaiduPayConfig.appid,
I114gBoxBaiduPayConfig.appkey,
time, userName, uid));
if (sign.equalsIgnoreCase(orderEnc)) {
Toast.makeText(
ctx,
"簡化模式登入結果:"
+ userName
+ "登入成功,驗證簽名成功!"
+ uid,
Toast.LENGTH_SHORT)
.show();
// 進入遊戲
Intent intent = new Intent(
ctx,
I114gBoxBaiduPayActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(
ctx,
"APP登入結果:"
+ userName
+ "登入成功,驗證簽名失敗!"
+ uid,
Toast.LENGTH_LONG)
.show();
}
} else if (retcode == ILoginCallback.RETCODE_FAIL) {// 簡化模式登入失敗
Toast.makeText(ctx,
"APP登入結果:" + "登入失敗",
Toast.LENGTH_LONG).show();
}
}
}, flag, false);
}
});
// 標準模式登入
findViewById(
I114gBoxResourceUtils.getId(ctx, "btn_baidu_login_standard"))
.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
boolean flag = is_third.isChecked();
final String time = System.currentTimeMillis() + "";
final String sign = MD5(Sign(
I114gBoxBaiduPayConfig.appid,
I114gBoxBaiduPayConfig.appkey, time));
// 呼叫標準模式登入
SDKApi.loginUIEx(I114gBoxBaiduLoginActivity.this,
I114gBoxBaiduPayConfig.appid, time, sign,
new ILoginCallbackEx() {
@Override
public void onCallBack(int retcode,
String token, String sign) {
I114gBoxLogUtils.i(TAG, "標準模式登入==="
+ "token:" + token + " "
+ "sign:" + sign);
if (retcode == ILoginCallback.RETCODE_SUCCESS) {// 當標準模式登入成功時
String orderEnc = MD5(Sign_Login_Callback_Ex(
I114gBoxBaiduPayConfig.appid,
I114gBoxBaiduPayConfig.appkey,
time, token));
if (sign.equalsIgnoreCase(orderEnc)) {// 當驗證簽名成功時
Toast.makeText(
ctx,
"APP獲取Token結果:" + token
+ "成功,驗證簽名成功!",
Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(
ctx,
I114gBoxBaiduPayActivity.class);
startActivity(intent);
finish();
} else {// 當簽名驗證失敗時
Toast.makeText(
ctx,
"APP獲取Token結果:" + token
+ "成功,驗證簽名失敗!",
Toast.LENGTH_LONG)
.show();
}
} else if (retcode == ILoginCallback.RETCODE_FAIL) {// 當標準模式登入失敗時
Toast.makeText(
I114gBoxBaiduLoginActivity.this,
"APP獲取token失敗",
Toast.LENGTH_LONG).show();
}
}
}, flag, false);
}
});
}
}
百度雲開放平臺SDK的支付類,I114gBoxBaiduPayActivity.java,其內容如下:
package com.i114gbox.aglieguy.op.baidu;
public class I114gBoxBaiduPayActivity extends I114gBoxActivity {
private static String TAG = "I114gBoxBaiduPayActivity";
private CheckBox check;
private boolean misAysn;
private Context ctx = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
I114gBoxLogUtils.d(TAG, "The onCreate method execute.");
super.onCreate(savedInstanceState);
I114gBoxCollectActivityUtils.getInstance().addActivity(this);// 收集Activity
ctx = this;
SharedPreferences preferences = getSharedPreferences("mycfg",
MODE_PRIVATE);
final int storedPreference = preferences.getInt("mykey", 0);
if (storedPreference == 1) {// 橫屏
// 強制為橫屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {// 豎屏
// 強制為豎屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
// 設定佈局檔案
setContentView(I114gBoxResourceUtils.getLayoutId(ctx,
"activity_baidu_pay"));
// 獲取支付是否非同步請求
check = (CheckBox) findViewById(I114gBoxResourceUtils.getId(ctx,
"cb_baidu_pay_async"));
// 獲取需要充值的金額
final EditText etPrice = (EditText) findViewById(I114gBoxResourceUtils
.getId(ctx, "et_baidu_price"));
etPrice.setInputType(InputType.TYPE_CLASS_NUMBER);
// 支付
findViewById(I114gBoxResourceUtils.getId(ctx, "btn_baidu_pay"))
.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String price = etPrice.getText().toString().trim();
if (TextUtils.isEmpty(price)) {// 當充值金額為空時
Toast.makeText(ctx, "請輸入收費金額", Toast.LENGTH_SHORT)
.show();
etPrice.requestFocus();
} else if (Integer.parseInt(price) <= 0) {// 當充值金額輸入不合法時
Toast.makeText(ctx, "收費金額應大於0", Toast.LENGTH_SHORT)
.show();
etPrice.requestFocus();
} else {// 支付開始
SDKApi.init(I114gBoxBaiduPayActivity.this,
storedPreference,
I114gBoxBaiduPayConfig.appid);
/* waresid目前固定填1 price的單位為分,請注意 */
// 開始支付
startPay(1, Integer.parseInt(price)); // 請填寫商品自己的引數
}
}
});
// 退出
findViewById(I114gBoxResourceUtils.getId(ctx, "btn_baidu_exit"))
.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
/** 生成外部訂單號 請保證唯一性 **/
@SuppressLint("SimpleDateFormat")
public String genExorderno() {
DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssS");
Date date = new Date();
String orderNo = dateFormat.format(date);
I114gBoxLogUtils.i(TAG, "訂單號為:" + orderNo);
return orderNo;
}
/** 開始支付 **/
public void startPay(int waresid, int price) {
misAysn = check.isChecked();
PayRequest payRequest = new PayRequest();
// 應用方提供的通知URL
payRequest.addParam("notifyurl", I114gBoxBaiduPayConfig.notifyurl);
// 應用ID
payRequest.addParam("appid", I114gBoxBaiduPayConfig.appid);
// 計費系統中的商品編碼
payRequest.addParam("waresid", 1);
// 商品訂單號
payRequest.addParam("exorderno", genExorderno());
// 商品價格
payRequest.addParam("price", price);
// 擴充套件資訊,原樣返回
payRequest.addParam("cpprivateinfo", "123456");
if (misAysn)
payRequest.addParam("asyncflag", 1);
String paramUrl = payRequest
.genSignedUrlParamString(I114gBoxBaiduPayConfig.appkey);
SDKApi.startPay(I114gBoxBaiduPayActivity.this, paramUrl,
new IPayResultCallback() {
@Override
public void onPayResult(int resultCode, String signValue,
String resultInfo) {// resultInfo = 應用編號&商品編號&外部訂單號
I114gBoxLogUtils.i(TAG, "resultCode:" + resultCode
+ " " + "signValue:" + signValue + " "
+ "resultInfo:" + resultInfo);
if (SDKApi.PAY_SUCCESS == resultCode) {// 當支付成功時
if (null == signValue) {
// 沒有簽名值,預設採用finish(),請根據需要修改
Toast.makeText(ctx, "沒有簽名值", Toast.LENGTH_SHORT)
.show();
// //finish();
}
boolean flag = PayRequest.isLegalSign(signValue,
resultInfo, I114gBoxBaiduPayConfig.appkey);
if (flag) {
// 合法簽名值,支付成功,請新增支付成功後的業務邏輯
Toast.makeText(ctx, "支付成功", Toast.LENGTH_SHORT)
.show();
} else {
// 非法簽名值,預設採用finish(),請根據需要修改
Toast.makeText(ctx, "支付成功,但是驗證簽名失敗",
Toast.LENGTH_SHORT).show();
}
} else if (SDKApi.PAY_CANCEL == resultCode) {// 當支付取消時
// 取消支付處理,預設採用finish(),請根據需要修改
Toast.makeText(ctx, "取消支付", Toast.LENGTH_SHORT)
.show();
} else if (SDKApi.PAY_HANDLING == resultCode) {// 當支付正在處理中
// 如果返回支付正在處理,請通過您的伺服器查詢
Toast.makeText(ctx, "支付處理中", Toast.LENGTH_SHORT)
.show();
} else {// 當支付失敗時
// 計費失敗處理,預設採用finish(),請根據需要修改
Toast.makeText(I114gBoxBaiduPayActivity.this,
"支付失敗", Toast.LENGTH_LONG).show();
}
}
});
}
}