dlh_test_server/app/api/controller/Pay.php

326 lines
11 KiB
PHP
Executable File

<?php
namespace app\api\controller;
use app\api\logic\PayLogic;
use app\common\basics\Api;
use app\common\enum\OrderEnum;
use app\common\enum\PayEnum;
use app\common\model\Client_;
use app\common\model\order\OrderTrade;
use app\common\model\order\Order;
use app\common\model\order\OrderLog;
use app\common\server\AliPayServer;
use app\common\server\JsonServer;
use app\common\model\Test;
use app\common\server\WeChatPayServer;
use app\common\server\WeChatServer;
use app\common\model\integral\IntegralOrder;
use app\common\model\RechargeOrder;
require_once ROOT_PATH . "/../vendor/hf/BsPayDemo/loader.php";
require_once ROOT_PATH . "/../vendor/hf/BsPaySdk/init.php";
use BsPaySdk\core\BsPay;
use BsPaySdk\config\MerConfig;
use think\facade\Db;
use think\facade\Log;
/**
* Class Pay
* @package app\api\controller
*/
class Pay extends Api
{
public $like_not_need_login = ['huifuNotify','notifyMnp1','notifyMnp', 'notifyOa', 'notifyApp', 'aliNotify', 'localPay', 'localBalance'];
/**
* @notes 支付入口
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author suny
* @date 2021/7/13 6:13 下午
*/
public function unifiedpay()
{
$user_id = $this->user_id;
$post = $this->request->post();
if ($post['from'] == 'recharge') {
return $this->huifuCz($user_id, $post);
}
if(!isset($post['pay_way'])) {
return JsonServer::error('请选择支付方式');
}
// 信用支付
if ($post['from'] == 'repayment') {
$pay_way = $post['pay_way'];
$order['order_id'] = $user_id;
$post['order_id'] = $user_id;
}
if ($post['from'] == 'order') {
// 更新支付方式
$order = Order::findOrEmpty($post['order_id']);
$order->pay_way = $post['pay_way'];
$order->save();
$pay_way = $order['pay_way'];
}
if ($post['from'] == 'trade') {
$order = Order::where('trade_id', $post['order_id'])->findOrEmpty();
// 更新支付方式
Order::where('trade_id', $post['order_id'])->update(['pay_way' => $post['pay_way']]);
$pay_way = $post['pay_way'];
}
if ($post['from'] == 'recharge') {
return JsonServer::error('充值接口维护中');
$pay_way = isset($post['pay_way']) ? $post['pay_way'] : PayEnum::WECHAT_PAY;
}
// 积分订单
if ($post['from'] == 'integral') {
$order = IntegralOrder::findOrEmpty($post['order_id']);
IntegralOrder::where('id', $post['order_id'])->update(['pay_way' => $post['pay_way']]);
$pay_way = $post['pay_way'];
}
if( $pay_way != 5){
if($post['from'] != 'repayment'){
// order,trade方式金额为0直接走余额支付
if (isset($order) && $order['order_amount'] == 0) {
$result = PayLogic::balancePay($post['order_id'], $post['from']);
return $result;
}
}
}
if(isset($post['payment_method'])){
$payment_method = $post['payment_method'];
}else{
$payment_method = "";
}
switch ($pay_way) {
case OrderEnum::PAY_WAY_BALANCE://余额支付
$result = PayLogic::balancePay($post['order_id'], $post['from']);
break;
case OrderEnum::PAY_WAY_WECHAT://微信支付
$result = PayLogic::wechatPay($post['order_id'], $post['from'],$payment_method, $this->client);
break;
case OrderEnum::PAY_WAY_XINY://信用支付
$result = PayLogic::xinyPay($post['order_id'], $post['from']);
break;
case OrderEnum::PAY_WAY_ALIPAY://支付宝支付
$result = PayLogic::aliPay($post['order_id'], $post['from'],$this->client);
$data = [
'code' => 10001,
'msg' => '发起成功',
'data' => $result,
'show' => 0,
];
return json($data);
break;
}
return $result;
}
public function huifuCz($user_id,$post)
{
$order = RechargeOrder::where([
['id', '=', $post['order_id']]
])->find();
$user = Db::name("user_auth")->where("user_id",$user_id)->find();
// $out = "dlh".time().$user_id;
$config = new BsPay();
$merConfig = $config->getConfig();
// 2.组装请求参数
$paramsInfo = array();
// 请求日期
$paramsInfo["req_date"] = date("Ymd");
// 请求流水号
$paramsInfo["req_seq_id"] = $order['order_sn'];
// 商户号
$paramsInfo["huifu_id"] = "6666000158818881";
// 商品描述
$paramsInfo["goods_desc"] = "充值";
// 交易类型
$paramsInfo["trade_type"] = "T_MINIAPP";
// 交易金额
$paramsInfo["trans_amt"] = $order['order_amount'];
// 交易有效期
$paramsInfo["time_expire"] = "20250518235959";
// 微信参数集合
$arr = array(
"sub_appid"=>"wx586d77b9edb0636d",
"sub_openid"=>$user['openid']
);
$paramsInfo["wx_data"] = json_encode($arr,true);
// $paramsInfo["wx_data"] =
// "{\"sub_appid\":\"wx586d77b9edb0636d\",\"sub_openid\":\"ouOMm4zEGaZbvNhq9PyoYN53902c\",
// \"detail\":{\"cost_price\":\"43.00\",\"receipt_id\":\"20220628132043853798\",\"goods_detail\":
// [{\"goods_id\":\"6934572310301\",\"goods_name\":\"太龙双黄连口服液\",\"price\":\"43.00\"
// ,\"quantity\":\"1\",\"wxpay_goods_id\":\"12235413214070356458058\"}]}}";
// 是否延迟交易
$paramsInfo["delay_acct_flag"] = "N";
// // 分账对象
// $paramsInfo["acct_split_bunch"] = "{\"acct_infos\":[{\"div_amt\":\"0.10\",\"huifu_id\":\"6666000156739560\"}]}";
// 传入分账遇到优惠的处理规则
$paramsInfo["term_div_coupon_type"] = "0";
// 禁用信用卡标记
$paramsInfo["limit_pay_type"] = "NO_CREDIT";
// 场景类型
$paramsInfo["pay_scene"] = "02";
// 备注
$paramsInfo["remark"] = "string";
// 异步通知地址
$paramsInfo["notify_url"] = "https://dlh.hainings.com/api/pay/huifuNotify";
// print_r("<pre>");
// var_dump($paramsInfo);
// 3. 发起API调用
$bspay = new BsPay();
$result = $bspay->post("v2.trade.payment.jspay", $paramsInfo, '', 'default');
// var_dump($result);
$result = $this->objectToArray($result);
$rspDatas = $result['rspDatas']['data'];
return JsonServer::success('操作成功', json_decode($rspDatas['pay_info'], true));
}
public function huifuNotify(){
$result = $_REQUEST;
// Log::write($result, '回调信息');
if(isset($result['resp_code'])&&$result['resp_code']=="00000000"){
$d['pay_status'] = 1;
$d['pay_time'] = time();
$d['pay_company'] = 3;
$resp_data = json_decode($result['resp_data'],true);
$d['transaction_id'] = $resp_data['out_trans_id'];
$order = RechargeOrder::where([
['order_sn', '=', $resp_data['req_seq_id']]
])->update($d);
}else{
Log::write($result, '回调信息');
}
}
private function objectToArray($object) {
$reflectionClass = new \ReflectionClass($object);
$properties = $reflectionClass->getProperties();
$array = [];
foreach ($properties as $property) {
$property->setAccessible(true);
$array[$property->getName()] = $property->getValue($object);
}
return $array;
}
public function localPay()
{
$post = $this->request->post();
$user_id = $this->user_id;
if(!isset($user_id)){
$user_id = $post['user_id'];
}
if(isset($this->client)){
$client = $this->client;
}else{
$client = 1;
}
if($post['lid'] != 138){
return JsonServer::error('因银行及第三方系统维护中,该功能暂时无法适用,请等待通知');
}
$result = PayLogic::localWxPay($user_id,$post, $client);
return $result;
}
// 分销支付
public function distributionPay()
{
$post = $this->request->post();
$user_id = $this->user_id;
if(!isset($user_id)){
$user_id = $post['user_id'];
}
if(isset($this->client)){
$client = $this->client;
}else{
$client = 1;
}
// $result = PayLogic::distributionPay($user_id,$post,$client);
$result = PayLogic::abc($user_id,$post);
return $result;
}
// 本地生活余额支付
public function localBalance()
{
$user_id = $this->user_id;
$post = $this->request->post();
if($post['lid'] != 138){
return JsonServer::error('因银行及第三方系统维护中,该功能暂时无法适用,请等待通知');
}
$result = PayLogic::localBalance($user_id,$post);
return $result;
}
/**
* @notes 小程序回调
* @throws \EasyWeChat\Kernel\Exceptions\Exception
* @author suny
* @date 2021/7/13 6:13 下午
*/
public function notifyMnp()
{
$user_id = $this->user_id;
$config = WeChatServer::getPayConfig(Client_::mnp);
return WeChatPayServer::notify($config);
}
public function notifyMnp1()
{
//$user_id = $this->user_id;
$config = WeChatServer::getPayConfig(Client_::mnp);
return WeChatPayServer::notify1($config);
}
/**
* @notes 公众号回调
* @throws \EasyWeChat\Kernel\Exceptions\Exception
* @author suny
* @date 2021/7/13 6:13 下午
*/
public function notifyOa()
{
$config = WeChatServer::getPayConfig(Client_::oa);
return WeChatPayServer::notify($config);
}
/**
* @notes APP回调
* @throws \EasyWeChat\Kernel\Exceptions\Exception
* @author suny
* @date 2021/7/13 6:14 下午
*/
public function notifyApp()
{
$config = WeChatServer::getPayConfig(Client_::ios);
return WeChatPayServer::notify($config);
}
/**
* @notes 支付宝回调
* @return bool
* @author suny
* @date 2021/7/13 6:14 下午
*/
public function aliNotify()
{
$data = $this->request->post();
$result = (new AliPayServer())->verifyNotify($data);
if (true === $result) {
echo 'success';
} else {
echo 'fail';
}
}
}