CONSULTING QIAN1
咨询千一
选择下列产品马上在线沟通
微信扫码咨询
咨询电话:0833-2134912
你可能遇到了下面的问题
关闭右侧工具栏
支付宝向用户付款单笔转账接口示例demo
  • 作者:千一科技
  • 发表时间:2018-06-21 09:42
  • 来源:未知

<?php
class Pay
{
    /**
    * 单笔转账(以下公钥私钥 appid 都是沙箱环境)
    * @param rsaPrivateKey         请填写开发者私钥去头去尾去回车,一行字符串'
    * @param alipayrsaPublicKey    请填写支付宝公钥,一行字符串
    * @param out_biz_no            商户转账唯一订单号
    * @param payee_type            收款方账户类型
    *                            (1、ALIPAY_USERID:支付宝账号对应的支付宝唯一用户号。以2088开头的16位纯数字组成。
    *                              2、ALIPAY_LOGONID:支付宝登录号,支持邮箱和手机号格式。)
    * @param payee_account         收款方账户
    * @param amount                转账金额,单位:元。
    * @param payer_show_name       付款方姓名
    * @param payee_real_name       收款方真实姓名
    * @param remark                转账备注
    */
    public function transferMoney($uid,$orderid,$money,$uname,$check){
        
       require_once('AopClient.php');  //这两个文件在支付宝单笔转账接口文档里下载
       require_once('AlipayFundTransToaccountTransferRequest.php');
 
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2017111609966828';
        $aop->rsaPrivateKey = 'MIICXgIBAAKBgQDdCMji20xkvK+sAI1WLpVMLquA6e1Vq/0An50GU5DrWzh4Iw38gk5mRiAqXGk4MKwJT+smcpsdf9JsghEIXyntTy6Rxl1ZrOHajtx+oW8Pn7KVkjVXgA0aMl+gOsu6OsTGC14UHQ0+Qt76qaanjNYdAEu3pjovdlkBcPcIU0u25wIDAQABAoGBAKk+Ybs6mYsE4yo7nf0gXBbiIVXrSFvx/h5tGOnQMMXVqi5rAPmYuPkctuJYsgRhWEklFXPyCw2SYwfMq8gSPeHX8QFShn+EbacovGHxL5Yl1Zpta02kVJke74aMQWGVAp0lKYW29ZOVvBkgOUFxdilEMBc1spVV2u2aLKYh4u9hAkEA9NczgjX2qGsx2fz7JciMUZ5ERdSqFdsQUSGOA2gJ1yM7+qbsmzGAtPlNRy5kSjxk7gpDurCqEd+Pf34W+JCgNwJBAOcbz8R725UTZo2QIqnJN4juketaXIQ/bYUv5tgg/MCEypiBL5NPKOtnn81/e6yh+tQD4WEvR6bIY5OqBzY1ZtECQQDhMnse/fGEUSxGG0kJJJ/jhlW3gPZ8KcSlSt6rRWsfpTnKvvQLfomqa09n9ESsf0Y6J9jKAyo6TE1Zb6lDMQsbAkAIsUoaSNJUhBtKXc5/9lqJYvfIE2kBbGVRKiETpgH7t8LrA4jnkIWofQhMsWvUbW9CxDaY8i9OvaDJMA7/YfjRAkEAls58jtwy0mCu5u6ZIMunhR2/OziExnlx61A8yUkK8HQHoTjJTJByHb197yYJ/HRO6NCcXEzSvsD2sKUQfG2MYw==';
        $aop->alipayrsaPublicKey='MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB';
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new AlipayFundTransToaccountTransferRequest();
        if($check)  $namecheck = "\"payee_real_name\":\"".$uname."\",";  //是否校验真实姓名
        else  $namecheck = '';
        $request->setBizContent("{" .
        "\"out_biz_no\":\"".$orderid."\"," .
        "\"payee_type\":\"ALIPAY_LOGONID\"," .
        "\"payee_account\":\"".$uid."\"," .
        "\"amount\":\"".$money."\"," .
        "\"payer_show_name\":\"转账方名称\"," .
        $namecheck.
        "\"remark\":\"转账备注\"" .
        "  }");
        $result = $aop->execute ( $request); 
          
        //var_export($result);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resdata = array(
            'return_code'      => $result->$responseNode->code,
            'sub_code'         => $result->$responseNode->sub_code,
            'payment_no'       => $result->$responseNode->order_id,
            'msg'              => $result->$responseNode->sub_msg,
        );
        return $resdata;
    }
 
    /**
    * 查询转账订单
    * @param out_biz_no   商户转账唯一订单号
    * @param order_id     支付宝转账单据号
    */
    public function findOrder(){
        
        Loader::import('transfer.aop.AopClient');
        Loader::import('transfer.aop.request.AlipayFundTransOrderQueryRequest');
 
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipaydev.com/gateway.do';//沙箱网关多了一个dev
        $aop->appId = '2016102700767808';
        $aop->rsaPrivateKey = 'MIICXAIBAAKBgQCgoKbSZ9R+V5YS1C6cViPQi3bMEVn/jtG3OHNwSD0XWh5y+Te/o7QIfFWanmsP7x21BvaAtDlXun8nXQ2nJzbN3siXQ2FZN0am3seyabRvay7t1RyvChPs4g3bzpxRF/WNZkxPfBVOdPzITX9Gh+1SkU1PGW+QEeQJQmmH5DKQWwIDAQABAoGAWqNsnyvxzz0zNPrANn2xBfu8lFhyNW/szPuwwDozJts2dWBF4gDJFUS7tV0RQtOGKuF79ZI9DVDycU7j5l+NX2ctWilyyvsp/xjhYFcytbjVxChyYWsjNrPcqm7cI0K6a+RkSDd2A3iyXdoBXuYINOH3pM3ZaQI7EN0m02zdJ9kCQQDLXpNWRnXvOLPNd9WPzssOn1LDesb0z57lqeeEzsRw2yp+QaHbua0aYRnUy1rh+Etg8lLe3VQSSd7Bg6bwDt3fAkEAyjJiTTlQ5/MvdcuZsv4NHZc7/oB+I1CnymaVLwefCGhWBmVC8ijB61SazVswW6MBGI1jrocxqmnqbXY43wklBQJAa5qa8qdNd0nVOP4S7YhJQG9kZzHrW1UPig0gBtEwd1Y/YZZwgcX4pWCDPmhqqe2qVJ1i47DP7u1PA60ZAyOamQJBAKu2+EbO+0fQ/ihSvXgGS0srCbdMllMVhznbvqxPE59HRG9wBde0B2P82O63l6zrFN4uDU7F38by5jUJ5qh7kqUCQCS1Ql2la6U59u+FBsL5QI2n5mTcNAr6zjcEVnZTX0l8FtX8IO68KKT7EpJvfAVYbn72OLhxI7NbYbIIZU+U+PQ=';
        $aop->alipayrsaPublicKey='MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIgHnOn7LLILlKETd6BFRJ0GqgS2Y3mn1wMQmyh9zEyWlz5p1zrahRahbXAfCfSqshSNfqOmAQzSHRVjCqjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpwIDAQAB';
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new \AlipayFundTransOrderQueryRequest ();
        $request->setBizContent("{" .
        "\"out_biz_no\":\"314232SS1423432\"," .
        "\"order_id\":\"20171010110070001500500000018477\"" .
        "  }");
 
        $result = $aop->execute ( $request); 
         
        var_export($result);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
        echo "成功";
        } else {
        echo "失败";
        }
    }
}

调用方法
$merchpay = new Pay();
$data = $merchpay->transferMoney($openid,$trade_no,$sjmoney,$uname,$check);
if(!empty($data['return_code'])&&$data['return_code'] == 10000){ 成功逻辑$data['payment_no'];}
elseif($data['sub_code']=='PAYER_BALANCE_NOT_ENOUGH'||$data['sub_code']=='EXCEED_LIMIT_DM_AMOUNT'){余额不足,超限}