📖 File Reader
<?php
namespace app\common;
use app\BaseController;
use app\api\common\Common_config;
use think\facade\Db;
use think\facade\View;
/*支付宝支付处理*/
class Common_alipay extends BaseController
{
protected $app_id = '';//apiid;
protected $notify_url = '';//回调地址
protected $private_key = '';//私钥
protected $gatewayUrl = 'https://openapi.alipay.com/gateway.do';//支付宝api地址
protected $public_key = '';//公钥
public function initialize()
{
$config = Common_config::config("'alipay_app_id','private_key','public_key'");
$this->app_id = $config["alipay_app_id"];
$this->notify_url = 'http://'.$_SERVER["SERVER_NAME"].'/api/Order/alipay_notify_url';
$this->private_key = $config["private_key"];
$this->public_key = $config["public_key"];
}
/**
* 支付宝支付(APP)
* @param $user_miyao 会员秘钥
* @param $order_sn 订单编号
* @param $goods_name 商品名称
* @param $price 订单总价格
*/
public function pay_app($user_miyao,$order_sn,$goods_name,$price)
{
header("Content-type:text/html;charset=utf-8");
require_once 'extend/alipay/aop/AopClient.php';
require_once 'extend/alipay/aop/request/AlipayFundTransToaccountTransferRequest.php';
require_once 'extend/alipay/aop/request/AlipayFundTransOrderQueryRequest.php';
require_once 'extend/alipay/aop/SignData.php';
//require_once 'extend/alipay/aop/request/AlipayTradePagePayRequest.php';
require_once 'extend/alipay/aop/request/AlipayTradeAppPayRequest.php';
$aop = new \AopClient();
$aop->gatewayUrl = $this->gatewayUrl;
$aop->appId = $this->app_id;
$aop->method = 'alipay.trade.app.pay';
$aop->charset = 'utf-8';
$aop->signType = 'RSA2';
$aop->format = 'json';
$aop->alipayrsaPublicKey = $this->public_key;//公钥
$aop->rsaPrivateKey = $this->private_key;//私钥
$biz_content = json_encode([
'out_trade_no'=> $order_sn,
'product_code'=> 'QUICK_MSECURITY_PAY',
'total_amount'=> $price,
'subject'=> $goods_name,
'timeout_express'=>'30m',
'body'=>'订单支付'
]);
$payRequest= new \AlipayTradeAppPayRequest();
$payRequest->setNotifyUrl($this->notify_url."/user_miyao/".$user_miyao.'/order_sn/'.$order_sn);
$payRequest->setBizContent($biz_content);
$response = $aop->sdkExecute($payRequest);
return $this->succeed_json("ok",$response,202);
}
/**
* 支付宝支付(H5)
* @param $user_miyao 会员秘钥
* @param $order_sn 订单编号
* @param $goods_name 商品名称
* @param $price 订单总价格
*/
public function pay_h5($user_miyao,$order_sn,$goods_name,$price)
{
header("Content-type:text/html;charset=utf-8");
require_once 'extend/alipay/aop/AopClient.php';
require_once 'extend/alipay/aop/request/AlipayFundTransToaccountTransferRequest.php';
require_once 'extend/alipay/aop/request/AlipayFundTransOrderQueryRequest.php';
require_once 'extend/alipay/aop/SignData.php';
require_once 'extend/alipay/aop/request/AlipayTradePrecreateRequest.php';
$aop = new \AopClient();
$aop->gatewayUrl = $this->gatewayUrl;
$aop->appId = $this->app_id;
$aop->method = 'alipayTradePagePay';
$aop->charset = 'utf-8';
$aop->signType = 'RSA2';
$aop->format = 'json';
$aop->alipayrsaPublicKey = $this->public_key;//公钥
$aop->rsaPrivateKey = $this->private_key;//私钥
$biz_content = json_encode([
'out_trade_no'=> $order_sn,
'product_code'=> 'QUICK_MSECURITY_PAY',
'total_amount'=> $price,
'subject'=> $goods_name,
'timeout_express'=>'30m',
'body'=>'订单支付'
]);
$payRequest = new \AlipayTradePrecreateRequest();
$payRequest->setNotifyUrl($this->notify_url."/user_miyao/".$user_miyao.'/order_sn/'.$order_sn);
$payRequest->setBizContent($biz_content);
$response = $aop->sdkExecute($payRequest);
return json_decode(file_get_contents('https://openapi.alipay.com/gateway.do?'.$response),true);
}
/*提现*/
public function tixian($momo,$order_sn,$name,$mobile,$remark){
header("Content-type:text/html;charset=utf-8");
require_once 'extend/alipay/aop/AopClient.php';
require_once 'extend/alipay/aop/request/AlipayFundTransToaccountTransferRequest.php';
require_once 'extend/alipay/aop/request/AlipayFundTransOrderQueryRequest.php';
require_once 'extend/alipay/aop/SignData.php';
//require_once 'extend/alipay/aop/request/AlipayTradePagePayRequest.php';
require_once 'extend/alipay/aop/request/AlipayTradeRefundRequest.php';
$aop = new \AopClient();
$aop->gatewayUrl = $this->gatewayUrl;
$aop->appId = $this->app_id;
$aop->method = 'alipayTradeRefund';
$aop->charset = 'utf-8';
$aop->signType = 'RSA2';
$aop->alipayrsaPublicKey = $this->public_key;
$aop->rsaPrivateKey = $this->private_key;
/* 1商户转账唯一订单号 2收款方账户类型 3收款方账户 4转账金额 5付款方姓名 6收款方真实姓名 7转账备注 */
$biz_content = json_encode([
'out_biz_no'=> $order_sn,
'payee_type'=> 'ALIPAY_LOGONID',
'payee_account'=> $mobile,
'amount'=>$momo,
'payer_show_name'=>'客宝提现',
'payee_real_name'=>$name,
'remark'=>$remark,
'timeout_express'=>'30m',
]);
$payRequest= new \AlipayFundTransToaccountTransferRequest();
$payRequest->setBizContent($biz_content);
$response = $aop->sdkExecute($payRequest);
$response = $this->curls('https://openapi.alipay.com/gateway.do?'.$response);
//$response = json_decode(file_get_contents('https://openapi.alipay.com/gateway.do?'.$response),true);
return json_encode($response);
}
/*退款*/
public function payment_refund($money,$ordersn,$order_sn,$momo){
header("Content-type:text/html;charset=utf-8");
require_once 'extend/alipay/aop/AopClient.php';
require_once 'extend/alipay/aop/request/AlipayFundTransToaccountTransferRequest.php';
require_once 'extend/alipay/aop/request/AlipayFundTransOrderQueryRequest.php';
require_once 'extend/alipay/aop/SignData.php';
//require_once 'extend/alipay/aop/request/AlipayTradePagePayRequest.php';
require_once 'extend/alipay/aop/request/AlipayTradeRefundRequest.php';
$aop = new \AopClient();
$aop->gatewayUrl = $this->gatewayUrl;
$aop->appId = $this->app_id;
$aop->method = 'alipayTradeRefund';
$aop->charset = 'utf-8';
$aop->signType = 'RSA2';
$aop->alipayrsaPublicKey = $this->public_key;
$aop->rsaPrivateKey = $this->private_key;
$biz_content = json_encode([
'out_trade_no'=> $ordersn,
'refund_amount'=> sprintf('%.2f',$momo),
'refund_reason'=> '客宝退款',
'out_request_no'=> $order_sn,
'timeout_express'=>'30m',
]);
$payRequest= new \AlipayTradeRefundRequest();
$payRequest->setBizContent($biz_content);
$response = $aop->sdkExecute($payRequest);
$response = $this->curls('https://openapi.alipay.com/gateway.do?'.$response);
return json_encode($response);
}
public function curls($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 0);
$param = curl_exec($ch);
curl_close($ch);
return json_decode($param,true);
}
// public function notifys()
// {
// $time = input('order_sn');
// header("Content-type:text/html;charset=utf-8");
// require_once __DIR__.'/../../../extend/alipay/aop/AopClient.php';
// require_once __DIR__.'/../../../extend/alipay/aop/request/AlipayFundTransToaccountTransferRequest.php';
// require_once __DIR__.'/../../../extend/alipay/aop/request/AlipayFundTransOrderQueryRequest.php';
// require_once __DIR__.'/../../../extend/alipay/aop/SignData.php';
// //require_once __DIR__.'/../../../extend/alipay/aop/request/AlipayTradePagePayRequest.php';
// require_once __DIR__.'/../../../extend/alipay/aop/request/AlipayTradePrecreateRequest.php';
//
// require_once __DIR__.'/../../../extend/alipay/aop/request/AlipayTradeQueryRequest.php';
// $alipay_config = $this->config();
// $aop = new \AopClient();
// $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
// $aop->appId = $alipay_config['app_id'];
// $aop->method = 'alipaytradequery';
// $aop->charset = 'utf-8';
// $aop->signType = 'RSA2';
// $aop->alipayrsaPublicKey = $alipay_config['public_key'];
// $aop->rsaPrivateKey = $alipay_config['private_key'];
// $biz_content = json_encode([
// 'trade_no'=> $time
// ]);
// $payRequest= new \AlipayTradeQueryRequest();
// $payRequest->setNotifyUrl($alipay_config['notify_url']);
// $payRequest->setBizContent($biz_content);
// $response = $aop->sdkExecute($payRequest);
// //$response = $this->curls('https://openapi.alipay.com/gateway.do?'.$response);
// echo "<pre>";
// print_r($biz_content);exit;
// return $response;
// }
// public function notify($time)
// {
// header("Content-type:text/html;charset=utf-8");
// require_once __DIR__.'/../../../extend/alipay/aop/AopClient.php';
// require_once __DIR__.'/../../../extend/alipay/aop/request/AlipayFundTransToaccountTransferRequest.php';
// require_once __DIR__.'/../../../extend/alipay/aop/request/AlipayFundTransOrderQueryRequest.php';
// require_once __DIR__.'/../../../extend/alipay/aop/SignData.php';
// //require_once __DIR__.'/../../../extend/alipay/aop/request/AlipayTradePagePayRequest.php';
// require_once __DIR__.'/../../../extend/alipay/aop/request/AlipayTradePrecreateRequest.php';
//
// require_once __DIR__.'/../../../extend/alipay/aop/request/AlipayTradeQueryRequest.php';
// $alipay_config = $this->config();
// $aop = new \AopClient();
// $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
// $aop->appId = $alipay_config['app_id'];
// $aop->method = 'alipaytradequery';
// $aop->charset = 'utf-8';
// $aop->signType = 'RSA2';
// $aop->alipayrsaPublicKey = $alipay_config['public_key'];
// $aop->rsaPrivateKey = $alipay_config['private_key'];
// $biz_content = json_encode([
// 'out_trade_no'=> $time
// ]);
// $payRequest= new \AlipayTradeQueryRequest();
// $payRequest->setNotifyUrl($alipay_config['notify_url']);
// $payRequest->setBizContent($biz_content);
// $response = $aop->sdkExecute($payRequest);
//
// $response = $this->curls('https://openapi.alipay.com/gateway.do?'.$response);
// return $response;
// }
}