ConvergepayController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /**
  3. * Author: 芸众商城 www.yunzshop.com
  4. * Date: 2019/4/24
  5. * Time: 下午3:10
  6. */
  7. namespace app\payment\controllers;
  8. use app\common\helpers\Url;
  9. use app\common\models\AccountWechats;
  10. use app\common\services\Pay;
  11. use app\common\services\PayFactory;
  12. use app\payment\PaymentController;
  13. use Yunshop\ConvergePay\models\WithdrawLog;
  14. use Yunshop\ConvergePay\services\NotifyService;
  15. use app\common\events\withdraw\WithdrawSuccessEvent;
  16. class ConvergepayController extends PaymentController
  17. {
  18. private $parameter = [];
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. $this->parameter = $_REQUEST;
  23. }
  24. public function notifyUrl()
  25. {
  26. if (empty(\YunShop::app()->uniacid)) {
  27. if (!$this->getResponse('r5_Mp')) {
  28. \Log::debug('汇聚支付回调公众号为空--->', $this->parameter);
  29. echo 'No official account exists.';exit();
  30. }
  31. \Setting::$uniqueAccountId = \YunShop::app()->uniacid =$this->getResponse('r5_Mp');
  32. AccountWechats::setConfig(AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid));
  33. }
  34. $payType = $this->getConvergePayType()[$this->getResponse('rc_BankCode')];
  35. if (!$payType) {
  36. \Log::debug('未知汇聚支付类型---'.$this->getResponse('rc_BankCode'));
  37. echo 'Unknown payment type';exit();
  38. }
  39. $this->log($this->parameter, $payType['name']);
  40. if ($this->getSignResult()) {
  41. if ($this->getResponse('r6_Status') == '100') {
  42. \Log::debug("<------{$payType['name']} 业务处理----");
  43. $data = $this->data($payType['name'], $payType['pay_type_id']);
  44. $this->payResutl($data);
  45. \Log::debug("----{$payType['name']} 处理结束---->");
  46. echo 'success';
  47. } else {
  48. //其他错误
  49. \Log::debug("------{$payType['name']} 支付失败-----");
  50. echo 'fail';
  51. }
  52. } else {
  53. //签名验证失败
  54. \Log::debug("------{$payType['name']} 签名验证失败-----");
  55. echo 'fail1';
  56. }
  57. }
  58. public function getConvergePayType()
  59. {
  60. return [
  61. 'ALIPAY_H5' => [
  62. 'name' => '汇聚-支付宝H5',
  63. 'pay_type_id' => PayFactory::CONVERGE_ALIPAY_H5_PAY,
  64. ],
  65. 'UNIONPAY_H5' => [
  66. 'name' => '汇聚-云闪付',
  67. 'pay_type_id' => PayFactory::CONVERGE_UNION_PAY,
  68. ],
  69. 'WEIXIN_CARD' => [
  70. 'name' => '汇聚-微信付款码',
  71. 'pay_type_id' => PayFactory::CONVERGE_WECHAT_CARD_PAY,
  72. ],
  73. 'ALIPAY_CARD' => [
  74. 'name' => '汇聚-支付宝付款码',
  75. 'pay_type_id' => PayFactory::CONVERGE_ALIPAY_CARD_PAY,
  76. ],
  77. ];
  78. }
  79. public function notifyUrlWechat()
  80. {
  81. if (empty(\YunShop::app()->uniacid)) {
  82. if (!$this->getResponse('r5_Mp')) {
  83. \Log::debug('汇聚支付回调公众号为空--->', $this->parameter);
  84. echo 'No official account exists.';exit();
  85. }
  86. \Setting::$uniqueAccountId = \YunShop::app()->uniacid =$this->getResponse('r5_Mp');
  87. AccountWechats::setConfig(AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid));
  88. }
  89. $this->log($this->parameter, '微信支付-HJ');
  90. if ($this->getSignResult()) {
  91. if ($_GET['r6_Status'] == '100') {
  92. \Log::debug('------微信支付-HJ 验证成功-----');
  93. if ($_GET['rc_BankCode'] == 'WEIXIN_CARD'){
  94. \Log::debug('------汇聚支付-微信付款码支付');
  95. $data = $this->data('汇聚支付-微信付款码', PayFactory::CONVERGE_WECHAT_CARD_PAY);
  96. }else{
  97. $data = $this->data('微信支付-HJ', '28');
  98. }
  99. $this->payResutl($data);
  100. \Log::debug('----微信支付-HJ 结束----');
  101. echo 'success';
  102. } else {
  103. //其他错误
  104. \Log::debug('------微信支付-HJ 其他错误-----');
  105. echo 'fail';
  106. }
  107. } else {
  108. //签名验证失败
  109. \Log::debug('------微信支付-HJ 签名验证失败-----');
  110. echo 'fail1';
  111. }
  112. }
  113. public function returnUrlWechat()
  114. {
  115. $trade = \Setting::get('shop.trade');
  116. if (!is_null($trade) && isset($trade['redirect_url']) && !empty($trade['redirect_url'])) {
  117. return redirect($trade['redirect_url'])->send();
  118. }
  119. if (0 == $_GET['state'] && $_GET['errorDetail'] == '成功') {
  120. return redirect(Url::absoluteApp('member/payYes', ['i' => $this->getResponse('r5_Mp')]))->send();
  121. } else {
  122. return redirect(Url::absoluteApp('member/payErr', ['i' => $this->getResponse('r5_Mp')]))->send();
  123. }
  124. }
  125. public function notifyUrlAlipay()
  126. {
  127. if (empty(\YunShop::app()->uniacid)) {
  128. if (!$this->getResponse('r5_Mp')) {
  129. \Log::debug('汇聚支付回调公众号为空--->', $this->parameter);
  130. echo 'No official account exists.';exit();
  131. }
  132. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $this->getResponse('r5_Mp');
  133. AccountWechats::setConfig(AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid));
  134. }
  135. $this->log($this->parameter, '支付宝支付-HJ');
  136. if ($this->getSignResult()) {
  137. if ($_GET['r6_Status'] == '100') {
  138. \Log::debug('------支付宝支付-HJ 验证成功-----');
  139. if ($_GET['rc_BankCode'] == 'ALIPAY_CARD'){
  140. \Log::debug('------汇聚支付-支付宝付款码支付');
  141. $data = $this->data('汇聚支付-支付宝付款码', PayFactory::CONVERGE_ALIPAY_CARD_PAY);
  142. }else{
  143. $data = $this->data('支付宝-HJ支付', '29');
  144. }
  145. $this->payResutl($data);
  146. \Log::debug('----支付宝支付-HJ 结束----');
  147. echo 'success';
  148. } else {
  149. //其他错误
  150. \Log::debug('------支付宝支付-HJ 其他错误-----');
  151. echo 'fail';
  152. }
  153. } else {
  154. //签名验证失败
  155. \Log::debug('------支付宝支付-HJ 签名验证失败-----');
  156. echo 'fail1';
  157. }
  158. }
  159. public function returnUrlAlipay()
  160. {
  161. $trade = \Setting::get('shop.trade');
  162. if (!is_null($trade) && isset($trade['redirect_url']) && !empty($trade['redirect_url'])) {
  163. return redirect($trade['redirect_url'])->send();
  164. }
  165. if (0 == $_GET['state'] && $_GET['errorDetail'] == '成功') {
  166. return redirect(Url::absoluteApp('member/payYes', ['i' => $this->getResponse('r5_Mp')]))->send();
  167. } else {
  168. return redirect(Url::absoluteApp('member/payErr', ['i' => $this->getResponse('r5_Mp')]))->send();
  169. }
  170. }
  171. //银联支付回调
  172. public function notifyUrlUnionPay()
  173. {
  174. if (empty(\YunShop::app()->uniacid)) {
  175. if (!$this->getResponse('r5_Mp')) {
  176. \Log::debug('汇聚支付回调公众号为空--->', $this->parameter);
  177. echo 'No official account exists.';exit();
  178. }
  179. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $this->getResponse('r5_Mp');
  180. AccountWechats::setConfig(AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid));
  181. }
  182. $this->log($this->parameter, '汇聚云闪付支付');
  183. if ($this->getSignResult()) {
  184. if ($_GET['r6_Status'] == '100') {
  185. \Log::debug('<------汇聚云闪付支付 业务处理----');
  186. $data = $this->data('汇聚云闪付支付', PayFactory::CONVERGE_UNION_PAY);
  187. $this->payResutl($data);
  188. \Log::debug('----汇聚云闪付支付 处理结束---->');
  189. echo 'success';
  190. } else {
  191. //其他错误
  192. \Log::debug('------汇聚云闪付支付 支付失败-----');
  193. echo 'fail';
  194. }
  195. } else {
  196. //签名验证失败
  197. \Log::debug('------汇聚云闪付支付 签名验证失败-----');
  198. echo 'fail1';
  199. }
  200. }
  201. /**
  202. * 签名验证
  203. *
  204. * @return bool
  205. */
  206. public function getSignResult()
  207. {
  208. $pay = \Setting::get('plugin.convergePay_set');
  209. $notify = new NotifyService();
  210. $notify->setKey($pay['hmacVal']);
  211. return $notify->verifySign();
  212. }
  213. /**
  214. * 支付日志
  215. *
  216. * @param $data
  217. * @param $sign
  218. */
  219. public function log($data, $sign)
  220. {
  221. //访问记录
  222. Pay::payAccessLog();
  223. //保存响应数据
  224. Pay::payResponseDataLog($this->getResponse('r2_OrderNo'), $sign, json_encode($data));
  225. }
  226. /**
  227. * 支付回调参数
  228. *
  229. * @param $pay_type_id
  230. * @return array
  231. */
  232. public function data($pay_type, $pay_type_id)
  233. {
  234. $data = [
  235. 'total_fee' => floatval($this->parameter['r3_Amount']),
  236. 'out_trade_no' => $this->getResponse('r2_OrderNo'),
  237. 'trade_no' => $this->parameter['r7_TrxNo'],
  238. 'unit' => 'yuan',
  239. 'pay_type' => $pay_type,
  240. 'pay_type_id' => $pay_type_id
  241. ];
  242. return $data;
  243. }
  244. /**
  245. * 提现回调
  246. *
  247. */
  248. public function notifyUrlWithdraw()
  249. {
  250. $parameter = request();
  251. \Log::debug('汇聚提现回调参数--', $parameter->input());
  252. //查询提现记录
  253. $withdrawLog = WithdrawLog::where('merchantOrderNo',$parameter->merchantOrderNo)->first();
  254. if (!$withdrawLog) {
  255. echo json_encode([
  256. 'statusCode' => 2002,
  257. 'message' => "汇聚代付记录不存在,单号:{$parameter->merchantOrderNo}",
  258. 'errorCode' => '',
  259. 'errorDesc' => ''
  260. ]);exit();
  261. }
  262. //已提现成功的记录无需再处理
  263. if ($withdrawLog->status == 1) {
  264. echo json_encode([
  265. 'statusCode' => 2001,
  266. 'message' => "成功"
  267. ]);exit();
  268. }
  269. //设置公众号i
  270. if (empty(\YunShop::app()->uniacid)) {
  271. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $withdrawLog->uniacid;
  272. AccountWechats::setConfig(AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid));
  273. }
  274. //访问记录
  275. Pay::payAccessLog();
  276. //保存响应数据
  277. Pay::payResponseDataLog($withdrawLog->withdraw_sn, '汇聚提现回调', $parameter->input());
  278. if ($this->checkWithdrawHmac($parameter)) {
  279. if ($parameter->status == '205') {
  280. \Log::debug('------汇聚打款 开始-----'.$withdrawLog->withdraw_type);
  281. if ($withdrawLog->withdraw_type == 1) {
  282. //兼容供应商提现
  283. if ( app('plugins')->isEnabled('supplier')) {
  284. $supplierWithdraw = \Yunshop\Supplier\common\models\SupplierWithdraw::where('apply_sn', $withdrawLog->withdraw_sn)->where('status', 4)->first();
  285. if ($supplierWithdraw) {
  286. $supplierWithdraw->status = 3;
  287. $supplierWithdraw->pay_time = time();
  288. $supplierWithdraw->save();
  289. }
  290. }
  291. } else {
  292. event(new WithdrawSuccessEvent($withdrawLog->withdraw_sn));
  293. }
  294. \Log::debug('----汇聚打款 结束----');
  295. $withdrawLog->status = 1;
  296. $withdrawLog->response_data = $parameter->input();
  297. $withdrawLog->save();
  298. echo json_encode([
  299. 'statusCode' => 2001,
  300. 'message' => "成功"
  301. ]);exit();
  302. }
  303. \Log::debug('------汇聚打款失败---- ', $parameter->input());
  304. if ( in_array($parameter->input('status'), ['204', '208','214'])) {
  305. $withdrawLog->status = -1;
  306. $withdrawLog->desc = $parameter->input('errorCodeDesc');
  307. $withdrawLog->response_data = $parameter->input();
  308. $withdrawLog->save();
  309. echo json_encode([
  310. 'statusCode' => 2002,
  311. 'message' => "受理失败",
  312. 'errorCode' => $parameter->errorCode,
  313. 'errorDesc' => $parameter->errorCodeDesc
  314. ]);exit();
  315. }
  316. } else {
  317. //签名验证失败
  318. \Log::debug('------汇聚打款 签名验签失败-----');
  319. echo json_encode([
  320. 'statusCode' => 2002,
  321. 'message' => "签名验签失败",
  322. 'errorCode' => '300002017',
  323. 'errorDesc' => '签名验签失败'
  324. ]);exit();
  325. }
  326. }
  327. /**
  328. * 验证提现签名
  329. *
  330. * @param $parameter
  331. * @return bool
  332. */
  333. public function checkWithdrawHmac($parameter)
  334. {
  335. $setting = \Setting::get('plugin.convergePay_set');
  336. $verify = $parameter->hmac == md5($parameter->status . $parameter->errorCode . $parameter->errorCodeDesc . $parameter->userNo
  337. . $parameter->merchantOrderNo . $parameter->platformSerialNo . $parameter->receiverAccountNoEnc
  338. . $parameter->receiverNameEnc . sprintf("%.2f", $parameter->paidAmount) . sprintf("%.2f", $parameter->fee) . $setting['hmacVal']);
  339. \Log::debug('---汇聚打款签名验证--->', [$verify]);
  340. return $verify;
  341. }
  342. /**
  343. * 微信或支付宝退款
  344. */
  345. public function refundUrlWechat()
  346. {
  347. $this->logRefund($this->parameter, '微信或支付宝退款-HJ');
  348. if ($this->getSignWechatResult()) {
  349. if ($this->parameter['ra_Status'] == '100') {
  350. \Log::debug('------微信或支付宝退款-HJ 验证成功-----');
  351. \Log::debug('----微信或支付宝退款-HJ 结束----');
  352. } else {
  353. //其他错误
  354. \Log::debug('------微信或支付宝退款-HJ 其他错误-----');
  355. }
  356. } else {
  357. //签名验证失败
  358. \Log::debug('------微信或支付宝退款-HJ 签名验证失败-----');
  359. }
  360. echo 'success';
  361. }
  362. /**
  363. * 汇聚-微信或支付宝退款 签名验证
  364. *
  365. * @return bool
  366. */
  367. public function getSignWechatResult()
  368. {
  369. $pay = \Setting::get('plugin.convergePay_set');
  370. \Log::debug('--汇聚-微信或支付宝退款签名验证参数--' . $this->parameter['r1_MerchantNo'] . $this->parameter['r2_OrderNo']
  371. . $this->parameter['r3_RefundOrderNo'] . $this->parameter['r4_RefundAmount_str'] . $this->parameter['r5_RefundTrxNo']
  372. . $this->parameter['ra_Status'] . $pay['hmacVal']);
  373. return $this->parameter['hmac'] == md5($this->parameter['r1_MerchantNo'] . $this->parameter['r2_OrderNo']
  374. . $this->parameter['r3_RefundOrderNo'] . $this->parameter['r4_RefundAmount_str'] . $this->parameter['r5_RefundTrxNo']
  375. . $this->parameter['ra_Status'] . $pay['hmacVal']);
  376. }
  377. /**
  378. * 支付日志
  379. *
  380. * @param $data
  381. * @param $sign
  382. */
  383. public function logRefund($data, $sign)
  384. {
  385. //访问记录
  386. Pay::payAccessLog();
  387. //保存响应数据
  388. Pay::payResponseDataLog($data['r2_OrderNo'], $sign, json_encode($data));
  389. }
  390. /**
  391. * 获取参数值
  392. * @param string $key
  393. * @return string
  394. */
  395. public function getResponse($key)
  396. {
  397. //todo 兼容以前判断
  398. if ($key == 'r2_OrderNo' && strpos($this->parameter['r2_OrderNo'], ':') !== false) {
  399. $attach = explode(':', $_GET['r2_OrderNo']);
  400. return $attach[0];
  401. }
  402. return array_get($this->parameter, $key, '');
  403. }
  404. }