* @version : HUOSDK 8.0 */ namespace huolib\withdraw\driver; use huolib\constant\SettleConst; use huolib\withdraw\Driver; use think\Loader; class Alipay extends Driver { private $config = [ 'partner' => '', 'seller_email' => '', 'key' => '', 'sign_type' => '', 'input_charset' => 'utf-8', // 编码 (固定值不用改) 'transport' => 'http', // 协议 (固定值不用改) 'cacert' => '', 'notify_url' => '', 'return_url' => '', 'show_url' => '', 'private_key_path' => '', 'public_key_path' => '', ]; /** * 构造函数 */ public function __construct() { if (file_exists(GLOBAL_CONF_PATH."extra/pay/zfb/config.php")) { $_config = include GLOBAL_CONF_PATH."extra/pay/zfb/config.php"; } else { $_config = array(); } $this->config = array_merge($this->config, $_config); $this->config['sign_type'] = strtoupper(trim('RSA')); $this->config['cacert'] = GLOBAL_CONF_PATH.'extra/pay/zfb/cacert.pem'; $this->config['private_key_path'] = GLOBAL_CONF_PATH.'extra/pay/zfb/key/rsa_private_key.pem'; $this->config['public_key_path'] = GLOBAL_CONF_PATH.'extra/pay/zfb/key/rsa_public_key.pem'; $this->notify_url = get_val($this->config, 'notify_url', ''); } /** * 支付宝提现 * https://docs.open.alipay.com/api_28/alipay.fund.trans.toaccount.transfer * https://docs.open.alipay.com/309 * * @throws \Exception */ public function withDraw() { $_re_data = []; Loader::import('withdraw.alipay.aop.AopClient', EXTEND_PATH, '.php'); Loader::import('withdraw.alipay.aop.request.AlipayFundTransToaccountTransferRequest', EXTEND_PATH, '.php'); Loader::import('withdraw.alipay.aop.SignData', EXTEND_PATH, '.php'); $_gateway_url = 'https://openapi.alipay.com/gateway.do'; $_aop = new \AopClient(); $_aop->gatewayUrl = $_gateway_url;//支付宝网关 这个是不变的 $_aop->appId = $this->config['app_id'];//商户appid 在支付宝控制台找 $_aop->rsaPrivateKey = $this->getPrivateKey($this->config['private_key_path']);//私钥 工具生成的 $_aop->alipayrsaPublicKey = $this->getPublicKey($this->config['public_key_path']);//支付宝公钥 上传应用公钥后 支付宝生成的支付宝公钥 $_aop->apiVersion = '1.0'; $_aop->signType = 'RSA'; $_aop->postCharset = 'utf-8'; $_aop->format = 'json'; $_request = new \AlipayFundTransToaccountTransferRequest(); $_request->setBizContent( "{". "\"out_biz_no\":\"$this->order_id\",". "\"payee_type\":\"ALIPAY_LOGONID\",". "\"payee_account\":\"$this->payee_account\",". "\"amount\":\"$this->real_amount\",". "\"payer_show_name\":\"$this->payer_show_name\",". "\"payee_real_name\":\"$this->payee_real_name\",". "\"remark\":\"$this->remark\"". "}" ); $_result = $_aop->execute($_request); $_response_node = str_replace(".", "_", $_request->getApiMethodName())."_response"; $_resultCode = $_result->$_response_node->code; $_re_data['result'] = json_encode($_result); $_re_data['code'] = $_resultCode; $_re_data['msg'] = $_result->$_response_node->msg; if (!empty($_resultCode) && $_resultCode == 10000) { $_re_data['code'] = SettleConst::SETTLE_SUCCESS; //发起支付查询 $_rs = $this->orderQuery('s1530961465310009494'); $_re_data['query_result'] = $_rs['result']; } else { $_re_data['msg'] = $_result->$_response_node->sub_msg; } return $_re_data; } /** * 查询是否订单是否到账 * https://docs.open.alipay.com/api_28/alipay.fund.trans.order.query * * * @param string $order_id 商户系统内部订单号 * @param null $ext 扩展信息 * * @return array * @throws \Exception */ public function orderQuery($order_id, $ext = null) { $_re_data = []; Loader::import('withdraw.alipay.aop.AopClient', EXTEND_PATH, '.php'); Loader::import('withdraw.alipay.aop.request.AlipayFundTransOrderQueryRequest', EXTEND_PATH, '.php'); Loader::import('withdraw.alipay.aop.SignData', EXTEND_PATH, '.php'); $_gateway_url = 'https://openapi.alipay.com/gateway.do'; $_aop = new \AopClient(); $_aop->gatewayUrl = $_gateway_url;//支付宝网关 这个是不变的 $_aop->appId = $this->config['app_id'];//商户appid 在支付宝控制台找 $_aop->format = 'json'; $_aop->postCharset = 'utf-8'; $_aop->signType = 'RSA'; $_aop->apiVersion = '1.0'; $_aop->rsaPrivateKey = $this->getPrivateKey($this->config['private_key_path']);//私钥 工具生成的 $_aop->alipayrsaPublicKey = $this->getPublicKey($this->config['public_key_path']);//支付宝公钥 上传应用公钥后 支付宝生成的支付宝公钥 $_request = new \AlipayFundTransOrderQueryRequest(); $_request->setBizContent( "{". "\"out_biz_no\":\"$order_id\",". "}" ); $_result = $_aop->execute($_request); $_response_node = str_replace(".", "_", $_request->getApiMethodName())."_response"; $_resultCode = $_result->$_response_node->code; $_re_data['result'] = json_encode($_result); $_re_data['code'] = $_result->$_response_node->code; $_re_data['msg'] = $_result->$_response_node->msg; if (!empty($_resultCode) && $_resultCode == 10000) { switch ($_result->$_response_node->status) { case 'SUCCESS': $_re_data['status'] = SettleConst::SETTLE_PAY_SUCCESS; break; case 'INIT': case 'DEALING': $_re_data['status'] = SettleConst::SETTLE_PAY_PROCESSING; break; default: $_re_data['status'] = SettleConst::SETTLE_PAY_FAILED; } } else { $_re_data['msg'] = $_result->$_response_node->sub_msg; } return $_re_data; } /** * 通过路径获取RSA 私钥内容 * * @param string $private_key_path 私钥路径 * * @return null|string */ public function getPrivateKey($private_key_path) { if (!file_exists($private_key_path)) { return null; } $_private_key = file_get_contents($private_key_path); $_private_key = str_replace("-----BEGIN RSA PRIVATE KEY-----", "", $_private_key); $_private_key = str_replace("-----END RSA PRIVATE KEY-----", "", $_private_key); $_private_key = str_replace("\n", "", $_private_key); $private_key = wordwrap($_private_key, 64, "\n", true); return $private_key; } /** * 通过路径获取RSA公钥内容 * * @param string $public_key_path 公钥路径 * * @return null|string */ function getPublicKey($public_key_path) { if (!file_exists($public_key_path)) { return null; } $_public_key = file_get_contents($public_key_path); //以下为了初始化私钥,保证在您填写私钥时不管是带格式还是不带格式都可以通过验证。 $_public_key = str_replace("-----BEGIN PUBLIC KEY-----", "", $_public_key); $_public_key = str_replace("-----END PUBLIC KEY-----", "", $_public_key); $_public_key = str_replace("\n", "", $_public_key); $_public_key = wordwrap($_public_key, 64, "\n", true); return $_public_key; } }