扫码支付集成开发者指南

来自    当面付 | 
作者:王兰玉  |  更新于:2018-11-26 15:16:08     

本帖仅供参考,非官方demo

文档地址:点击【查看

sdk下载:点击【查看

官方demo下载地址:点击【查看

正式环境准备工作:

           1、签约:https://openclub.alipay.com/read.php?tid=276&fid=72

           2、创建应用:https://openclub.alipay.com/read.php?tid=1606&fid=72

           3、生成密钥:https://openclub.alipay.com/read.php?tid=1833&fid=69

           4、下载服务端sdk:https://docs.open.alipay.com/54/106370/

支付宝网关:

沙箱环境支付宝网:https://openapi.alipaydev.com/gateway.do   

正式环境支付宝网关:https://openapi.alipay.com/gateway.do

 

java版(适用于jdk1.5以上)

AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
		AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
		request.setReturnUrl("https://docs.open.alipay.com/200");
		request.setNotifyUrl("https://docs.open.alipay.com/200");
		AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
		//out_trade_no(订单号)必须是唯一的,测试一次必须修改新的订单号
		model.setOutTradeNo("201811287635213732424");
		model.setTotalAmount("0.1");
		model.setSubject("扫码支付测试");
		model.setTimeoutExpress("1m");
		model.setBody("body");
		//二维码失效时间,最小2m中,1m会直接失效
		model.setQrCodeTimeoutExpress("3m");
		request.setBizModel(model);
		AlipayTradePrecreateResponse response =  alipayClient.execute(request);
               //第三方请求,传入的是谁的授权token,资金就收款到谁的账户,不传token则收款到app_id对应的账户	
               // AlipayTradePrecreateResponse response =  alipayClient.execute(request,"","app_auth_token");
		if(response.isSuccess()){
			System.out.println("调用成功");
		} else {
			System.out.println("调用失败");
		}
		
		System.out.println("二维码码串:"+response.getQrCode());
		System.out.println("接口调用返回结果:"+response.getBody());

 

 

返回结果

二维码码串:https://qr.alipay.com/bax0366066dftdpgmyn40063
接口调用返回结果:{"alipay_trade_precreate_response":{"code":"10000","msg":"Success","out_trade_no":"201811287635213732424","qr_code":"https:\/\/qr.alipay.com\/bax0366066dftdpgmyn40063"},"sign":"By6cfRS9uONd4BgLDa60oW3DXVLhHgfOldkBH7ctx1sYh1qDdCDmf+rP9mbKXZA5IoQ47R8QNQ4fATrR0QMs/zAg6qgB9nP1Oj9zg4DVNOPXMUi1bGx2texH3W5cfGXpQ8ffVqKcnVk7qzufTBgjjZJfQ8jV1i8ZeTnd4+L662pk45EHTOBcElc04JXOwlTLurYiVw/9htN9m1KQE0pjKvstBF5fr57fP7e0RJBUAGw1oezyA+GQRjTrgpXFxu0wqzZxJ0ylN4xdH/ZBTd6IW7ClE5+VuNrlM/57T+UDqkDXzndsM4bRnwALN/16E4wp9MT/AJkRT0CqSYpkRWy46g=="}

 

php版(适用于php 5.5以上环境)

<?php
/**
 * 
 * 扫码支付文档地址:https://docs.open.alipay.com/api_1/alipay.trade.precreate
 */
require_once 'AopSdk.php';
$aop = new AopClient ();
$aop->gatewayUrl = 'https://openapi.alipaydev.com/gateway.do';
$aop->appId = '2016101800718925';
$aop->rsaPrivateKey = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDfDBUJhaw46ck8MHccIBXPt2USOvCRZ0dQNZZwC0EmZvcnL2+NUyoJMbgcV3U5o6wApajkpibf9UkrtYgI3/GZhOgzfzH0IJYAXmYCEniM+Cl5ipIWhY0W/s+uNRGNdXvNlketNNcVqAvkKZdcHz23N60BXsvjdR+BJ/np2CUmr9OCv0aqqwOO190rOuZvVaFCJD5LDmvDFdXPzrJHcOAyYjb2hhJIWt5n4o/b3LMYeuxfwWuQ4VlGx+s0wZJ1udUyAX8X8byqYcnevG8A1sQd/dRVNr8f1BxS8kjDIDljCzKPoYyoaoeJMhJIxrV8gL57oEIX7PbrlvFLplaC7g/XAgMBAAECggEAI4m/SFceC6tkPszSDY8nKoNj/TWa4u/7stH2+D8y0DWTp5CtS202w2RCm3Fr4hUr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bo2UAZAe6pwERzqOEEVLyBExEV3+qkDwIewx4lghFCQpwhO6SNWj4ppSgXh7XAss=';
$aop->alipayrsaPublicKey='MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnVuBUzhfo7kWUN/TS/kHmb4afsAlUVaTR3U5e74V0os1bzlfvh+cpwnwzaEIb36CR1OzeIIzXgARIW/zGAL1/PLG8dFEXQOIGkA0a8YXGVNX9KeWnFCnCQ5gQxrfJ3ryasXzWBxv5bex1VrVsQ2TZ1VdUD+S7dR4SWFHvyck8/xGt3Hz5peUqo/OIlfQpoTBXdqfnJNrTVAMqcQhrIWwgpSVgmUq+UhlZsDaiUvxWWFm9v9nY172jFfzup/DIET//OpIghL6AB2F+ywkCcHdZHiGklnpNoQKQ6XMVKaO/k2nlHU64Y4WGSgJnSG6B798ohZoi3pndrVPAH5+5BKEFQIDAQAB';
$aop->apiVersion = '1.0';
$aop->postCharset='utf-8';
$aop->format='json';
$aop->signType = 'RSA2';

