在技术集成之前,商户需要进行注册,并签约安全支付服务。签约成功后可获取支付宝分配的合作商户ID(PartnerID),账户ID(SellerID),调用接口时使用。
支付细节的实现,主要通过支付宝提供的一个支付安全服务安装包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提供的接口。