|
@@ -12,6 +12,10 @@ 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;
|
|
|
/**
|
|
|
* 支付接口
|
|
|
*/
|
|
@@ -25,36 +29,68 @@ class Pay extends Base
|
|
|
parent::_initialize();
|
|
|
set_addon_config('epay', ['version'=>'v3'], false);
|
|
|
}
|
|
|
- /**
|
|
|
- *
|
|
|
- * 支付回调
|
|
|
- * @return void
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 支付成功回调
|
|
|
*/
|
|
|
public function notify()
|
|
|
{
|
|
|
- $type = $this->request->param('type');
|
|
|
- $paytype = $this->request->param('paytype');
|
|
|
- if ($type == 'notify') {
|
|
|
- $pay = \addons\epay\library\Service::checkNotify($paytype);
|
|
|
- if (!$pay) {
|
|
|
- echo '签名错误';
|
|
|
- \think\Log::write('签名错误', 'epay',);
|
|
|
- return;
|
|
|
+ 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();
|
|
|
}
|
|
|
- $data = $pay->verify();
|
|
|
- // try {
|
|
|
- $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
|
|
|
- PayOperService::settle($data['out_trade_no'], $payamount, $paytype == 'alipay' ? $data['trade_no'] : $data['transaction_id']);
|
|
|
- // } catch (\Exception $e) {
|
|
|
- // \think\Log::write($e->getMessage(), 'epay');
|
|
|
- // }
|
|
|
- echo $pay->success();
|
|
|
- }// pc 支付
|
|
|
- elseif ($type == 'return') {
|
|
|
- // $order_sn = $this->request->param('order_sn');
|
|
|
- // $this->redirect("index/shop.order/detail", ['orderid' => $order_sn]);
|
|
|
+
|
|
|
+ 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' ? 'json' : ''));
|
|
|
}
|
|
|
- return;
|
|
|
+
|
|
|
+ return $result;
|
|
|
}
|
|
|
|
|
|
/**
|