说明:
本帖是利用支付宝沙箱测试“单笔转账到支付宝账户”,demo中会配置个人的沙箱账号信息,方便大家测试。
测试环境:visual studio 2010及以上+.NET framework 3.5及以上
注意事项:
1.目前仅支持账户余额渠道付款。
2.转账额度
1)单日转出累计额度为100万元。
2)转账给个人支付宝账户,单笔最高5万元;转账给企业支付宝账户,单笔最高10万元。
特殊逻辑注意点:
1)如果商户重复请求转账,支付宝会幂等返回成功结果,商户必须对重复转账的业务做好幂等处理;如果不判断,
存在潜在的风险,商户自行承担因此而产生的所有损失。
2)如果调用alipay.fund.trans.toaccount.transfer掉单时,或返回结果code=20000时,或返回结果code=40004,
sub_code= SYSTEM_ERROR时,请调用alipay.fund.trans.order.query发起查询,如果未查询到结果,
请保持原请求不变再次请求alipay.fund.trans.toaccount.transfer接口。
3)商户处理转账结果时,对于错误码的处理,只能使用sub_code作为后续处理的判断依据,
不可使用sub_msg作为后续处理的判断依据。
参数解读:
1.amount 转账金额,只支持2位小数,小数点前最大支持13位,金额必须大于等于0.1元。
2.payer_show_name付款方姓名(最长支持100个英文/50个汉字)。显示在收款方的账单详情页。如果该字段不传
则默认显示付款方的支付宝认证姓名或单位名称。
3.payee_real_name收款方真实姓名(最长支持100个英文/50个汉字),如果本参数不为空,则会校验该账户在支付宝
登记的实名是否与收款方真实姓名一致。
4.remark转账备注(支持200个英文/100个汉字)。当付款方为企业账户,且转账金额达到(大于等于)50000元,
remark不能为空。收款方可见,会展示在收款用户的收支详情中。
测试流程(声明:本demo仅做参考测试使用)
1.下载sdk,导入运行环境:[url]https://docs.open.alipay.com/54/103419[/url]
2.在Config文件中配置基本信息,
我这边配置的是沙箱环境下的,如果要测试正式环境的记得修改支付宝网关( serverUrl)去掉“dev”
using System;using System.Collections.Generic;using System.IO;//using System.Linq;using System.Web; /// /// 基础配置类/// namespace Com.Alipay{ public class Config { 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/G8qmHJ3rxvANbEHf3UVTa/H9QcUvJIwyA5Ywsyj6GMqGqHiTISSMa1fIC+e6BCF+z265bxS6ZWgu4P1wIDAQABAoIBACOJv0hXHgurZD7M0g2PJyqDY/01muLv+7LR9vg/MtA1k6eQrUttNsNkQptxa+IVK/CmdHSJ7KO+ZzwpnlUbqVj2jjC2UXU+Q+gnIkWuP7rmR2VwKLqh6MsDKn/tQLCzSViGW0FDrAltTwe5gL5S0/h/THdtbTr+jFh90BnsMG8rRHK4tGJM66EA7KNz9PMXWQ6V7a5oN+LS56zYcPoJfCzraZG3dEjqxgLKEwBNb020i7U0djwFu0UGZMiltWp7KLs8HbmphTIU3Pg03ONDgyeW+cZcEw615+vNRO1E1+ycrWEz5o6yhR3Hl/YUN78UpJtHxEO7F6BdKTgS/uwJDiECgYEA9NKgsQklRPMdl2/7IWshgSwrpqiMx7R+RG+Nls/ADgh9vzbPR2oc1wiXyloRM1j5bWHQ9+MZbuE/uq+5DfYTTVez3R0VjDDchZVKyfM0s0nLtdynUAsfa/ibMw/FUUbYTR7Vg3r4/N6d6m8K+qzRcOi+58o8Y0ROEWgOeoCnUK8CgYEA6Trzs+iWg/eNaou4xcZ2zmYdjnUaw1B5AhvAseNngwPN2ggoyyc54yXS17BdhzjdmA3Fzp9qR8kTpLOmvqWWd5z91Tl+UdOkLj61uXKhd5MTu/r9uQ1TyOBd1Vs/tpm1ObsZaliEU2YpH5782XlUHBoBYd3pLSOpVQwKOng/7VkCgYABMvrogxP0p4AwmrLPMhgi6tV35oFyBPdNXIKEB5Z4OyBqjFktj15+yyL7Mb+90aS/HWiJnkokmePXBT9Ryy/DBDf0UE0nt6GM4fNQ0+khK65cwKztHnDT9Y7FDEG6KS+sCQegsjx/+JMrb8nM+oW4GNu+hOYQ0mfTe//+J50qLwKBgD/SoAnlZfrq7Unwvjlw2+YTT5dCOHamONb43TxQrGzPx68YeIFPVKnyGcZoPs/YIduQOUcXnK/UOInjl4eoLIrC7TtCJm0h61W0V2pOEh10o/vW7UOMgBcCPus5sy+21D7eElBltKBV3n8GtM6WQwwFmcc8QKjXj2j0iIADlArRAoGBAPM8kqdFHQUEDhCbk1CZR4H/hStI1DERFQYvQkIGyvIhf4Q5gN+AeM9CQpFhLBqI4kt0u9mSQV8a2fEaMCRqi7jWR1AA/nhM46AYWTiH59kdAaNlAGQHuqcBEc6jhBFS8gRMRFd/qpA8CHsMeJYIRQkKcITukjVo+KaUoF4e1wLL"; //应用ID public static string appId = "2016101800718925"; public static string format = "json"; //支付宝网关 public static string serverUrl = "https://openapi.alipaydev.com/gateway.do"; //编码,无需修改 public static string charset = "utf-8"; //签名类型,支持RSA2(推荐!)、RSA public static string sign_type = "RSA2"; //版本号,无需修改 public static string version = "1.0"; /// /// 私钥文件类型转换成纯文本类型 /// /// 过滤后的字符串类型私钥 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; } }}
正式环境测试配置可参照RSA密钥生成,支付应用创建:[url]https://openclub.alipay.com/read.php?tid=1606&fid=25[/url]
3.测试接口 当收款方类型PayeeType=ALIPAY_USERID:pid,PayeeAccount=pid(以2088开头的16位纯数字)
当收款方类型PayeeType=ALIPAY_LOGONID,PayeeAccount=支付宝账号
using Aop.Api;using Aop.Api.Request;using Aop.Api.Response;using Com.Alipay;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace AlipayFundTransToaccountTransfer{ class Program { static void Main(string[] args) { IAopClient client = new DefaultAopClient(Config.serverUrl, Config.appId, Config.merchant_private_key, Config.format, Config.version, Config.sign_type, Config.alipay_public_key, Config.charset, false); AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest(); request.BizContent = "{" + "\"out_biz_no\":\"201708173142321421343112\"," + "\"payee_type\":\"ALIPAY_LOGONID\"," + "\"payee_account\":\"wmrefm7468@sandbox.com\"," + "\"amount\":\"1.23\"," + "\"payer_show_name\":\"上海交通卡退款\"," + "\"remark\":\"转账备注\"" + " }"; AlipayFundTransToaccountTransferResponse response = client.Execute(request); Console.WriteLine(response.Body); } }}
运行返回结果:
{"alipay_fund_trans_toaccount_transfer_response":{"code":"10000","msg":"Success","order_id":"20170817110070001500580000006011","out_biz_no":"201708173142321421343112","pay_date":"2017-08-17 10:43:04"},"sign":"DZgSEvtm9FbtPxzl6LsHtl1l5R/bY0pgUZyTh5sbJsHNFuHF9dKnPNId4CRaaig0U/ANpSPbtshVsv4sXJcLvfFhDRoix0kqiIqBSeEKSX11iSIZwN8tzQ8loqe1QsXYrLVBGPcwlbULZRq9NEfGnU46aJ22NhrZNdO3FzjeYn1cBfqMsFCVfyDanmmV6aW+SYF3f8cdrpH5FE+KvJpdf9W329cf49WoxK955dRwi891ayTA1AZabehFk00A0eo+jRZaDXpdZ0bc6D1bskyLKqG4iFD691pkbakbtXJRF115Z50c33hckjhDVlTcB5kRQIDCrSO8Q7PEkRE/JvID2g=="}{"alipay_fund_trans_toaccount_transfer_response":{"code":"10000","msg":"Success","order_id":"20170817110070001500580000006011","out_biz_no":"201708173142321421343112","pay_date":"2017-08-17 10:43:04"},"sign":"DZgSEvtm9FbtPxzl6LsHtl1l5R/bY0pgUZyTh5sbJsHNFuHF9dKnPNId4CRaaig0U/ANpSPbtshVsv4sXJcLvfFhDRoix0kqiIqBSeEKSX11iSIZwN8tzQ8loqe1QsXYrLVBGPcwlbULZRq9NEfGnU46aJ22NhrZNdO3FzjeYn1cBfqMsFCVfyDanmmV6aW+SYF3f8cdrpH5FE+KvJpdf9W329cf49WoxK955dRwi891ayTA1AZabehFk00A0eo+jRZaDXpdZ0bc6D1bskyLKqG4iFD691pkbakbtXJRF115Z50c33hckjhDVlTcB5kRQIDCrSO8Q7PEkRE/JvID2g=="}
如果你传入的手机号码对应多个支付宝账号,则会报该"手机号对应多个支付宝账户,请传入收款方姓名确定正确的收款账号"
{"alipay_fund_trans_toaccount_transfer_response":{"code":"40004","msg":"Business Failed","sub_code":"PAYEE_ACC_OCUPIED","sub_msg":"该手机号对应多个支付宝账户,请传入收款方姓名确定正确的收款账号","out_biz_no":"20170810154232142343211"},"sign":"WohzE7+1Q6lYUS6HrEE/KkT6ZMV7sPzLt2Z1m8oh/YST17x1sYx7sQ437yVVDLIAGgSA20lhcMPrKiv/QIrMh48kxuimisXZNbemAcG1OXP5Lp+OW3c+uicBzHd7zJguws1SpzsDpfTBBeoXS4j1rcHiG98hdYUiIKQHUzr2kVKNinKqywOrZm4UtiE6YCwxWcYQ3MECuc+4aDZ4MZksmHkrLklNeHOxUbxxOee8NDsuqSsuR0UkaChpyf7kao1bUJ7U5WdM1kLsCfsPg711KaM4DAlV0/U5LIzTNEHUKrftCPiUukZ/Tm648zZasDErV6sGBSpgT8Gcj0zPywlRuw=="}
当报该错时,可以传入payee_real_name收款方真实姓名(最长支持100个英文/50个汉字)进行支付,前提是两个账号真实姓名不一样,如果一样则只能传邮箱号,或者PayeeType=ALIPAY_USERID,PayeeAccount=pid进行解决