config = WeChatPayMerchantConfigService::getPayConfig(); $this->app = new Application($this->config); } public function wechatPayServiceMerchantAppPay($order) { try { $response = $this->app->getClient()->postJson('pay/unifiedorder', [ 'appid' => $this->config['app_id'], 'mchid' => $this->config['mch_id'], 'sub_mch_id' => $order['sub_mch_id'], 'description' => PayOrderEnum::getPayOrderTypeDesc($order['order_type']), 'out_trade_no' => $order['order_no'], 'notify_url' => $this->config['notify_url'], 'total_fee' => $order['total_fee'], 'spbill_create_ip' => $_SERVER['SERVER_ADDR'], // 调用微信支付API的机器IP 'time_start' => date('YmdHis', $order['create_time']), 'trade_type' => 'APP', 'attach' => $order['order_no'] ]); $result = $response->toArray(false); $this->checkResultFail($result); $re = $this->app->getUtils()->buildAppConfig($result['prepay_id'], $this->config['appid']); $re['partnerid'] = $order['sub_mch_id']; return $re; } catch (\Exception $e) { $this->setError($e->getMessage()); return false; } } /** * @notes 退款 * @param array $refundData * @return mixed * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @author 段誉 * @date 2023/2/28 16:53 */ public function refund(array $refundData) { $response = $this->app->getClient()->postJson('secapi/pay/refund', [ 'transaction_id' => $refundData['transaction_id'], 'out_refund_no' => $refundData['refund_sn'], 'amount' => [ 'refund' => intval($refundData['refund_amount'] * 100), 'total' => intval($refundData['total_amount'] * 100), 'currency' => 'CNY', ] ]); $result = $response->toArray(false); $this->checkResultFail($result); return $result; } /** * @notes 查询退款 * @param $refundSn * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @author 段誉 * @date 2023/3/1 11:16 */ public function queryRefund($refundSn) { $response = $this->app->getClient()->get("v3/refund/domestic/refunds/{$refundSn}"); return $response->toArray(false); } /** * @notes 捕获错误 * @param $result * @throws \Exception * @author 段誉 * @date 2023/2/28 12:09 */ public function checkResultFail($result) { if (!empty($result['code']) || !empty($result['message'])) { throw new \Exception('微信:'. $result['code'] . '-' . $result['message']); } } /** * @notes 预支付配置 * @param $prepayId * @param $appId * @return mixed[] * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @author 段誉 * @date 2023/2/28 17:38 */ public function getPrepayConfig($prepayId, $appId) { return $this->app->getUtils()->buildBridgeConfig($prepayId, $appId); } /** * @notes 支付回调 * @return \Psr\Http\Message\ResponseInterface * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException * @throws \ReflectionException * @throws \Throwable * @author 段誉 * @date 2023/2/28 14:20 */ public function notify() { $server = $this->app->getServer(); // 支付通知 $server->handlePaid(function (Message $message) { if ($message['trade_state'] === 'SUCCESS') { $extra['transaction_id'] = $message['transaction_id']; $attach = $message['attach']; $message['out_trade_no'] = mb_substr($message['out_trade_no'], 0, 18); switch ($attach) { case 'recharge': $order = RechargeOrder::where(['sn' => $message['out_trade_no']])->findOrEmpty(); if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) { return true; } PayNotifyLogic::handle('recharge', $message['out_trade_no'], $extra); break; } } return true; }); // 退款通知 $server->handleRefunded(function (Message $message) { return true; }); return $server->serve(); } }