支付細節的實現,主要通過支付寶提供的壹個支付安全服務安裝包alipay_plugin_20120428msp.apk,首次使用,首先檢查是否安裝此插件,沒有會提示安裝。具體的支付細節,在這個插件內完成。大部分的支付平臺也都是采用的這種方式。
調用支付寶的接口進行支付,主要有以下幾個步驟
1. 將商戶ID,收款帳號,外部訂單號,商品名稱,商品介紹,價格,通知地址封裝成訂單信息
2. 對訂單信息進行簽名
3. 將訂單信息,簽名,簽名方式封裝成請求參數
4. 調用pay方法。
主要流程圖如下:
支付接口pay方法的調用如下:
。。。
// start pay for this order.
// 根據訂單信息開始進行支付
try {
// prepare the order info.
// 準備訂單信息
String orderInfo = getOrderInfo(position);
// 這裏根據簽名方式對訂單信息進行簽名
String signType = getSignType();
String strsign = sign(signType, orderInfo);
Log.v("sign:", strsign);
// 對簽名進行編碼
strsign = URLEncoder.encode(strsign);
// 組裝好參數
String info = orderInfo + "&sign=" + "\"" + strsign + "\"" + "&"
+ getSignType();
Log.v("orderInfo:", info);
// start the pay.
// 調用pay方法進行支付
MobileSecurePayer msp = new MobileSecurePayer();
boolean bRet = msp.pay(info, mHandler, AlixId.RQF_PAY,this);
if (bRet) {
// show the progress bar to indicate that we have started
// paying.
// 顯示“正在支付”進度條
closeProgress();
mProgress = BaseHelper.showProgress(this,null, "正在支付",false,
true);
} else
} catch (Exception ex) {
Toast.makeText(AlixDemo.this, R.string.remote_call_failed,
Toast.LENGTH_SHORT).show();
}
。。。
/**
* 向支付寶發送支付請求
*
* @param strOrderInfo
* 訂單信息
* @param callback
* 回調handler
* @param myWhat
* 回調信息
* @param activity
* 目標activity
* @return
*/
public boolean pay(final String strOrderInfo,final Handler callback,
final int myWhat, final Activity activity) {
if (mbPaying)
return false;
mbPaying = true;
//
mActivity = activity;
// bind the service.
// 綁定服務
if (mAlixPay ==null) {
// 綁定安全支付服務需要獲取上下文環境,
// 如果綁定不成功使用mActivity.getApplicationContext().bindService
// 解綁時同理
mActivity.getApplicationContext().bindService(
new Intent(IAlixPay.class.getName()),mAlixPayConnection,
Context.BIND_AUTO_CREATE);
}
// else ok.
// 實例壹個線程來進行支付
new Thread(new Runnable() {
public void run() {
try {
// wait for the service bind operation to completely
// finished.
// Note: this is important,otherwise the next mAlixPay.Pay()
// will fail.
// 等待安全支付服務綁定操作結束
// 註意:這裏很重要,否則mAlixPay.Pay()方法會失敗
synchronized (lock) {
if (mAlixPay ==null)
lock.wait();
}
// register a Callback for the service.
// 為安全支付服務註冊壹個回調
mAlixPay.registerCallback(mCallback);
// call the MobileSecurePay service.
// 調用安全支付服務的pay方法
String strRet =mAlixPay.Pay(strOrderInfo);
BaseHelper.log(TAG,"After Pay: " + strRet);
// set the flag to indicate that we have finished.
// unregister the Callback, and unbind the service.
// 將mbPaying置為false,表示支付結束
// 移除回調的註冊,解綁安全支付服務
mbPaying = false;
mAlixPay.unregisterCallback(mCallback);
mActivity.getApplicationContext().unbindService(
mAlixPayConnection);
// send the result back to caller.
// 發送交易結果
Message msg = new Message();
msg.what = myWhat;
msg.obj = strRet;
callback.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
// send the result back to caller.
// 發送交易結果
Message msg = new Message();
msg.what = myWhat;
msg.obj = e.toString();
callback.sendMessage(msg);
}
}
}).start();
return true;
}
調用了支付服務之後,有兩種方式返回交易結果:
1. 支付結果作為接口返回的字符串返回。返回的參數包含在result字符串中,具體再進行解析。
2. 支付寶服務器通知。商戶需要提供壹個http協議的接口,包含在參數裏傳遞給安全支付,即notify_url。支付寶服務器在支付完成後,會用POST方法調用notufy_url,以xml為數據格式傳輸支付結果。需要註意的是,商戶服務器收到支付寶發的通知之後,需要返回壹個純字符串“success”,不然支付寶的服務器會持續調用七次回調url提供的接口。