//生成随机订单号
$date=date("YmdHis");
$arr=range(1000,9999);
shuffle($arr);
$request = new AlipayTradePrecreateRequest();
$request->setBizContent("{" .
    "\"out_trade_no\":\"".$date.$arr[0]."\"," .
    "\"total_amount\":\"0.01\"," .
	"\"subject\":\"tets\"," .
    "\"timeout_express\":\"2m\"" .
    "}");

$result = $aop->execute($request);
//第三方请求,传入的是谁的授权token,资金就收款到谁的账户,不传token则收款到app_id对应的账户
//$result = $aop->execute($request,"","app_auth_token");
var_dump($result);
?>

 

返回结果:

 public 'code' => string '10000' (length=5)
 public 'msg' => string 'Success' (length=7)
 public 'out_trade_no' => string '201811280635179895' (length=18)
 public 'qr_code' => string 'https://qr.alipay.com/bax06738zqab6gooosbs00ed' (length=46)
 public 'sign' => string 'ZH7BJQEM8Y+Rjfqm/FOztvwJ9EWa533AZkC+Xs0WlcYRvCmYyxHPYYPa1BuMPTC7uvQjCL1nm7BMwrofB8wZ604cWXwSgL/JIG7Qwx/69S5rfDuiFMepS6Y/G6RP6uijDQCblQW0h0EXWRQn9gQxDpjjpUheKx+WQH5cmpbatreCfOyeeEs4DI8K+1BpBfacV01e/ZorTuYaJYgIn2SueoPHIub5qN5TEvvpbDxF365ZwIJE7rgOivTHPXHm6JHkXdYV3oRehyJrcDzCg6GtGeKTWwm07bYhoLUNp2FH+36E0KpCClYtmo2fMe2eSAGtLRzw6Vz44hdD5P+8wBZjHg==' 


.net版

公共参数配置爱

 public class Config
    {
        //应用appId沙箱
        public static string appId = "2016101800718925";

        //支付宝网关
        public static string serverUrl = "https://openapi.alipaydev.com/gateway.do";

        //沙箱支付宝公钥
        public static string alipay_public_key = @"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnVuBUzhfo7kWUN/TS/kHmb4afsAlUVaTR3U5e74V0os1bzlfvh+cpwnwzaEIb36CR1OzeIIzXgARIW/zGAL1/PLG8dFEXQOIGkA0a8YXGVNX9KeWnFCnCQ5gQxrfJ3ryasXzWBxv5bex1VrVsQ2TZ1VdUD+S7dR4SWFHvyck8/xGt3Hz5peUqo/OIlfQpoTBXdqfnJNrTVAMqcQhrIWwgpSVgmUq+UhlZsDaiUvxWWFm9v9nY172jFfzup/DIET//OpIghL6AB2F+ywkCcHdZHiGklnpNoQKQ6XMVKaO/k2nlHU64Y4WGSgJnSG6B798ohZoi3pndrVPAH5+5BKEFQIDAQAB";

        //开发者原始私钥,千万不要用pkcs8格式的 沙箱
        public static string merchant_private_key = @"MIIEowIBAAKCAQEA3wwVCYWsOOnJPDB3HCAVz7dlEjrwkWdHUDWWcAtBJmb3Jy9vjVMqCTG4HFd1OaOsAKWo5KYm3/VJK7WICN/xmYToM38x9CCWAF5mAhJ4jPgpeYqSFoWNFv7PrjURjXV7zZZHrTTXFagL5CmXXB89tzetAV7L43UfgSf56dglJq/Tgr9GqqsDjtfdKzrmb1WhQiQ+Sw5rwxXVz86yR3DgMmI29oYSSFreZ+KP29yzGHrsX8FrkOFZRsfrNMGSdbnVMgF/F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h10o/vW7UOMgBcCPus5sy+21D7eElBltKBV3n8GtM6WQwwFmcc8QKjXj2j0iIADlArRAoGBAPM8kqdFHQUEDhCbk1CZR4H/hStI1DERFQYvQkIGyvIhf4Q5gN+AeM9CQpFhLBqI4kt0u9mSQV8a2fEaMCRqi7jWR1AA/nhM46AYWTiH59kdAaNlAGQHuqcBEc6jhBFS8gRMRFd/qpA8CHsMeJYIRQkKcITukjVo+KaUoF4e1wLL";

        public static string format = "json";


   
        //编码,无需修改
        public static string charset = "utf-8";

        //签名类型,支持RSA2(推荐!)、RSA
        public static string sign_type = "RSA2";

        //版本号,无需修改
        public static string version = "1.0";


        /// <summary>
        /// 私钥文件类型转换成纯文本类型
        /// </summary>
        /// <returns>过滤后的字符串类型私钥</returns>
        public static string getMerchantPriveteKeyStr()
        {
            StreamReader sr = new StreamReader(merchant_private_key);
            string pubkey = sr.ReadToEnd();
            sr.Close();
            if (pubkey != null)
            {
                pubkey = pubkey.Replace("-----BEGIN PUBLIC KEY-----", "");
                pubkey = pubkey.Replace("-----END PUBLIC KEY-----", "");
                pubkey = pubkey.Replace("\r", "");
                pubkey = pubkey.Replace("\n", "");
            }
            return pubkey;
        }

    }

 

