请先在支付宝商家中心申请开通商家代扣接口
<?php
namespace app\api\controller;
use think\Controller;
class Alifreepay extends Controller
{
//获取会员信息
public function index(){
$mac_id=123;
$appid =config('config')['app_id'];
$url=config('questurl').'/api/Alifreepay/receive';
$redirect_uri = urlencode($url);
$url ='https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id='.$appid.'&scope=auth_base&state='.$mac_id.'&redirect_uri='.$redirect_uri;
// dump($url);exit;
$this->redirect($url);
}
//支付宝跳转
public function receive(){
header("Content-type:text/html;charset=utf-8");
//引入的SDK
vendor('alipayclub.AopSdk');
vendor('alipayclub.aop.AopClient');
vendor('alipayclub.aop.request.AlipaySystemOauthTokenRequest');
vendor('alipayclub.aop.request.AlipayUserUserinfoShareRequest');
$code = $_GET['auth_code'];
$_SESSION['mac_id']=$_GET['state'];
//APPID
$appid = config('config')['app_id'];
//私钥 文件名(rsa_private_key.pem)
$rsaPrivateKey = config('config')['merchant_private_key'];
//公钥 文件名 (rsa_public_key.pem)
$alipayrsaPublicKey =config('config')['alipay_public_key'];
//初始化
$aop = new \AopClient();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = $appid;
$aop->rsaPrivateKey = $rsaPrivateKey;
$aop->alipayrsaPublicKey = $alipayrsaPublicKey;
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='UTF-8';
$aop->format='json';
//获取access_token
$request = new \AlipaySystemOauthTokenRequest ();
$request->setGrantType("authorization_code");
$request->setCode($code);//这里传入 code
$result = $aop->execute($request);
$user_id=$result->alipay_system_oauth_token_response->user_id;
var_dump($user_id);exit;
}
//查看是否签约
public function check_signagreement(){
$user_id='2088802809584550';
$external_sign_no='1609918511';
// $url='*******/zhima/query/query?user_id='.$user_id.'&external_sign_no='.$external_sign_no;
$param['service']='alipay.dut.customer.agreement.query';
$param['partner']=config('config')['partner'];
$param['product_code']='GENERAL_WITHHOLDING_P';
$param['alipay_user_id']=$user_id;
$param['scene']='INDUSTRY|ALIPAYGO';
$param['appid']=config('config')['app_id'];
$param['external_sign_no']=$external_sign_no;
$key=config('config')['md5key'];
$sign=SinParams($param,$key);
// $url='https://mapi.alipay.com/gateway.do?partner=2088121753340152&product_code=GENERAL_WITHHOLDING_P&service='.$param['service'].'&sign='.$sign.'&sign_type=MD5';
$url='https://mapi.alipay.com/gateway.do?sign='.$sign.'&scene='.$param['scene'].'&external_sign_no='.$param['external_sign_no'].'&appid='.$param['appid'].'&product_code='.$param['product_code'].'&sign_type=MD5&service='.$param['service'].'&partner='.$param['partner'].'&alipay_user_id='.$param['alipay_user_id']; // var_dump($url);
$res=http_get($url);
//xml转数组
$values = json_decode(json_encode(simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
dump($values);
//array(5) { ["is_success"] => string(1) "T" ["request"] => array(1) { ["param"] => array(9) { [0] => string(16) "2088831484036885" [1] => string(35) "alipay.dut.customer.agreement.query" [2] => string(16) "2021002117666484" [3] => string(32) "410130066d15b20bcc2cc1461246a125" [4] => string(16) "2088802809584550" [5] => string(21) "GENERAL_WITHHOLDING_P" [6] => string(10) "1609918511" [7] => string(3) "MD5" [8] => string(17) "INDUSTRY|ALIPAYGO" } } ["response"] => array(1) { ["userAgreementInfo"] => array(14) { ["agreement_detail"] => string(2) "{}" ["agreement_no"] => string(20) "20214906678966429555" ["external_sign_no"] => string(10) "1609918511" ["invalid_time"] => string(19) "2115-02-01 00:00:00" ["pricipal_type"] => string(4) "CARD" ["principal_id"] => string(16) "2088802809584550" ["product_code"] => string(21) "GENERAL_WITHHOLDING_P" ["scene"] => string(17) "INDUSTRY|ALIPAYGO" ["sign_modify_time"] => string(19) "2021-01-06 15:35:52" ["sign_time"] => string(19) "2021-01-06 15:35:52" ["status"] => string(6) "NORMAL" ["thirdpart_id"] => string(16) "2088831484036885" ["thirdpart_type"] => string(7) "PARTNER" ["valid_time"] => string(19) "2021-01-06 15:35:52" } } ["sign"] => string(32) "92d0b9320c3fef3339269b931c9a5ab9" ["sign_type"] => string(3) "MD5"
}
//进行签约
public function signagreement(){
//$param['external_sign_no']='2225'; 我的签约号
$param['access_info']='{"channel":"ALIPAYAPP"}';
$param['external_sign_no']=time();
//$param['external_user_id']='0002';
$param['partner']=config('config')['partner'];
$param['product_code']='GENERAL_WITHHOLDING_P';
//$param['return_url']='*******/zhima/sn/return_url';
$param['scene']='INDUSTRY|ALIPAYGO';
$param['service']='alipay.dut.customer.agreement.page.sign';
$param['notify_url']=config('questurl').'/api/Alifreepay/notify';
$key=config('config')['md5key'];
$sign=SinParams($param,$key);
$url='https://mapi.alipay.com/gateway.do?access_info={"channel":"ALIPAYAPP"}&partner='.$param['partner'].'&product_code=GENERAL_WITHHOLDING_P&scene=INDUSTRY|ALIPAYGO&external_sign_no='.$param['external_sign_no'].'¬ify_url='.$param['notify_url'].'&service='.$param['service'].'&sign='.$sign.'&sign_type=MD5';
//dump($url);
$this->redirect($url);
}
//异步回调
public function notify(){
// sign_modify_time=2021-01-06+15%3A35%3A52¬ify_time=2021-01-06+15%3A35%3A52&alipay_user_id=2088802809584550&sign=2babd0ad96cad276ca6629d5334089b9&product_code=GENERAL_WITHHOLDING_P&sign_time=2021-01-06+15%3A35%3A52¬ify_id=2021010600222153552048741410623441&scene=INDUSTRY%7CALIPAYGO¬ify_type=dut_user_sign&partner_id=2088831484036885&agreement_no=20214906678966429555&invalid_time=2115-02-01+00%3A00%3A00&valid_time=2021-01-06+15%3A35%3A52&external_sign_no=1609918511&sign_type=MD5&status=NORMAL
$da=file_get_contents("php://input");
file_put_contents('./zhima_notify1.txt',json_encode($da));
if($da){
$data['partner_id']=$da['partner_id'];
$data['agreement_no']=$da['agreement_no']; //支付发送此参数
$data['status']=$da['status'];
$data['alipay_user_id']=$da['alipay_user_id'];
$data['external_sign_no']=$da['external_sign_no'];//签约协议号
$data['sign_time']=time();
echo 'success';
exit;
}
}
//进行扣款
public function pay(){
$agreement_no='20214906678966429555';
$param['service']='alipay.acquire.createandpay';
$param['partner']=config('config')['partner'];
$param['_input_charset']='utf-8';
$param['out_trade_no']=time();
$param['subject']='测试';
$param['notify_url']=config('questurl').'/api/alifreepay/paynotify';
$param['product_code']='GENERAL_WITHHOLDING';
// $param['product_code']='BARCODE_CODE_OFFLINE';
$param['total_fee']='0.01';
$param['agreement_info']='{"agreement_no":'.$agreement_no.'}';
//$param['terminal_id']='xnbf';
$key=config('config')['md5key'];
$sign=SinParams($param,$key);
$url='https://mapi.alipay.com/gateway.do?partner='.$param['partner'].'&product_code='.$param['product_code'].'&_input_charset=utf-8¬ify_url='.$param['notify_url'].'&out_trade_no='.time().'&agreement_info='.$param['agreement_info'].'&subject=测试&total_fee='.$param['total_fee'].'&service='.$param['service'].'&sign='.$sign.'&sign_type=MD5';
$res=http_get($url);
$values = json_decode(json_encode(simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
dump($values);
// array(5) {
// ["is_success"] => string(1) "T"
// ["request"] => array(1) {
// ["param"] => array(11) {
// [0] => string(10) "1609920568"
// [1] => string(16) "2088831484036885"
// [2] => string(37) "{"agreement_no":20214906678966429555}"
// [3] => string(5) "utf-8"
// [4] => string(6) "测试"
// [5] => string(27) "alipay.acquire.createandpay"
// [6] => string(4) "0.01"
// [7] => string(32) "c9393efb02b66790b70abcdeed1cc5c4"
// [8] => string(19) "GENERAL_WITHHOLDING"
// [9] => string(49) "********/api/alifreepay/paynotify"
// [10] => string(3) "MD5"
// }
// }
// ["response"] => array(1) {
// ["alipay"] => array(7) {
// ["buyer_logon_id"] => string(13) "734***@qq.com"
// ["buyer_user_id"] => string(16) "2088802809584550"
// ["gmt_payment"] => string(19) "2021-01-06 16:09:29"
// ["out_trade_no"] => string(10) "1609920568"
// ["result_code"] => string(25) "ORDER_SUCCESS_PAY_SUCCESS"
// ["total_fee"] => string(4) "0.01"
// ["trade_no"] => string(28) "2021010622001484551455234449"
// }
// }
// ["sign"] => string(32) "23c901a8408fcdff8f6fca06b8a6a96c"
// ["sign_type"] => string(3) "MD5"
// }
}
}