Victory 2016-10-10 15:23:08 2222次浏览 0条回复 8 4 1

软件内嵌支付宝扫码开发——当面付

忙了两周了,今天终于将支付宝的扫码功能搞定了,所以在这里做一个笔记,主要记录一下在开发“当面付”过程中遇到的一些问题以及解决方法。
支付宝扫码支付是一种使用频率比较高的付款方式,主流的扫码支付还有微信,而且支付宝扫码接口也是在微信推出扫码支付后才开发完成的,所以对有过微信支付开发经验的人来说支付宝二维码支付实现起来会轻松一些,本人开始接到项目需求时也是感觉无从下手,只能搜集资料和大量查阅文档,“摸着石头过河”是这个时候唯一的办法了。可能有人看到题目时认为这个功能实现起来应该不难,可以直接将二维码图片固定到软件内部,让客户付款就好了,我只能说“外行”,没有回调过程的付款方式是没有走完整个订单的流程,需求描述起来很简单:软件内部嵌入支付宝扫码支付,而且要整个支付流程,包括订单生成维护、回调处理、二维码生成请求处理等。我想详尽的描述一下整个开发流程,会附上部分代码。

流程当面付文档,文档描述了开发当面付的大致流程,一些名词解释可以直接查阅文档,里边比较有趣的是秘钥配置和集成配置SDK,具体文档自己感悟一下,支付的安全性也由秘钥生成的签名来保障。

接口:蚂蚁金服在SDK中集成了所有有关支付的接口,当面付使用到的接口为“alipay.trade.prcreate”,此接口的描述是“收银员通过收银台或商户后台调用支付宝接口,生成二维码后,展示给用户,由用户扫描二维码完成订单支付”,所以根据个人需求选择合适的接口。接口的请求参数和响应参数是开发者与服务器交互的数据过程,请求demo:

$aop = new AopClient ();//Aop实例化执行对象
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';//同意请求接口
$aop->appId = 'your app_id';//应用的ID号
$aop->rsaPrivateKeyFilePath = 'merchant_private_key_file';
$aop->alipayPublicKey='alipay_public_key_file';//私钥和公钥要一一对应
$aop->apiVersion = '1.0';
$aop->postCharset='GBK';
$aop->format='json';
$request = new AlipayTradePrecreateRequest ();//统一下单请求
$request->setBizContent("{" .//获取订单内容,参数可查文档
"    \"out_trade_no\":\"20150320010101001\"," .
"    \"seller_id\":\"2088102146225135\"," .
"    \"total_amount\":88.88," .
"    \"discountable_amount\":8.88," .
"    \"undiscountable_amount\":80," .
"    \"buyer_logon_id\":\"15901825620\"," .
"    \"subject\":\"Iphone6 16G\"," .
"    \"body\":\"Iphone6 16G\"," .
"      \"goods_detail\":[{" .
"                \"goods_id\":\"apple-01\"," .
"        \"alipay_goods_id\":\"20010001\"," .
"        \"goods_name\":\"ipad\"," .
"        \"quantity\":1," .
"        \"price\":2000," .
"        \"goods_category\":\"34543238\"," .
"        \"body\":\"特价手机\"," .
"        \"show_url\":\"http://www.alipay.com/xxx.jpg\"" .
"        }]," .
"    \"operator_id\":\"yx_001\"," .
"    \"store_id\":\"NJ_001\"," .
"    \"terminal_id\":\"NJ_T_001\"," .
"    \"extend_params\":{" .
"      \"sys_service_provider_id\":\"2088511833207846\"," .
"      \"hb_fq_num\":\"3\"," .
"      \"hb_fq_seller_percent\":\"100\"" .
"    }," .
"    \"timeout_express\":\"90m\"," .
"    \"royalty_info\":{" .
"      \"royalty_type\":\"ROYALTY\"," .
"        \"royalty_detail_infos\":[{" .
"                    \"serial_no\":1," .
"          \"trans_in_type\":\"userId\"," .
"          \"batch_no\":\"123\"," .
"          \"out_relation_id\":\"20131124001\"," .
"          \"trans_out_type\":\"userId\"," .
"          \"trans_out\":\"2088101126765726\"," .
"          \"trans_in\":\"2088101126708402\"," .
"          \"amount\":0.1," .
"          \"desc\":\"分账测试1\"," .
"          \"amount_percentage\":\"100\"" .
"          }]" .
"    }," .
"    \"sub_merchant\":{" .
"      \"merchant_id\":\"19023454\"" .
"    }," .
"    \"alipay_store_id\":\"2016052600077000000015640104\"" .
"  }");
$result = $aop->execute ( $request); //执行订单请求,返回结果
 
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if(!empty($resultCode)&&$resultCode == 10000){
echo "成功";
} else {
echo "失败";
}

响应:

{
    "alipay_trade_precreate_response":{
        "code":"10000",
        "msg":"Success",
        "out_trade_no":"6823789339978248",
        "qr_code":"https://qr.alipay.com/bavh4wjlxf12tper3a"
    },
    "sign":"ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
}

响应结果以对象形式返回,可以打印执行请求后返回的结果,提取信息,也可以封装为JSON以便使用,其他配置(如回调路径设置)可以通过调用AlipayTradePrecreateRequest内部的方法进行设置,参数设置查看文档。

回调:完成整个订单后,支付宝服务器会根据当时请求内部(注意:不要注释系统参数中的回调参数)设置的回调路经进行回调,通知订单完成情况,回调处理处理,涉及验签,Aop内部封装了验签函数,直接调用即可。
DEMO:多语言版本的集成包下载文档。
原创帖,转载请注明出处

觉得很赞
    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册