| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 | <?phpnamespace app\api\controller;use think\Db;use think\Log;use think\Exception;use think\exception\HttpResponseException;use app\common\Service\Pay\PayService;use app\common\Service\Pay\PayOperService;use app\common\Enum\ChannelEnum;use app\common\Service\OrderService;use app\common\model\Third as ThirdOauth;use app\common\model\Order;use app\common\model\pay\Index as PayModel;use app\common\Enum\PayEnum;use Yansongda\Pay\Pay as YansongdaPay;use Psr\Http\Message\ResponseInterface;/** * 支付接口 */class Pay extends Base{    protected $noNeedLogin = ['notify'];    protected $noNeedRight = '*';    public function _initialize()    {        parent::_initialize();    }     /**     * 支付成功回调     */    public function notify()    {        Log::write('pay-notify-comein:');        $payment = $this->request->param('payment');        $platform = $this->request->param('platform');        $payService = new PayService($payment, $platform);                $result = $payService->notify(function ($data, $originData = []) use ($payment) {            Log::write('pay-notify-data:' . json_encode($data));            $out_trade_no = $data['out_trade_no'];            // 查询 pay 交易记录            $payModel = PayModel::where('pay_sn', $out_trade_no)->find();            if (!$payModel || $payModel->status !=  PayEnum::PAY_STATUS_UNPAID) {                // 订单不存在,或者订单已支付                return YansongdaPay::$payment()->success();            }            Db::transaction(function () use ($payModel, $data, $originData, $payment) {                $notify = [                    'pay_sn' => $data['out_trade_no'],                    'transaction_id' => $data['transaction_id'],                    'notify_time' => $data['notify_time'],                    'buyer_info' => $data['buyer_info'] ?? "",                    'payment_json' => $originData ? json_encode($originData) : json_encode($data),                    'pay_fee' => $data['pay_fee'],          // 微信的已经*100处理过了                    'pay_type' => $payment              // 支付方式                ];                // pay 实例                $payOper = new PayOperService($payModel->user_id);                $payOper->notify($payModel, $notify);            });            return YansongdaPay::$payment()->success();        });        return $this->payResponse($result, $payment);    }       /**     * 处理返回结果 tp5 不能直接 return YansongdaPay::$payment()->success()     *     * @param object|string $result     * @param string|null $payment     * @return void     */    private function payResponse($result = null, $payment = null)    {        if ($result instanceof ResponseInterface) {            $content = $result->getBody()->getContents();            $content = $payment == 'wechat' || $payment == 'douyin' ? json_decode($content, true) : $content;            return response($content, 200, [], ($payment == 'wechat' || $payment == 'douyin' ? 'json' : ''));        }        return $result;    }    /**     * 预支付订单接口     * @return void     */    public function prepay()    {        $orderId = $this->request->post('orderId');        $payment = $this->request->post('payment');        // $openid = $this->request->post('openid', '');        // $money = $this->request->post('money', 0);        $logincode = $this->request->post('login_code', '');        // $money = $money > 0 ? $money : 0;        $platform = $this->request->header('platform');        $userId = $this->auth->getUser()->id;        // 获取订单实例        $order = new Order();        $order = $order->where('user_id', $userId)->where('id', $orderId)->find();                 if (!$order) {            $this->error(__('No Results were found'));        }        // 验证订单状态        if (!$order->canPayHandle()) {            $this->error(__('订单状态不支持支付'));        }        // 验证支付类型        if (!$payment || !in_array($payment, ['wechat', 'alipay', 'douyin'])) {            $this->error('支付类型不能为空');        }        // 验证支付环境        // if ($channel && !ChannelEnum::channelSupportsPayment($channel, $payment)) {        //     $this->error('当前渠道不支持该支付方式');        // }               $payOper = new PayOperService($this->auth->getUser());        $orderType = $order->type;        $payModel = $payOper->{$payment}($order, $order->amount, $orderType);        $order->save(['pay_type' =>$payment]);        $order_data = [            'order_id' => $order->id,            'out_trade_no' => $payModel->pay_sn,            'total_amount' => $payModel->pay_fee,      // 剩余支付金额        ];        // 微信公众号,小程序支付,必须有 openid        if ($payment == 'wechat') {            // 如果传了 code            if (!empty($logincode)) {                $json = (new \app\common\library\Wechat\Service())->getWechatSession($logincode);                $openid = $json['openid'] ?? '';                if(empty($openid)){                    $this->error('未获取到openidid');                }                $order_data['payer']['openid'] = $openid;            }else{                if (in_array($platform, ['WechatOfficialAccount', 'WechatMiniProgram'])) {                    if (isset($openid) && $openid) {                        // 如果传的有 openid                        $order_data['payer']['openid'] = $openid;                    } else {                        // 没有 openid 默认拿下单人的 openid                        $oauth = ThirdOauth::where([                            'user_id' => $order->user_id,                            'platform' => $payment,                            'apptype' => lcfirst(str_replace($payment, '', $platform))                        ])->find();                            $order_data['payer']['openid'] = $oauth ? $oauth->openid : '';                    }                        if (empty($order_data['payer']['openid'])) {                        // 缺少 openid                        $this->error('miss_openid', -1);                    }                }                }                       $order_data['description'] = '商城订单支付';        } else {            $order_data['subject'] = '商城订单支付';        }        if($platform == ChannelEnum::CHANNEL_DOUYIN_MINI_PROGRAM){            $order_data['out_order_no'] = $payModel->pay_sn;        }        // echo  "<pre>";        // print_r(lcfirst(str_replace($payment, '', $platform)));        // echo  "</pre>";die();        // echo "<pre>";        // print_r($order_data);        // echo "</pre>";die();        try {            $payService = new PayService($payment, $platform);                        $result = $payService->pay($order_data);                    } catch (\Yansongda\Pay\Exception\Exception $e) {            $this->error('支付失败' . (config('app_debug') ? ":" . $e->getMessage() : ',请重试'));        } catch (HttpResponseException $e) {            $data = $e->getResponse()->getData();            $message = $data ? ($data['msg'] ?? '') : $e->getMessage();            $this->error('支付失败' . (config('app_debug') ? ":" . $message : ',请重试'));        } catch (\Exception $e) {            // 捕获构造函数错误和其他异常            $errorMessage = $e->getMessage();            Log::error('PayService创建失败: ' . $errorMessage);            $this->error('支付服务初始化失败' . (config('app_debug') ? ":" . $errorMessage : ',请重试'));        }        if (in_array($platform, [ChannelEnum::CHANNEL_ANDROID_APP, ChannelEnum::CHANNEL_IOS_APP])) {            if (in_array($payment, ['wechat', 'douyin'])) {                // Yansongda\Supports\Collection,可当数组,可当字符串,这里不用处理            } else {                // Guzzle                $result = $result->getBody()->getContents();            }        }                $this->success('', [            'pay_data' => $result,        ]);    }       /**     * 处理余额混合支付     * @param PayOperService $payOper     * @param mixed $order     * @param string $order_type     * @param float $money     * @return mixed     */    protected function handleBalanceMixedPayment($payOper, $order, $order_type, $money)    {        return Db::transaction(function () use ($payOper, $order, $order_type, $money) {            // 加锁读订单            $order = $order->lock(true)->find($order->id);            // 余额支付            $order = $payOper->money($order, $money, $order_type);            return $order;        });    }    /**     * 处理纯余额支付     * @param PayOperService $payOper     * @param mixed $order     * @param string $order_type     * @return mixed     * @throws Exception     */    protected function handleBalancePayment($payOper, $order, $order_type)    {        $order = Db::transaction(function () use ($payOper, $order, $order_type) {            // 加锁读订单            $order = $order->lock(true)->find($order->id);            $order = $payOper->money($order, $order->remain_pay_fee, $order_type);            return $order;        });        if ($order->status != $order::STATUS_PAID) {            throw new Exception('订单支付失败');        }                return $order;    }    /**     * 处理货到付款     * @param PayOperService $payOper     * @param mixed $order     * @param string $order_type     * @return mixed     * @throws Exception     */    protected function handleOfflinePayment($payOper, $order, $order_type)    {        if (!isset($order->ext['offline_status']) || $order->ext['offline_status'] != 'enable') {            throw new Exception('订单不支持货到付款');        }        return Db::transaction(function () use ($payOper, $order, $order_type) {            // 加锁读订单            $order = $order->lock(true)->find($order->id);            $order = $payOper->offline($order, 0, $order_type);     // 增加 0 记录            return $order;        });    }    /**     * 处理第三方支付(微信、支付宝)     * @param PayOperService $payOper     * @param mixed $order     * @param string $order_type     * @param string $payment     * @param string $platform     * @param string $channel     * @param string $openid     * @return mixed     * @throws Exception     */    protected function handleThirdPartyPayment($payModel, $order, $payment, $platform, $openid)    {        $order_data = [            'order_id' => $order->id,            'out_trade_no' => $payModel->pay_sn,            'total_amount' => $payModel->pay_fee,      // 剩余支付金额        ];        // 微信支付需要 openid        if ($payment == 'wechat') {            $this->handleWechatPaymentData($order_data, $platform, $openid, $order);            $order_data['description'] = '商城订单支付';        } else {            $order_data['subject'] = '商城订单支付';        }        // 创建支付服务        $payService = new PayService($payment, $platform);        try {            $result = $payService->pay($order_data);        } catch (\Yansongda\Pay\Exception\Exception $e) {            throw new Exception('支付失败' . (config('app_debug') ? ":" . $e->getMessage() : ',请重试'));        } catch (HttpResponseException $e) {            $data = $e->getResponse()->getData();            $message = $data ? ($data['msg'] ?? '') : $e->getMessage();            throw new Exception('支付失败' . (config('app_debug') ? ":" . $message : ',请重试'));        }        // APP 平台特殊处理        if ($platform == 'App') {            if ($payment == 'wechat') {                // Yansongda\Supports\Collection,可当数组,可当字符串,这里不用处理            } else {                // Guzzle                $result = $result->getBody()->getContents();            }        }        return $result;    }    /**     * 处理微信支付数据     * @param array &$order_data     * @param string $platform     * @param string $openid     * @param mixed $order     * @throws Exception     */    protected function handleWechatPaymentData(&$order_data, $platform, $openid, $order)    {        // 微信公众号,小程序支付,必须有 openid        if (in_array($platform, ['WechatOfficialAccount', 'WechatMiniProgram'])) {            if (isset($openid) && $openid) {                // 如果传的有 openid                $order_data['payer']['openid'] = $openid;            } else {                // 没有 openid 默认拿下单人的 openid                // 需要根据实际的第三方授权模型调整                try {                    $oauthModel = '\\app\\common\\model\\Third';                    if (class_exists($oauthModel)) {                        $oauth = $oauthModel::where([                            'user_id' => $order->user_id,                            'platform' => 'wechat',                            'apptype' => lcfirst(str_replace('wechat', '', $platform))                        ])->find();                        $order_data['payer']['openid'] = $oauth ? $oauth->openid : '';                    } else {                        $order_data['payer']['openid'] = '';                    }                } catch (\Exception $e) {                    $order_data['payer']['openid'] = '';                }            }            if (empty($order_data['payer']['openid'])) {                // 缺少 openid                throw new Exception('miss_openid', -1);            }        }    }         }
 |