请求示例

   IAopClient client = new DefaultAopClient("https://openapi.alipaydev.com/gateway.do", Com.Alipay.Config.appId, Com.Alipay.Config.merchant_private_key, "json", "1.0", "RSA2", Com.Alipay.Config.alipay_public_key, "GBK", false);
            AlipayTradePrecreateRequest alipayRequest = new AlipayTradePrecreateRequest();//创建API对应的request
            AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
            model.OutTradeNo = "2018092815181234561223434123";
            model.Subject = "扫码支付测试";
            model.TotalAmount = "0.01";
            model.QrCodeTimeoutExpress = "5";
            alipayRequest.SetBizModel(model);

            //第三方调用方式
            //AlipayTradePrecreateResponse response = client.Execute(alipayRequest, "", "app_auth_token");
            //自调用方式
            AlipayTradePrecreateResponse response = client.Execute(alipayRequest);
            Console.WriteLine("扫码支付返回信息:" + response.Body);

 

返回结果

扫码支付返回信息:{"alipay_trade_precreate_response":{"code":"10000","msg":"Succ
ess","out_trade_no":"2018092815181234561223434123","qr_code":"https:\/\/qr.alipa
y.com\/bax06292n6mzvqlvcefc00dd"},"sign":"eHPVyjPjaH/dXdnftit/WerHSihQvmFeA7Nu7P
xSVDHIBDLE+RTaBWBPLvVqQPY1VmCCxFr+2I0aG93OYzyCwHbN3oj5+VSLiJZhzmGJnxm33MD8Iokxn3
L3Q3f99O9ek3f58ui+92Mydy/jc/5ar9Ayh0moaW3GnskqPUTSAjryirUAAER/2Y6DU7srXe2O9TYofP
AqQRBMMjCSSmGZ+cZCKtzeL2yXU81X9pQUlB2wGpWyGTLTCrBZImNauSu4ihop7tMiSDN6j5xhC+twnH
3d5JwDB+8D7vBSEfBXMJ1Gj3JO0md7U3dnVu4w73lzvY4aoeP+PfuscKSiFxGtdw=="}

 

接口测试注意事项

1.将返回的二维码串(Qr_Code)传入前台生成二维码,用户使用手机支付宝扫码进行支付;我这边就偷懒使用工具生成了,各位别学我哈

 

 

2."code":"10000" 不代表支付成功,只是接口调用成功,请勿混为一谈,判断支付是否成功,请通过异步通知(notify_url)的返回值进行判断

3.qr_code_timeout_express:允许用户最晚付款时间,从接口调用成功开始计时,最小值2m;传值1m扫码支付会直接报二维码已失效,二维码有效时间是2小时,所以该参数设置值超过2h也是无效值;

4.timeout_express:最小值为1m,最大值为15t,不传默认15t;

5.app_auth_token:

  使用场景:用于不会开发的商家找服务商代发起请求用,避免商户获取密钥,

  如何获取:服务商使用第三方应用授权获取商家的app_auth_token,再将app_auth_token传入接口中进行请求,不传值app_auth_token,资金默认收入对应的appid,传值app_auth_token资金将收入到app_auth_token对应的授权商家账号中;

如何进行第三方应用授权请参考该帖:https://openclub.alipay.com/read.php?tid=1649&fid=68

 

6.如果遇到如下报错,请参考对应解决方案帖进行排查

  无权限的小伙伴:https://openclub.alipay.com/read.php?tid=1672&fid=72

  验签出错的小伙伴:https://openclub.alipay.com/read.php?tid=2432&fid=72

  验签失败的小伙伴:https://openclub.alipay.com/read.php?tid=1598&fid=69

 

如果还是报错,请按如下方式进行提问:

正确提问方式:使用环境(沙箱还是正式环境)+接口名称(中文+英文)+ 请求参数 + 报错信息详情

 

TIGERCHENG | 2018-12-01 19:41:44
楼主,能发篇nodejs的吗?
我的回复

返回顶部