bourse stock
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
8.7 KiB

<?php
namespace app\home\service;
use app\model\RechargeApplyModel;
use app\model\UserWithdrawalModel;
use think\facade\Log;
/**
* 处理团队佣金业务
*/
class NicePayService extends BaseHomeService
{
public $key="5cc9479f7e218232fc45e5de6c93a158";
public $app_key="MCH12292";
public $p_type=['UPI','IMPS'];//945
public $pay_url="http://merchant.nicepay.pro/api/recharge";//代收
public $app_url="http://merchant.nicepay.pro/api/withdraw";//代收
public $bla_url="http://merchant.nicepay.pro/api/balance";//代收
public function create_order($orderId,$amount)
{
$notify_url=env('PAY.NOTIFY_URL');
$post_data['app_key']=$this->app_key;
$post_data['balance']=$amount;
$post_data['ord_id']=$orderId;
$post_data['notify_url']=$notify_url.url('nicepay_notify');
$randomKey = array_rand($this->p_type);
$post_data['p_type']=$this->p_type[$randomKey];
$sign=$this->createSign($this->key,$post_data);
$post_data['sign']=$sign;
//$header = array("Content-Type:application/json;");
//$res=$this->curlPost($this->pay_url,$post_data,10,$header);
$res=$this->fetch_page_json($this->pay_url,$post_data);
Log::info('收到nicepay响应:'.$res."----".json_encode($post_data));
$result=json_decode($res,true);
return $result;
}
public function NicePayNotify($data){
Log::info('收到nicepay异步回调:'.json_encode($data));
if($this->check_sign($data,$this->key)){
if($data['status']==1){
$order_info=RechargeApplyModel::getOrderByNo([
'order_no'=>$data['order']
]);
if($order_info && $data['amount']==$order_info['total_amount']){
if($order_info['status']==0){
return (new PayService())->dealPayNotify($order_info);
}
}else{
Log::info('nicepay支付订单不存在:'.json_encode($data));
}
}else{
Log::info('nicepay支付订单支付失败:'.json_encode($data));
}
}else{
Log::info('nicepay签名校验失败:'.json_encode($data));
}
}
public function apply_pay($orderId,$amount,$bank_account,$nike_name,$ifsc)
{
$notify_url=env('PAY.NOTIFY_URL');
$post_data['app_key']=$this->app_key;
$post_data['balance']=round($amount,2);
$post_data['ord_id']=$orderId;
$post_data['card']=$bank_account;
$post_data['name']=$nike_name;
$randomKey = array_rand($this->p_type);
$post_data['p_type']=$this->p_type[$randomKey];
$post_data['ifsc']=$ifsc;
$post_data['notify_url']=$notify_url.'/bs/nicepay_anotify';
$sign=$this->createSign($this->key,$post_data);
$post_data['sign']=$sign;
Log::info('nicepay post:'.json_encode($post_data));
$header = array("Content-Type:application/json;charset=UTF-8");
//$res=$this->curlPost($this->app_url,$post_data,10,$header);
$res=$this->fetch_page_json($this->app_url,$post_data);
$result=json_decode($res,true);
Log::info('nicepay post结果:'.$res);
if($result['err']==0 && !empty($result)){
return [
'code'=>200,
'msg'=>'ok',
'order_idx'=>'',
'content'=>$res
];
}else{
return [
'code'=>300,
'msg'=>$res,
'order_idx'=>'',
'content'=>$res
];
}
}
public function NicePayApplyNotify($data){
Log::info('收到nicepay代付异步回调:'.json_encode($data));
if($this->check_sign($data,$this->key)){
if($data['status']==1){
$order_info=UserWithdrawalModel::getUserDrawalInfo([
'order_no'=>$data['order']
]);
if($order_info && $order_info['status']==3){
UserWithdrawalModel::where([
'id'=>$order_info['id']
])->update([
'status'=>4,
'deal_time'=>date('Y-m-d H:i:s')
]);
Log::info('nicepay代付支付成功:'.json_encode($data));
}else{
Log::info('nicepay代付订单不存在:'.json_encode($data));
}
}else{
UserWithdrawalModel::where([
'order_no'=>$data['order']
])->update([
'status'=>1,
]);
Log::info('nicepay代付失败:'.json_encode($data));
}
}else{
Log::info('nicepay代付签名校验失败:'.json_encode($data));
}
}
public function getBalance()
{
$post_data['app_key']=$this->app_key;
$sign=$this->createSign($this->key,$post_data);
$post_data['sign']=$sign;
// $header = array("Content-Type:application/json;charset=UTF-8");
// $res=$this->curlPost($this->bla_url,$post_data,10,$header);
$res=$this->fetch_page_json($this->app_url,$post_data);
$result=json_decode($res,true);
if($result['err']==0 && !empty($result)){
return [
'amount'=>$result['balance']
];
}else{
return [
'amount'=>0
];
}
}
function fetch_page_json ($url, $params = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type:application/json;charset=UTF-8"]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
$errno = curl_errno($ch);
$errmsg = curl_error($ch);
if ($errno != 0)
{
throw new Exception($errmsg, $errno);
}
curl_close($ch);
return $result;
}
private function check_sign($param,$salt){
$data = $param;
$sign = $data['sign'];
if(!$sign){
return false;
}
unset($data['sign']);
ksort($data);
$str="";
foreach ($data as $key => $value)
{
$str=$str.$value;
}
$str = $str."".$salt;
$check_sign = md5($str);
if($sign != $check_sign){
return false;
}else{
return true;
}
}
private function curlPost($url, $post_data = array(), $timeout = 5, $header = "", $data_type = "") {
$header = empty($header) ? '' : $header;
//支持json数据数据提交
if($data_type == 'json'){
$post_string = json_encode($post_data);
}elseif($data_type == 'array') {
$post_string = $post_data;
}elseif(is_array($post_data)){
$post_string = http_build_query($post_data, '', '&');
}
$ch = curl_init(); // 启动一个CURL会话
curl_setopt($ch, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查 // https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
//curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的Post请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); // Post提交的数据包
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // 设置超时限制防止死循环
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
//curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模拟的header头
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* 创建签名
* @param $Md5key
* @param $list
* @return string
*/
protected function createSign($salt, $param)
{
$data = $param;
ksort($data);
$str="";
foreach ($data as $key => $value)
{
$str=$str.$value;
}
$str = $str.$salt;
return md5($str);
}
}