Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用户取消支付,签名验证异常 #24

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions plugins/alipay/proj.android/src/org/cocos2dx/plugin/IAPAlipay.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ public void onCancel(DialogInterface dialog) {
mcontext.onKeyDown(KeyEvent.KEYCODE_BACK, null);
}
}


private static final int PAY_SUCCESS_STATUS_CODE = 9000;

private static void initUIHandle() {
//
// the handler use to receive the pay result.
Expand All @@ -186,24 +188,33 @@ public void handleMessage(Message msg) {

// 从通知中获取参数
try {
// 获取交易状态,具体状态代码请参看文档
String memo = "memo=";
int imemoStart = strRet.indexOf("memo=");
imemoStart += memo.length();
int imemoEnd = strRet.indexOf(";result=");
memo = strRet.substring(imemoStart, imemoEnd);
// 对通知进行验签
ResultChecker resultChecker = new ResultChecker(strRet);

int retVal = resultChecker.checkSign();
// 返回验签结果以及交易状态
if (retVal == ResultChecker.RESULT_CHECK_SIGN_FAILED) {
payResult(IAPWrapper.PAYRESULT_FAIL, "签名验证失败");
} else if (retVal == ResultChecker.RESULT_CHECK_SIGN_SUCCEED && resultChecker.isPayOk()) {
payResult(IAPWrapper.PAYRESULT_SUCCESS, "支付成功");
} else {
payResult(IAPWrapper.PAYRESULT_FAIL, "支付失败");
}
// 获取交易状态码,具体状态代码请参看文档
String tradeStatus = "resultStatus={";
int imemoStart = strRet.indexOf("resultStatus=");
imemoStart += tradeStatus.length();

int imemoEnd = imemoStart + 4;
tradeStatus = strRet.substring(imemoStart, imemoEnd);
int statusCode = Integer.valueOf(tradeStatus);
if(statusCode == PAY_SUCCESS_STATUS_CODE)
{
// 对通知进行验签
ResultChecker resultChecker = new ResultChecker(strRet);
int retVal = resultChecker.checkSign();
// 返回验签结果以及交易状态
if (retVal == ResultChecker.RESULT_CHECK_SIGN_FAILED) {
payResult(IAPWrapper.PAYRESULT_FAIL, "签名验证失败");
} else if (retVal == ResultChecker.RESULT_CHECK_SIGN_SUCCEED/* && resultChecker.isPayOk()*/) {
payResult(IAPWrapper.PAYRESULT_SUCCESS, "支付成功");
} else {
payResult(IAPWrapper.PAYRESULT_FAIL, "支付失败");
}
}
else
{
payResult(IAPWrapper.PAYRESULT_FAIL, "支付失败");
}

} catch (Exception e) {
e.printStackTrace();
payResult(IAPWrapper.PAYRESULT_FAIL, "结果解析失败");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ int checkSign() {

try {
JSONObject objContent = BaseHelper.string2JSON(this.mContent, ";");
/**
* 用户取消操作,返回值为
* resultStatus={6001};memo={操作已经取消。};result={}
* 下面对result的处理会出现NullPointException
*/
String result = objContent.getString("result");
result = result.substring(1, result.length() - 1);
// 获取待签名数据
Expand All @@ -77,33 +82,33 @@ int checkSign() {
return retVal;
}

int getResultStatus() {
int ret = 9000;
try {
JSONObject objContent = BaseHelper.string2JSON(this.mContent, ";");
String result = objContent.getString("resultStatus");
result = result.substring(1, result.length() - 1);

ret = Integer.parseInt(result);
} catch (Exception e) {
e.printStackTrace();
ret = 4001;
}
return ret;
}

private static final int PAY_SUCCESS_STATUS_CODE = 9000;
public boolean isPayOk() {
boolean isPayOk = false;

String success = getSuccess();
if (success.equalsIgnoreCase("true") &&
checkSign() == RESULT_CHECK_SIGN_SUCCEED &&
PAY_SUCCESS_STATUS_CODE == getResultStatus())
{
isPayOk = true;
}

return isPayOk;
}
// int getResultStatus() {
// int ret = 9000;
// try {
// JSONObject objContent = BaseHelper.string2JSON(this.mContent, ";");
// String result = objContent.getString("resultStatus");
// result = result.substring(1, result.length() - 1);
//
// ret = Integer.parseInt(result);
// } catch (Exception e) {
// e.printStackTrace();
// ret = 4001;
// }
// return ret;
// }
//
// private static final int PAY_SUCCESS_STATUS_CODE = 9000;
// public boolean isPayOk() {
// boolean isPayOk = false;
//
// String success = getSuccess();
// if (success.equalsIgnoreCase("true") &&
// checkSign() == RESULT_CHECK_SIGN_SUCCEED &&
// PAY_SUCCESS_STATUS_CODE == getResultStatus())
// {
// isPayOk = true;
// }
//
// return isPayOk;
// }
}