NotifyController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. <?php
  2. namespace api\controllers;
  3. use addons\HuijuBill\common\models\OrderModel;
  4. use addons\HuijuBill\common\models\OrderRefundModel;
  5. use common\components\payment\AliPay;
  6. use common\components\payment\AlternativePay;
  7. use common\components\payment\joinpay\util\RequestUtil;
  8. use common\models\common\PaidSinglePayLog;
  9. use common\models\common\PaymentRefund;
  10. use common\models\common\PaymentTrade;
  11. use common\models\common\PaymentTransfer;
  12. use common\models\user\UserWithdrawLog;
  13. use EasyWeChat\Factory;
  14. use common\enums\AddonsEnum;
  15. use services\common\WithdrawTransferService;
  16. use yii\web\Controller;
  17. use Yii;
  18. use Exception;
  19. use common\helpers\WechatHelper;
  20. use common\helpers\FileHelper;
  21. use common\helpers\ArrayHelper;
  22. use common\enums\PayTypeEnum;
  23. use yii\helpers\Json;
  24. /**
  25. *
  26. * 支付回调
  27. *
  28. * Class NotifyController
  29. * @package frontend\controllers
  30. */
  31. class NotifyController extends Controller
  32. {
  33. //use PayNotify;
  34. /**
  35. * 关闭csrf
  36. *
  37. * @var bool
  38. */
  39. public $enableCsrfValidation = false;
  40. /**
  41. * 公用支付回调 - 微信
  42. *
  43. * @return bool|string
  44. */
  45. public function actionWechat()
  46. {
  47. try{
  48. $xml = file_get_contents('php://input');
  49. $wechat_post = WechatHelper::xmlToArray($xml);
  50. Yii::$app->custom->logs('wechat_notify', $wechat_post);
  51. if(!$wechat_post || !$wechat_post['out_trade_no']){
  52. throw new Exception('参数不合法');
  53. }
  54. $trade = Yii::$app->services->pay->getByTradeNo(trim($wechat_post['out_trade_no']));
  55. if(empty($trade)){
  56. throw new Exception('交易流水号不存在:'.trim($wechat_post['out_trade_no']));
  57. }
  58. $config = Yii::$app->services->pay->getConfig($trade->mall_id,'wechat');
  59. //微信支付配置获取
  60. $wechat = Yii::$app->payment->wechat($config);
  61. $response = $wechat->notify();
  62. if ($response->isPaid()) {
  63. $message = $response->getRequestData();
  64. //日志记录回调文件
  65. FileHelper::writeLog($this->getLogPath('wechat'), json_encode(ArrayHelper::toArray($message)));
  66. ///////////// 建议在这里调用微信的【订单查询】接口查一下该笔订单的情况,确认是已经支付 /////////////
  67. $message['trade_no'] = $message['transaction_id'];
  68. $message['pay_fee'] = bcdiv($message['total_fee'],100,2);
  69. //pay success 注意微信会发二次消息过来 需要判断是通知还是回调
  70. if ($this->pay($message,PayTypeEnum::WECHAT)) {
  71. return WechatHelper::success();
  72. }
  73. throw new Exception('pay error:'.trim($wechat_post['out_trade_no']));
  74. } else {
  75. throw new Exception('response is not Paid:'.trim($wechat_post['out_trade_no']));
  76. }
  77. }catch(Exception $e){
  78. $logPath = $this->getLogPath('error');
  79. FileHelper::writeLog($logPath, json_encode([
  80. 'post' => $wechat_post,
  81. 'error' => $e->getMessage(),
  82. ]));
  83. return WechatHelper::fail();
  84. }
  85. }
  86. /**
  87. * 公用支付回调 - 微信小程序支付回调
  88. * @return bool|string|\Symfony\Component\HttpFoundation\Response
  89. * @throws \EasyWeChat\Kernel\Exceptions\Exception
  90. */
  91. public function actionWechatMp()
  92. {
  93. $xml = file_get_contents('php://input');
  94. $wechat_post = WechatHelper::xmlToArray($xml);
  95. Yii::$app->custom->logs('wechatmp_notify', $wechat_post);
  96. if(!$wechat_post || !$wechat_post['out_trade_no']){
  97. return WechatHelper::fail();
  98. }
  99. $trade = Yii::$app->services->pay->getByTradeNo(trim($wechat_post['out_trade_no']));
  100. if(empty($trade)){
  101. return WechatHelper::fail();
  102. }
  103. // 微信支付参数配置
  104. // 设置appid
  105. // $config = Yii::$app->services->pay->getConfig($trade->mall_id,'wechat');
  106. $config = Yii::$app->services->pay->getWechatConfig($trade->mall_id);
  107. $config['app_id'] = $config['mini_program_app_id'];
  108. \Yii::$app->params['wechatMiniProgramConfig'] = [
  109. 'app_id' => $config['mini_program_app_id'] ?? '',
  110. 'secret' => $config['mini_program_secret'] ?? '',
  111. 'mch_id' => $config['wechat_mch_id'] ?? '',
  112. 'key' => $config['wechat_pay_secret'],
  113. 'cert_path' => dirname(Yii::$app->basePath).'/'.$config['wechat_cert_pem_file'],
  114. 'key_path' => dirname(Yii::$app->basePath).'/'.$config['wechat_key_pem_file'],
  115. ];
  116. // Yii::$app->params['wechatPaymentConfig'] = $config;
  117. // $response = Yii::$app->wechat->payment->handlePaidNotify(function ($message, $fail) {
  118. $payment = Factory::payment(Yii::$app->params['wechatMiniProgramConfig']);
  119. $response = $payment->handlePaidNotify(function ($message, $fail) {
  120. $logPath = $this->getLogPath('wechat-mp');
  121. FileHelper::writeLog($logPath, Json::encode(ArrayHelper::toArray($message)));
  122. if ($message['return_code'] === 'SUCCESS') {
  123. $message['trade_no'] = $message['transaction_id'];
  124. $message['pay_fee'] = bcdiv($message['total_fee'],100,2);
  125. if ($this->pay($message,PayTypeEnum::WECHAT)) {
  126. return true;
  127. }
  128. }
  129. return $fail('处理失败,请稍后再通知我');
  130. });
  131. return $response;
  132. }
  133. /**
  134. * 公用支付回调 - 支付宝
  135. *
  136. * @throws Exception
  137. */
  138. public function actionAlipay()
  139. {
  140. $out_trade_no = $_POST['out_trade_no'];
  141. Yii::$app->custom->logs('alipay', $out_trade_no);
  142. if (!$out_trade_no) {
  143. die('操作失败');
  144. }
  145. $trade = Yii::$app->services->pay->getByTradeNo(trim($out_trade_no));
  146. if(empty($trade)){
  147. die('操作失败');
  148. }
  149. try {
  150. switch ($trade->trade_type) {
  151. case AliPay::JS:
  152. $config = Yii::$app->services->pay->getAlipayConfig($trade);
  153. break;
  154. default:
  155. $config = Yii::$app->services->pay->getConfig($trade->mall_id, 'alipay');
  156. }
  157. $alipay = Yii::$app->payment->alipay($config);
  158. $request = $alipay->notify();
  159. /** @var \Omnipay\Alipay\Responses\AopCompletePurchaseResponse $response */
  160. $response = $request->send();
  161. if ($response->isPaid()) {
  162. $message = Yii::$app->request->post();
  163. $message['pay_fee'] = $message['total_amount'];
  164. $message['mch_id'] = $message['auth_app_id'];
  165. //支付宝回调内有 out_trade_no 商户订单号
  166. // 日志记录
  167. $logPath = $this->getLogPath('alipay');
  168. FileHelper::writeLog($logPath, json_encode(ArrayHelper::toArray($message)));
  169. if ($this->pay($message, PayTypeEnum::ALI)) {
  170. die('操作成功');
  171. }else{
  172. throw new Exception('alipay pay error:'.$out_trade_no);
  173. }
  174. }
  175. throw new Exception('alipay response isPaid error:'.$out_trade_no);
  176. } catch (\Exception $e) {
  177. // 记录报错日志
  178. FileHelper::writeLog($this->getLogPath('error'), $e->getMessage());
  179. die('操作失败'); // 通知响应
  180. }
  181. }
  182. /**
  183. * 公用支付回调 - 汇聚支付
  184. * @return string
  185. */
  186. public function actionJoinpay()
  187. {
  188. $params = \Yii::$app->request->get();
  189. $params = array_map('urldecode', $params);
  190. Yii::$app->custom->logs('joinpay', $params);
  191. try{
  192. $my_param = !empty($params['r5_Mp']) ? json_decode($params['r5_Mp'],true) : []; //json格式
  193. //获取对应支付配置
  194. $mall_id = $my_param['mall_id'] ?? 0;
  195. $config = Yii::$app->services->pay->getConfig($mall_id,'joinpay');
  196. //调用支付类:回调方法
  197. $pay = Yii::$app->payment->joinpay($config);
  198. $message = $pay->notify();
  199. if ($this->pay($message,PayTypeEnum::JOINPAY)) {
  200. die('操作成功');
  201. }
  202. die('操作失败');
  203. }catch(\Exception $e){
  204. // 记录报错日志
  205. $logPath = $this->getLogPath('error');
  206. FileHelper::writeLog($logPath, json_encode([
  207. 'post' => $params,
  208. 'error' => $e->getMessage(),
  209. ]));
  210. die('操作失败'); // 通知响应
  211. }
  212. }
  213. /**
  214. * 公用支付回调 - 汇聚快捷支付
  215. * @return string
  216. */
  217. public function actionJoinpayFastPay()
  218. {
  219. try{
  220. $post = \Yii::$app->request->post();
  221. Yii::$app->custom->logs('joinpayfastpay', $post);
  222. if(empty($post) || empty($post['data'])){
  223. die('操作失败'); // 通知响应
  224. }
  225. $data = json_decode($post['data'],true);
  226. $callback_param = json_decode($data['callback_param'],true);
  227. $mall_id = $callback_param['mall_id'] ?? 0;
  228. //获取对应支付配置
  229. $config = Yii::$app->services->pay->getConfig($mall_id,'joinpay_fast');
  230. $pay = Yii::$app->payment->joinpayFastPay($config);
  231. $message = $pay->notify($post);
  232. if ($this->pay($message,PayTypeEnum::JOINPAY_FAST_PAY)) {
  233. die('操作成功');
  234. }
  235. die('操作失败');
  236. }catch(\Exception $e){
  237. // 记录报错日志
  238. $logPath = $this->getLogPath('error');
  239. FileHelper::writeLog($logPath, json_encode([
  240. 'post' => $post,
  241. 'error' => $e->getMessage(),
  242. ]));
  243. die('操作失败'); // 通知响应
  244. }
  245. }
  246. public function actionSandpayFastPay()
  247. {
  248. try{
  249. $post = \Yii::$app->request->post();
  250. Yii::error("actionSandpayFastPay post: " , var_export($post, true));
  251. if(empty($post) || empty($post['data'])){
  252. die('操作失败'); // 通知响应
  253. }
  254. $data = json_decode($post['data'],true);
  255. // $trade = PaymentTrade::findOne(['out_trade_no' => $data['body']['orderCode'] ?? '']);
  256. $trade = Yii::$app->services->pay->getByTradeNo($data['body']['orderCode']);
  257. if (empty($trade)) die("交易不存在: {$data['body']['orderCode']}");
  258. //获取对应支付配置
  259. $config = Yii::$app->services->pay->getConfig($trade->mall_id,'sandpay_fast');
  260. $pay = Yii::$app->payment->sandpayFastPay($config);
  261. $message = $pay->notify($post);
  262. if ($this->pay($message,PayTypeEnum::SANDPAY_FAST_PAY)) {
  263. die('操作成功');
  264. }
  265. }catch(\Exception $e){
  266. // 记录报错日志
  267. $logPath = $this->getLogPath('error');
  268. FileHelper::writeLog($logPath, json_encode([
  269. 'post' => $post,
  270. 'error' => $e->getMessage(),
  271. ]));
  272. die('操作失败'); // 通知响应
  273. }
  274. }
  275. /**
  276. * 公用支付回调 - 汇聚分账支付
  277. *
  278. * @return void
  279. */
  280. public function actionHuiju_bill()
  281. {
  282. try {
  283. $post = Yii::$app->request->post();
  284. if (empty($post)) {
  285. throw new Exception('参数不存在');
  286. }
  287. // 查询流水订单
  288. $trade = Yii::$app->services->pay->getByTradeNo(trim($post['order_no']));
  289. if (empty($trade)) {
  290. FileHelper::writeLog($this->getLogPath('error'), 12223423432234);
  291. throw new Exception('流水订单不存在');
  292. }
  293. // 支付配置
  294. $config = Yii::$app->services->setting->addons->get($trade->mall_id, AddonsEnum::ADDONS_NAME_HUIJU_BILL, 'basic');
  295. $pay = Yii::$app->payment->huijuBill($config);
  296. if (empty($pay)) {
  297. throw new Exception('支付类不存在');
  298. }
  299. // 查询支付订单
  300. $order = OrderModel::findOne(['trade_id' => $trade->id]);
  301. // 支付回调
  302. $res = $pay->notify($post, $trade);
  303. if ($res === true) {
  304. $post['out_trade_no'] = $post['order_no'];
  305. $post['pay_fee'] = $post['amount'];
  306. $this->pay($post, PayTypeEnum::HUIJU_BILL, function () use ($order) {
  307. $order->success();
  308. });
  309. }else{
  310. exit('notify_error');
  311. }
  312. } catch (Exception $e) {
  313. FileHelper::writeLog($this->getLogPath('error'), json_encode(ArrayHelper::toArray([
  314. 'message' => $e->getMessage(),
  315. 'line' => $e->getLine(),
  316. 'file' => method_exists($e, 'getFile') ? $e->getFile() : '',
  317. // 'trace' => $e->getTrace(),
  318. // 'code' => $e->getCode(),
  319. ])));
  320. exit('error'.$e->getMessage());
  321. }
  322. exit('success');
  323. }
  324. /**
  325. * 公用支付回调 - 汇聚分账支付退款回调
  326. *
  327. * @return void
  328. */
  329. public function actionHuiju_bill_refund()
  330. {
  331. try {
  332. $post = Yii::$app->request->post();
  333. if (empty($post)) {
  334. throw new Exception('参数不存在');
  335. }
  336. // 是否返回退款订单号
  337. if (empty($post['refund_order_no'])) {
  338. throw new Exception('退款失败,未返回订单号');
  339. }
  340. // 查询退款订单
  341. $refund_order = OrderRefundModel::findOne(['refund_order_no' => $post['refund_order_no']]);
  342. if (empty($refund_order)) {
  343. throw new Exception("退款失败,订单{$post['refund_order_no']}订单号不存在");
  344. }
  345. // 查询中台订单
  346. $config = Yii::$app->services->setting->addons->get($refund_order->mall_id, AddonsEnum::ADDONS_NAME_HUIJU_BILL, 'basic');
  347. $pay = Yii::$app->payment->huijuBill($config);
  348. $res = $pay->refund_notify($post, $refund_order);
  349. // 设置订单失败或成功
  350. $res ? $refund_order->success() : $refund_order->fail();
  351. } catch (Exception $e) {
  352. FileHelper::writeLog($this->getLogPath('error'), json_encode(ArrayHelper::toArray([
  353. 'message' => $e->getMessage(),
  354. 'line' => $e->getLine(),
  355. 'file' => method_exists($e, 'getFile') ? $e->getFile() : '',
  356. ])));
  357. }
  358. exit('success');
  359. }
  360. /**
  361. * 支付公用方法
  362. * @param $message
  363. * @param int $pay_type 支付类型
  364. * @param callback $call_back 回调函数
  365. * @return bool
  366. */
  367. protected function pay($message, $pay_type, $call_back = null)
  368. {
  369. $transaction = Yii::$app->db->beginTransaction();
  370. try {
  371. /**
  372. * @var PaymentTrade $payLog
  373. */
  374. if (!($payLog = Yii::$app->services->pay->getByTradeNo($message['out_trade_no']))) {
  375. throw new Exception('找不到支付信息');
  376. }
  377. // 支付完成
  378. if ($payLog->is_pay == 1) {
  379. return true;
  380. }
  381. //支付金额验证
  382. if(bccomp($message['pay_fee'], $payLog->getOnlinePayFee()) !== 0){
  383. throw new Exception('支付金额不一致');
  384. }
  385. $payLog->setOnlinePayType($pay_type);
  386. $payLog->isMixedPay() && $payLog->setOnlinePaySuccess();
  387. $payLog->is_pay = 1;
  388. $payLog->pay_time = time();
  389. $payLog->third_trade_no = $message['trade_no'] ?? '';
  390. if (!empty($message['trade_type'])) {
  391. $payLog->trade_type = $message['trade_type'];
  392. }
  393. if (!$payLog->save()) {
  394. throw new Exception('日志修改失败');
  395. }
  396. // 业务回调
  397. $orders = Yii::$app->services->pay->changePaymentTradeOrder($payLog,$pay_type);
  398. // 回调函数
  399. if (is_callable($call_back)) {
  400. call_user_func($call_back);
  401. }
  402. $transaction->commit();
  403. Yii::$app->services->pay->notify($orders);
  404. return true;
  405. } catch (\Exception $e) {
  406. $transaction->rollBack();
  407. // FileHelper::writeLog($this->getLogPath('error'), $e->getMessage());
  408. // 记录报错日志
  409. FileHelper::writeLog($this->getLogPath('error'), json_encode(ArrayHelper::toArray([
  410. 'message' => $e->getMessage(),
  411. 'line' => $e->getLine(),
  412. 'file' => method_exists($e, 'getFile') ? $e->getFile() : '',
  413. 'trace' => $e->getTrace(),
  414. 'code' => $e->getCode(),
  415. ])));
  416. return false;
  417. }
  418. }
  419. /**
  420. * @param $type
  421. * @return string
  422. */
  423. protected function getLogPath($type)
  424. {
  425. return Yii::getAlias('@runtime') . "/pay-logs/" . date('Y_m_d') . '/' . $type . '.txt';
  426. }
  427. /**
  428. * 中金支付异步回调
  429. */
  430. public function actionSicpay(){
  431. $mall_id = $this->request->get('mall_id');
  432. $data = $this->request->post();
  433. Yii::error("---notify-sicpay---{$mall_id}");
  434. Yii::error($data);
  435. try {
  436. $pay_service = new \addons\Sicpay\common\services\PayService();
  437. $res = $pay_service->notifyHandle($mall_id,$data);
  438. Yii::error(json_encode($res));
  439. Yii::getLogger()->flush(true);
  440. if(is_array($res)){
  441. // 成功
  442. $res = $this->pay($res,PayTypeEnum::SIC_PAY);
  443. if($res) {
  444. return 'success';
  445. }
  446. }
  447. return 'error';
  448. } catch (Exception $e) {
  449. FileHelper::writeLog($this->getLogPath('error'), json_encode(ArrayHelper::toArray([
  450. 'message' => $e->getMessage(),
  451. 'line' => $e->getLine(),
  452. 'file' => method_exists($e, 'getFile') ? $e->getFile() : '',
  453. ])));
  454. return 'error';
  455. }
  456. }
  457. /**
  458. * 单笔代付回调
  459. */
  460. public function actionJoinpaySingle()
  461. {
  462. try{
  463. $post = \Yii::$app->request->post();
  464. \Yii::error('JoinpaySinglePay post:'.json_encode($post));
  465. $merchantOrderNo = $post['merchantOrderNo'] ?? '';
  466. if(!$post || !$merchantOrderNo){
  467. throw new Exception('invalid param');
  468. }
  469. $log = PaidSinglePayLog::findOne(['merchant_order_no' => $merchantOrderNo]);
  470. if(!$log){
  471. throw new Exception('invalid param');
  472. }
  473. //验签
  474. //获取对应支付配置
  475. $config = \Yii::$app->services->setting->addons->get($log->mall_id, AddonsEnum::ADDONS_NAME_JOIN_PAY, 'basic');
  476. $params['status'] = $post['status'];
  477. $params['errorCode'] = $post['errorCode'];
  478. $params['errorCodeDesc'] = $post['errorCodeDesc'];
  479. $params['userNo'] = $post['userNo'];
  480. if(isset($post['tradeMerchantNo'])){
  481. $params['tradeMerchantNo'] = $post['tradeMerchantNo'];
  482. }
  483. $params['merchantOrderNo'] = $post['merchantOrderNo'];
  484. $params['platformSerialNo'] = $post['platformSerialNo'];
  485. $params['receiverAccountNoEnc'] = $post['receiverAccountNoEnc'];
  486. $params['receiverNameEnc'] = $post['receiverNameEnc'];
  487. $params['paidAmount'] = sprintf("%.2f", $post['paidAmount']);
  488. $params['fee'] = sprintf("%.2f", $post['fee']);
  489. //验证签名
  490. $hmac = $post['hmac'];
  491. $hmacVal = urlencode(RequestUtil::hmacRequest($params, $config['joinpay_merchant_secret']));
  492. if ($hmacVal !== $hmac) {
  493. throw new Exception('invalid sign');
  494. }
  495. $pay = new AlternativePay($config, $log->mall_id);
  496. $res = $pay->queryAlternativeOrder($log->merchant_order_no);
  497. if (!empty($res)) {
  498. $trans = \Yii::$app->db->beginTransaction();
  499. try {
  500. $pay->updateSinglePayStatus($log,$res);
  501. $trans->commit();
  502. } catch (\Exception $e) {
  503. $trans->rollBack();
  504. \Yii::error('修改汇聚代付失败,获取结果:' . json_encode($res));
  505. throw new Exception('save error');
  506. }
  507. }
  508. echo json_encode(['statusCode' => '2001','message' => '成功']);
  509. exit;
  510. }catch(\Exception $e){
  511. \Yii::error('actionJoinpaySingle error:' . $e->getMessage());
  512. echo json_encode(['statusCode' => '2002','message' => $e->getMessage()]);
  513. exit;
  514. }
  515. }
  516. /**
  517. * 汇聚退款回调
  518. * @author zmz
  519. */
  520. public function actionJoinpayRefund()
  521. {
  522. try {
  523. $post = Yii::$app->request->post();
  524. if (empty($post)) {
  525. throw new Exception('参数不存在');
  526. }
  527. // 是否返回退款订单号
  528. if (empty($post['r2_OrderNo'])) {
  529. throw new Exception('退款失败,商户订单号');
  530. }
  531. if (empty($post['r3_RefundOrderNo'])) {
  532. throw new Exception('退款失败,商户退款订单号');
  533. }
  534. // 查询退款订单
  535. $payment_refund = PaymentRefund::findOne(['order_no' => $post['r3_RefundOrderNo'],'is_pay' => 0,'pay_type' => PayTypeEnum::JOINPAY]);
  536. if (empty($payment_refund)) {
  537. throw new Exception("退款失败,订单{$post['r3_RefundOrderNo']}订单号不存在");
  538. }
  539. Yii::$app->services->pay->refundNotify($payment_refund);
  540. } catch (Exception $e) {
  541. FileHelper::writeLog($this->getLogPath('error'), json_encode(ArrayHelper::toArray([
  542. 'message' => $e->getMessage(),
  543. 'line' => $e->getLine(),
  544. 'file' => method_exists($e, 'getFile') ? $e->getFile() : '',
  545. ])));
  546. }
  547. exit('success');
  548. }
  549. /**
  550. * 汇聚退款回调
  551. * @author zmz
  552. */
  553. public function actionJoinpayFastRefund()
  554. {
  555. try {
  556. $post = Yii::$app->request->post();
  557. \Yii::error('JoinpayFastRefund post:'.json_encode($post));
  558. if(empty($post) || empty($post['data'])){
  559. die('操作失败'); // 通知响应
  560. }
  561. $data = json_decode($post['data'],true);
  562. // 查询退款订单
  563. $payment_refund = PaymentRefund::findOne(['order_no' => $data['refund_order_no'],'is_pay' => 0,'pay_type' => PayTypeEnum::JOINPAY_FAST_PAY]);
  564. if (empty($payment_refund)) {
  565. throw new Exception("汇聚快捷支付退款失败,订单{$data['refund_order_no']}订单号不存在");
  566. }
  567. //验签
  568. $config = Yii::$app->services->pay->getConfig($payment_refund->mall_id,'joinpay_fast');
  569. if(!$config){
  570. throw new Exception("汇聚快捷支付退款失败,查询不到配置");
  571. }
  572. $pay = \Yii::$app->payment->joinpayFastPay($config);
  573. $pay->signature_verify($post);
  574. if($data['refund_status'] == '100'){
  575. //退款成功
  576. Yii::$app->services->pay->refundNotify($payment_refund);
  577. }
  578. exit('success');
  579. } catch (Exception $e) {
  580. FileHelper::writeLog($this->getLogPath('error'), json_encode(ArrayHelper::toArray([
  581. 'message' => $e->getMessage(),
  582. 'line' => $e->getLine(),
  583. 'file' => method_exists($e, 'getFile') ? $e->getFile() : '',
  584. ])));
  585. exit('fail');
  586. }
  587. //exit('success');
  588. }
  589. /**
  590. * 汇聚退款回调
  591. * @author zmz
  592. */
  593. public function actionSandpayFastRefund()
  594. {
  595. try {
  596. $post = Yii::$app->request->post();
  597. \Yii::error('SandpayFastRefund post:'.json_encode($post));
  598. if(empty($post) || empty($post['data'])){
  599. die('操作失败'); // 通知响应
  600. }
  601. $data = json_decode($post['data'],true);
  602. // 查询退款订单
  603. $payment_refund = PaymentRefund::findOne(['order_no' => $data['body']['oriOrderCode'],'is_pay' => 0,'pay_type' => PayTypeEnum::SANDPAY_FAST_PAY]);
  604. if (empty($payment_refund)) {
  605. throw new Exception("杉德快捷支付退款失败,订单 {$data['body']['oriOrderCode']} 订单号不存在");
  606. }
  607. //验签
  608. $config = Yii::$app->services->pay->getConfig($payment_refund->mall_id,'sandpay_fast');
  609. if(!$config){
  610. throw new Exception("汇聚快捷支付退款失败,查询不到配置");
  611. }
  612. $pay = \Yii::$app->payment->sandpayFastPay($config);
  613. $pay->client->verify($config, $post['data'], $post['sign']);
  614. if($data['body']['orderStatus'] == 1){
  615. //退款成功
  616. Yii::$app->services->pay->refundNotify($payment_refund);
  617. }
  618. exit('success');
  619. } catch (Exception $e) {
  620. FileHelper::writeLog($this->getLogPath('error'), json_encode(ArrayHelper::toArray([
  621. 'message' => $e->getMessage(),
  622. 'line' => $e->getLine(),
  623. 'file' => method_exists($e, 'getFile') ? $e->getFile() : '',
  624. ])));
  625. exit('fail');
  626. }
  627. //exit('success');
  628. }
  629. /**
  630. * 微信商家转账回调
  631. */
  632. public function actionWechatTransfer($appKey)
  633. {
  634. try {
  635. $data = Yii::$app->request->post();
  636. if (empty($data)) {
  637. $file = file_get_contents("php://input");
  638. $data = json_decode($file, true);
  639. }
  640. // 存储回调过来的数据
  641. Yii::$app->custom->logs('微信商家转账回调数据:' . $appKey, json_encode($data));
  642. if (empty($data)) {
  643. return json_encode(['code' => 'FAIL', 'message' => '参数错误']);
  644. }
  645. $res = new WithdrawTransferService();
  646. $res = $res->notify($data, $appKey);
  647. if (!$res) {
  648. return json_encode(['code' => 'FAIL', 'message' => '参数错误']);
  649. }
  650. return json_encode(['code' => 'SUCCESS', 'message' => '操作成功']);
  651. } catch (Exception $e) {
  652. return json_encode(['code' => 'FAIL', 'message' => '参数错误']);
  653. }
  654. }
  655. }