<?php
namespace Pay;
use think\Config;
vendor ('Pay.alipay.pagepay.service.AlipayTradeService');
vendor ('Pay.alipay.pagepay.buildermodel.AlipayTradePagePayContentBuilder');
vendor ('Pay.wxpay.lib.WxPay#Api');
vendor ('Pay.wxpay.example.WxPay#NativePay');
vendor ('Pay.wxpay.example.WxPay#JsApiPay');
vendor ('Pay.wxpay.lib.WxPay#Api#php');
vendor ('Pay.wxpay.lib.WxPay#Notify#php');
vendor ('Pay.alipay.aop.AopClient');
class pay{
//全局阿里配置参数
public $alipay_config = null;
public $weixin_config = null;
public function __construct()
{
$this->alipay_config = Config::get('alipay_config');
$this->weixin_config = Config::get ('weixin_config');
}
/**
* 电脑手机支付宝支付类
* @author lsw
* @param $trade_no 订单参数
* @param $name 订单名称
* @param $price 订单金钱
* @param $body 内容
* @param $return_url 同步回调地址
* @param $notify_url 异步回调地址
* @access public Alipay
* @throws \Exception
*/
public function Alipay($trade_no,$name,$price,$body='')
{
//商户订单号,商户网站订单系统中唯一订单号,必填
$out_trade_no = trim($trade_no);
//订单名称,必填
$subject = trim($name);
//付款金额,必填
$total_amount = trim($price);
//商品描述,可空
$body = trim($body);
//构造参数
$payRequestBuilder = new \AlipayTradePagePayContentBuilder();
$payRequestBuilder->setBody($body);
$payRequestBuilder->setSubject($subject);
$payRequestBuilder->setTotalAmount($total_amount);
$payRequestBuilder->setOutTradeNo($out_trade_no);
$aop = new \AlipayTradeService($this->alipay_config);
/**
* pagePay 电脑网站支付请求
* @param $builder 业务参数,使用buildmodel中的对象生成。
* @param $return_url 同步跳转地址,公网可以访问
* @param $notify_url 异步通知地址,公网可以访问
* @return $response 支付宝返回的信息
*/
$aop->pagePay($payRequestBuilder,$this->alipay_config['return_url'],$this->alipay_config['notify_url']);
echo '支付跳转中';
}
/**
* jsapi支付接口
* @author lsw
* @param $trade_no
* @param $name
* @param $money
* @param $body
* @access public WxJspay
* @return mixed
* @throws \WxPayException
*/
public function WxJspay($trade_no,$name,$money,$body)
{
//①、获取用户openid
$tools = new \JsApiPay();
$openId = $tools->GetOpenid();
//②、统一下单
$input = new \WxPayUnifiedOrder();
$input->SetBody($name);
$input->SetAttach($body);
$input->SetOut_trade_no($trade_no);
$input->SetTotal_fee($money);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetNotify_url( $this->weixin_config['notify_url']);
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = \WxPayApi::unifiedOrder($input);
$data['jsApiParameters'] = $tools->GetJsApiParameters($order);
$data['editAddress'] = $tools->GetEditAddressParameters();
return $data;
}
/**
* 微信二维码码支付
* @author lsw
* @param $trade_no 订单号
* @param $name 名称
* @param $body 备注
* @param $money 钱
* @access public Wxpay
* @return string
*/
public function Wxpay($trade_no,$name,$money,$body)
{
//模式一
$notify = new \NativePay();
$input = new \WxPayUnifiedOrder();
$input->SetBody($name);
$input->SetAttach($body);
$input->SetOut_trade_no($trade_no);
$input->SetTotal_fee($money);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetNotify_url($this->weixin_config['notify_url']);
$input->SetTrade_type("NATIVE");
$input->SetProduct_id("123456789");
$result = $notify->GetPayUrl($input);
return 'http://paysdk.weixin.qq.com/example/qrcode.php?data='.urlencode($result["code_url"]);
}
/**
* 支付宝支付成功验证
* @author lsw
* @param $arr
* @access public check
* @return bool
* @throws \Exception
*/
public function check($arr)
{
$alipaySevice = new \AlipayTradeService($this->alipay_config);
return $alipaySevice->check($arr);
}
public function Queryorder($transaction_id)
{
$input = new \WxPayOrderQuery();
$input->SetTransaction_id($transaction_id);
$result = \WxPayApi::orderQuery($input);
if(array_key_exists("return_code", $result) && array_key_exists("result_code", $result) && $result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS")
{
return $result;
}
return false;
}
/**
* 微信验证订单完整性
* @author lsw
* @param $data
* @access public NotifyProcess
* @return bool
*/
public function NotifyProcess($data)
{
if(!array_key_exists("transaction_id", $data)){
echo "输入参数不正确";
return false;
}
//查询订单,判断订单真实性
if($result = $this->Queryorder($data->transaction_id)){
return $result;
}else{
return FALSE;
}
}
/**
* 微信返回app参数
* @author lsw
* @param $order_sn
* @param $name
* @param $body
* @param $price
* @param string $openId
* @param string $product_id
* @access public unifiedorder
* @return array
* @throws \WxPayException
*/
public function unifiedorder($order_sn,$name,$body,$price,$openId='', $product_id='')
{
//统一下单
$input = new \WxPayUnifiedOrder();
$input->SetBody($name);
$input->SetAttach($body);
$input->SetOut_trade_no($order_sn);
$input->SetTotal_fee(($price*100));
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetNotify_url($this->weixin_config['notify_url']);
$input->SetTrade_type("APP");
$input->SetOpenid($openId);
$input->SetProduct_id($product_id);
$result = \WxPayApi::unifiedOrder($input);
//二次加密
$resultModel = new \WxPayUnifiedOrder();
$resultModel->values = [
'appid' =>$result['appid'],
'partnerid' =>$result['mch_id'],
'package' =>'Sign=WXPay',
'prepayid' =>$result['prepay_id'],
'noncestr' =>$result['nonce_str'],
'timestamp' =>time (),
];
$data = [
'sign'=>$resultModel->MakeSign (),
'order_sn'=>$order_sn,
'trade_type'=>'APP',
'appid'=>$result['appid'],
'mch_id'=>$result['mch_id'],
'package'=>'Sign=WXPay',
'prepay_id'=>$result['prepay_id'],
'nonce_str'=>$result['nonce_str'],
'timestamp'=>$resultModel->values['timestamp'],
];
return $data;
}
public function json($arr) {
$str = json_encode($arr);
$search = "#\\\u([0-9a-f]+)#ie";
$replace = "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))";
return preg_replace($search, $replace, $str);
}
/**
* 支付宝返回app参数
* @author lsw
* @param $name
* @param $order_sn
* @param $price
* @access public return_alipay_app
* @return string
*/
public function return_alipay_app($name,$order_sn,$price)
{
$private_path = Config::get ('alipay_config.merchant_private_key');//私钥路径
//构造业务请求参数的集合(订单信息)
$content = array();
$content['subject'] = $name;
$content['out_trade_no'] = $order_sn;
// $content['timeout_express'] = "".$time."";
$content['total_amount'] = $price;
$content['product_code'] = "QUICK_MSECURITY_PAY";//销售产品码,固定值
$con = json_encode($content, JSON_UNESCAPED_UNICODE);//$content是biz_content的值,将之转化成json字符串
//公共参数
$Client = new \AopClient();//实例化支付宝sdk里面的AopClient类,下单时需要的操作,都在这个类里面
$param['app_id'] = Config::get ('alipay_config.app_id');
$param['method'] = 'alipay.trade.app.pay';//接口名称,固定值
$param['charset'] = 'utf-8';//请求使用的编码格式
$param['sign_type'] = 'RSA2';//商户生成签名字符串所使用的签名算法类型
$param['format'] = 'JSON';//商户生成签名字符串所使用的签名算法类型
$param['timestamp'] = date("Y-m-d H:i:s");//发送请求的时间
$param['version'] = '1.0';//调用的接口版本,固定为:1.0
$param['notify_url'] = Config::get ('alipay_config.notify_url');
$param['biz_content'] = $con;//业务请求参数的集合,长度不限,json格式,即前面一步得到的
$paramStr = $Client->getSignContent($param);//组装请求签名参数
$sign = $Client->alonersaSign($paramStr, $private_path, 'RSA2', false);//生成签名
$param['sign'] = urlencode($sign);
$param['order_str'] = $Client->getSignContentUrlencode($param);//最终请求参数
$param['a_sign'] = $sign;
$param['a_order_str'] = urldecode ($param['order_str']);//最终请求参数
return $param;
}
}