PaymentService.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\AccountLogModel;
  13. use App\Models\MemberModel;
  14. use App\Models\OrderModel;
  15. use App\Models\PaymentModel;
  16. use App\Models\PayOrdersModel;
  17. use App\Services\Api\SettleService;
  18. use Illuminate\Support\Facades\DB;
  19. use Yansongda\Pay\Pay;
  20. use Yansongda\Pay\Provider\Wechat;
  21. /**
  22. * 支付-服务类
  23. * @author laravel开发员
  24. * @since 2020/11/11
  25. * Class PaymentService
  26. * @package App\Services\Api
  27. */
  28. class PaymentService extends BaseService
  29. {
  30. protected static $instance = null;
  31. private $config = [];
  32. /**
  33. * 构造函数
  34. * @author laravel开发员
  35. * @since 2020/11/11
  36. * PaymentService constructor.
  37. */
  38. public function __construct()
  39. {
  40. $this->model = new PaymentModel();
  41. }
  42. /**
  43. * 静态入口
  44. * @return static|null
  45. */
  46. public static function make()
  47. {
  48. if (!self::$instance) {
  49. self::$instance = (new static());
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * 创建支付
  55. * @param string $scene 场景,store-购物消费,pay-生活充值,refund-退款
  56. * @param int $payType
  57. * @param int $isMin 是否是小程序
  58. * @return false|\Yansongda\Pay\Provider\Alipay|Wechat
  59. */
  60. public function createPay($scene, $payType = 10, $payPt = '')
  61. {
  62. $config = ConfigService::make()->getConfigOptionByGroup(6);
  63. if ($payType == 10) {
  64. $appid = isset($config['wxpay_appid']) ? $config['wxpay_appid'] : '';
  65. $mpAppid = isset($config['wxpay_mp_appid']) ? $config['wxpay_mp_appid'] : '';
  66. $minAppid = isset($config['wxpay_min_appid']) ? $config['wxpay_min_appid'] : '';
  67. $mchid = isset($config['wxpay_mchd']) ? $config['wxpay_mchd'] : '';
  68. $secretV3Key = isset($config['wxpay_key_v3']) ? $config['wxpay_key_v3'] : '';
  69. $secretV2Key = isset($config['wxpay_key_v2']) ? $config['wxpay_key_v2'] : '';
  70. $wxpaySecretCert = isset($config['wxpay_secret_cert']) ? $config['wxpay_secret_cert'] : '';
  71. $wxpayPublicCert = isset($config['wxpay_public_cert']) ? $config['wxpay_public_cert'] : '';
  72. if (empty($appid) || empty($mchid) || empty($secretV3Key)) {
  73. $this->error = 2616;
  74. return false;
  75. }
  76. // 支付参数
  77. $payConfig = config('payment.wechat');
  78. $payConfig['wechat']['default']['mch_id'] = $mchid;
  79. if ($payPt == 'min') {
  80. // 小程序支付
  81. $payConfig['wechat']['default']['mini_app_id'] = $minAppid ? $minAppid : $appid;
  82. } else if ($payPt == 'mp') {
  83. // 公众号
  84. $payConfig['wechat']['default']['mp_app_id'] = $mpAppid ? $mpAppid : $appid;
  85. } else {
  86. // APP支付
  87. $payConfig['wechat']['default']['app_id'] = $appid;
  88. }
  89. if ($secretV3Key) {
  90. $payConfig['wechat']['default']['mch_secret_key'] = $secretV3Key;
  91. } else if ($secretV2Key) {
  92. $payConfig['wechat']['default']['mch_secret_key_v2'] = $secretV2Key;
  93. }
  94. if ($wxpaySecretCert) {
  95. $payConfig['wechat']['default']['mch_secret_cert'] = $wxpaySecretCert;
  96. }
  97. if ($wxpayPublicCert) {
  98. $payConfig['wechat']['default']['mch_public_cert_path'] = $wxpayPublicCert;
  99. }
  100. //$payConfig['wechat']['default']['notify_url'] = url('/api/notify/' . $scene . '/10');
  101. $payConfig['wechat']['default']['notify_url'] = url(env('APP_URL') . 'api/notify/' . $scene . '/10');
  102. $this->config = $payConfig;
  103. //var_dump($payConfig);
  104. return Pay::wechat($payConfig);
  105. } else if ($payType == 20) {
  106. $appid = isset($config['alipay_appid']) ? $config['alipay_appid'] : '';
  107. $appSecretCert = isset($config['alipay_secret_cert']) ? $config['alipay_secret_cert'] : '';
  108. $appPublicCert = isset($config['alipay_app_public_cert_path']) ? $config['alipay_app_public_cert_path'] : '';
  109. $alipayPublicCert = isset($config['alipay_public_cert_path']) ? $config['alipay_public_cert_path'] : '';
  110. $alipayRootCert = isset($config['alipay_root_cert_path']) ? $config['alipay_root_cert_path'] : '';
  111. if (empty($appid) || empty($appSecretCert)) {
  112. $this->error = 2619;
  113. return false;
  114. }
  115. // 支付参数
  116. $payConfig = config('payment.alipay');
  117. $payConfig['alipay']['default']['app_id'] = $appid;
  118. $payConfig['alipay']['default']['app_secret_cert'] = $appSecretCert;
  119. if ($appPublicCert) {
  120. $payConfig['alipay']['default']['app_public_cert_path'] = $appPublicCert;
  121. }
  122. if ($alipayPublicCert) {
  123. $payConfig['alipay']['default']['alipay_public_cert_path'] = $alipayPublicCert;
  124. }
  125. if ($alipayRootCert) {
  126. $payConfig['alipay']['default']['alipay_root_cert_path'] = $alipayRootCert;
  127. }
  128. $payConfig['alipay']['default']['notify_url'] = url('/api/notify/' . $scene . '/20');
  129. $this->config = $payConfig;
  130. return Pay::alipay($payConfig);
  131. } else if ($payType == 30) {
  132. return true;
  133. }
  134. return false;
  135. }
  136. /**
  137. * 微信小程序支付
  138. * @param $userInfo
  139. * @param $order
  140. * @param string $scene
  141. * @return false|\Yansongda\Supports\Collection
  142. */
  143. public function minPay($userInfo, $order, $scene = 'store')
  144. {
  145. $amount = isset($order['pay_money']) ? $order['pay_money'] : 0;
  146. $openid = isset($order['openid']) ? $order['openid'] : '';
  147. if ($amount < 0) {
  148. $this->error = 2615;
  149. return false;
  150. }
  151. if (empty($openid)) {
  152. $this->error = 2614;
  153. return false;
  154. }
  155. $outTradeNo = isset($order['order_no']) && $order['order_no'] ? $order['order_no'] : get_order_num('PR');
  156. // 是否调用过支付,是则用新的支付单号
  157. if ($outTradeNo && $this->model->where(['out_trade_no' => $outTradeNo, 'mark' => 1])->value('id')) {
  158. $outTradeNo = $outTradeNo . date('is') . rand(1, 9);
  159. }
  160. $body = isset($order['body']) ? $order['body'] : '';
  161. $payData = [
  162. 'out_trade_no' => $outTradeNo,
  163. 'description' => $body ? $body : '订单支付',
  164. 'amount' => [
  165. 'total' => intval($amount * 1000/10),
  166. 'currency' => 'CNY'
  167. ],
  168. 'payer' => [
  169. 'openid' => $openid,
  170. ],
  171. ];
  172. // 创建支付
  173. try {
  174. $pay = $this->createPay($scene, 10, 'min');
  175. RedisService::set("caches:payments:wechat:minPay_{$scene}_{$outTradeNo}", ['order' => $order, 'config' => $this->config], 7200);
  176. if (empty($pay)) {
  177. $this->error = 2616;
  178. return false;
  179. }
  180. $pay = $pay->mini($payData);
  181. } catch (\Exception $exception) {
  182. RedisService::set("caches:payments:wechat:minPay_{$scene}_{$outTradeNo}_error", ['order' => $order, 'error' => $exception->getTrace(), 'config' => $this->config], 7200);
  183. $this->error = $exception->getMessage();
  184. return false;
  185. }
  186. if ($pay->package) {
  187. $data = [
  188. 'user_id' => $userInfo['id'],
  189. 'out_trade_no' => $outTradeNo,
  190. 'order_no' => $order['order_no'],
  191. 'params' => json_encode($pay, 256),
  192. 'total_fee' => $amount,
  193. 'pay_type' => 10,
  194. 'create_time' => time(),
  195. 'status' => 2,
  196. 'mark' => 1,
  197. ];
  198. if ($this->model->insertGetId($data)) {
  199. $this->error = 2617;
  200. return $pay;
  201. }
  202. }
  203. $this->error = 2618;
  204. return false;
  205. }
  206. /**
  207. * 微信公众号支付
  208. * @param $userInfo
  209. * @param $order
  210. * @param string $scene
  211. * @return false|\Yansongda\Supports\Collection
  212. */
  213. public function mpPay($userInfo, $order, $scene = 'store')
  214. {
  215. $amount = isset($order['pay_money']) ? $order['pay_money'] : 0;
  216. $openid = isset($order['openid']) ? $order['openid'] : '';
  217. if ($amount < 0) {
  218. $this->error = 2615;
  219. return false;
  220. }
  221. if (empty($openid)) {
  222. $this->error = 2614;
  223. return false;
  224. }
  225. $outTradeNo = isset($order['order_no']) && $order['order_no'] ? $order['order_no'] : get_order_num('PR');
  226. // 是否调用过支付,是则用新的支付单号
  227. if ($outTradeNo && $this->model->where(['out_trade_no' => $outTradeNo, 'mark' => 1])->value('id')) {
  228. $outTradeNo = $outTradeNo . date('is') . rand(1, 9);
  229. }
  230. $body = isset($order['body']) ? $order['body'] : '';
  231. $payData = [
  232. 'out_trade_no' => $outTradeNo,
  233. 'description' => $body ? $body : '订单支付',
  234. 'amount' => [
  235. 'total' => intval($amount * 1000/10),
  236. 'currency' => 'CNY'
  237. ],
  238. 'payer' => [
  239. 'openid' => $openid,
  240. ],
  241. ];
  242. // 创建支付
  243. try {
  244. $pay = $this->createPay($scene, 10, 'mp');
  245. RedisService::set("caches:payments:wechat:mp_{$scene}_{$outTradeNo}", ['order' => $order, 'config' => $this->config], 7200);
  246. if (empty($pay)) {
  247. $this->error = 2616;
  248. return false;
  249. }
  250. $pay = $pay->mp($payData);
  251. } catch (\Exception $exception) {
  252. RedisService::set("caches:payments:wechat:mp_{$scene}_{$outTradeNo}_error", ['order' => $order, 'error' => $exception->getTrace(), 'config' => $this->config], 7200);
  253. $this->error = $exception->getMessage();
  254. return false;
  255. }
  256. if ($pay->package) {
  257. $data = [
  258. 'user_id' => $userInfo['id'],
  259. 'out_trade_no' => $outTradeNo,
  260. 'order_no' => $order['order_no'],
  261. 'params' => json_encode($pay, 256),
  262. 'total_fee' => $amount,
  263. 'pay_type' => 10,
  264. 'create_time' => time(),
  265. 'status' => 2,
  266. 'mark' => 1,
  267. ];
  268. if ($this->model->insertGetId($data)) {
  269. $this->error = 2617;
  270. return $pay;
  271. }
  272. }
  273. $this->error = 2618;
  274. return false;
  275. }
  276. /**
  277. * 微信APP支付
  278. * @param $userInfo
  279. * @param $order
  280. * @param string $scene
  281. * @return false|\Yansongda\Supports\Collection
  282. */
  283. public function wechatPay($userInfo, $order, $scene = 'store')
  284. {
  285. $amount = isset($order['pay_money']) ? $order['pay_money'] : 0;
  286. if ($amount < 0) {
  287. $this->error = 2615;
  288. return false;
  289. }
  290. $outTradeNo = isset($order['order_no']) && $order['order_no'] ? $order['order_no'] : get_order_num('PR');
  291. // 是否调用过支付,是则用新的支付单号
  292. if ($outTradeNo && $this->model->where(['out_trade_no' => $outTradeNo, 'mark' => 1])->value('id')) {
  293. $outTradeNo = $outTradeNo . date('is') . rand(1, 9);
  294. }
  295. $body = isset($order['body']) ? $order['body'] : '';
  296. $type = isset($order['type']) ? $order['type'] : 0;
  297. $payData = [
  298. 'out_trade_no' => $outTradeNo,
  299. 'attach' => "order-{$type}",
  300. 'description' => $body ? $body : '订单支付',
  301. 'amount' => [
  302. 'total' => intval($amount * 1000/10),
  303. 'currency' => 'CNY'
  304. ],
  305. ];
  306. // 创建支付
  307. try {
  308. $pay = $this->createPay($scene, 10);
  309. RedisService::set("caches:payments:wechat:{$scene}_{$outTradeNo}", ['order' => $order, 'config' => $this->config], 7200);
  310. if (empty($pay)) {
  311. $this->error = 2616;
  312. return false;
  313. }
  314. $pay = $pay->app($payData);
  315. } catch (\Exception $exception) {
  316. RedisService::set("caches:payments:wechat:{$scene}_{$outTradeNo}_error", ['order' => $order, 'error' => $exception->getTrace(), 'config' => $this->config], 7200);
  317. $this->error = $exception->getMessage();
  318. return false;
  319. }
  320. if ($pay->prepayid) {
  321. $data = [
  322. 'user_id' => $userInfo['id'],
  323. 'out_trade_no' => $outTradeNo,
  324. 'order_no' => $order['order_no'],
  325. 'params' => json_encode($pay, 256),
  326. 'total_fee' => $amount,
  327. 'pay_type' => 10,
  328. 'create_time' => time(),
  329. 'status' => 2,
  330. 'mark' => 1,
  331. ];
  332. if ($this->model->insertGetId($data)) {
  333. $this->error = 2617;
  334. return $pay;
  335. }
  336. }
  337. $this->error = 2618;
  338. return false;
  339. }
  340. /**
  341. * 支付宝支付
  342. * @param $userInfo
  343. * @param $order
  344. * @return bool
  345. */
  346. public function aliPay($userInfo, $order, $scene = 'shop')
  347. {
  348. $amount = isset($order['pay_money']) ? $order['pay_money'] : 0;
  349. if ($amount < 0) {
  350. $this->error = 2615;
  351. return false;
  352. }
  353. // 是否调用过支付,是则用新的支付单号
  354. $outTradeNo = isset($order['order_no']) && $order['order_no'] ? $order['order_no'] : get_order_num('PY');
  355. if ($outTradeNo && $this->model->where(['out_trade_no' => $outTradeNo, 'mark' => 1])->value('id')) {
  356. $outTradeNo = $outTradeNo . date('is') . rand(1, 9);
  357. }
  358. $body = isset($order['body']) ? $order['body'] : '';
  359. $payData = [
  360. 'out_trade_no' => $outTradeNo,
  361. 'subject' => $body ? $body : '订单支付',
  362. 'total_amount' => $amount,
  363. ];
  364. // 创建支付
  365. $pay = $this->createPay($scene, 20);
  366. RedisService::set("caches:payments:alipay:{$scene}_{$outTradeNo}", ['order' => $order, 'config' => $this->config], 7200);
  367. if (empty($pay)) {
  368. $this->error = 2619;
  369. return false;
  370. }
  371. $pay = $pay->app($payData);
  372. if ($pay->getStatusCode() == 200) {
  373. $data = [
  374. 'user_id' => $userInfo['id'],
  375. 'out_trade_no' => $outTradeNo,
  376. 'order_no' => $order['order_no'],
  377. 'params' => json_encode($pay, 256),
  378. 'total_fee' => $amount,
  379. 'pay_type' => 20,
  380. 'create_time' => time(),
  381. 'status' => 2,
  382. 'mark' => 1,
  383. ];
  384. if ($this->model->insertGetId($data)) {
  385. $this->error = 2620;
  386. return $pay->getBody()->getContents();
  387. }
  388. }
  389. $this->error = 2621;
  390. return false;
  391. }
  392. /**
  393. * 订单支付回调处理
  394. * @param string $scene 场景 store-购物消费,pay-充值,refund-退款
  395. * @param int $payType 支付方式,10-微信支付,20-支付宝支付
  396. * @param array $data 回调数据
  397. * @return bool
  398. */
  399. public function catchNotify($scene, $payType, $data)
  400. {
  401. $outTradeNo = '';
  402. $payTotal = 0;
  403. $transactionId = '';
  404. $payAt = '';
  405. $notifyData = [];
  406. try {
  407. // 微信支付
  408. if ($payType == 10) {
  409. $resource = isset($data['resource']) ? $data['resource'] : [];
  410. $ciphertext = isset($resource['ciphertext']) ? $resource['ciphertext'] : [];
  411. $tradeStatus = isset($ciphertext['trade_state']) ? $ciphertext['trade_state'] : '';
  412. if ($tradeStatus != 'SUCCESS') {
  413. $this->error = 2622;
  414. return false;
  415. }
  416. $outTradeNo = isset($ciphertext['out_trade_no']) ? $ciphertext['out_trade_no'] : '';
  417. $transactionId = isset($ciphertext['transaction_id']) ? $ciphertext['transaction_id'] : '';
  418. if (empty($outTradeNo)) {
  419. $this->error = 2623;
  420. return false;
  421. }
  422. $payAt = isset($ciphertext['success_time']) ? date('Y-m-d H:i:s', strtotime($ciphertext['success_time'])) : date('Y-m-d H:i:s');
  423. $amount = isset($ciphertext['amount']) ? $ciphertext['amount'] : [];
  424. $payTotal = isset($amount['total']) ? moneyFormat($amount['total'] / 100, 3) : 0;
  425. $notifyData = $ciphertext;
  426. if ($payTotal <= 0) {
  427. $this->error = 2624;
  428. return false;
  429. }
  430. } // 支付宝支付
  431. else if ($payType == 20) {
  432. // TRADE_SUCCESS
  433. $tradeStatus = isset($data['trade_status']) ? $data['trade_status'] : '';
  434. if ($tradeStatus != 'TRADE_SUCCESS' && $tradeStatus != 'TRADE_FINISHED') {
  435. $this->error = 2622;
  436. return false;
  437. }
  438. $outTradeNo = isset($data['out_trade_no']) ? $data['out_trade_no'] : '';
  439. if (empty($outTradeNo)) {
  440. $this->error = 2623;
  441. return false;
  442. }
  443. $payTotal = isset($data['total_amount']) ? floatval($data['total_amount']) : 0;
  444. $transactionId = isset($data['trade_no']) ? trim($data['trade_no']) : '';
  445. $payAt = isset($data['send_pay_date']) ? trim($data['send_pay_date']) : date('Y-m-d H:i:s');
  446. $notifyData = $data;
  447. if ($payTotal <= 0) {
  448. $this->error = 2624;
  449. return false;
  450. }
  451. }
  452. // 支付信息
  453. $paymentInfo = $this->model->with(['user'])->where(['out_trade_no' => $outTradeNo, 'mark' => 1])
  454. ->select(['user_id', 'order_no', 'pay_type', 'total_fee', 'status'])
  455. ->first();
  456. $status = isset($paymentInfo['status']) ? $paymentInfo['status'] : 0;
  457. $totalFee = isset($paymentInfo['total_fee']) ? $paymentInfo['total_fee'] : 0;
  458. $paymentPayType = isset($paymentInfo['pay_type']) ? $paymentInfo['pay_type'] : 0;
  459. $orderNo = isset($paymentInfo['order_no']) ? $paymentInfo['order_no'] : '';
  460. $payUserId = isset($paymentInfo['user_id']) ? $paymentInfo['user_id'] : 0;
  461. if (empty($paymentInfo) || empty($orderNo) || $payUserId <= 0) {
  462. $this->error = 2625;
  463. return false;
  464. }
  465. // 验证支付状态
  466. if ($status == 1) {
  467. $this->error = 2626;
  468. return false;
  469. }
  470. // 验证支付方式
  471. if ($paymentPayType != $payType) {
  472. $this->error = 2627;
  473. return false;
  474. }
  475. if ($payTotal != $totalFee || $payTotal <= 0) {
  476. $this->error = 2628;
  477. return false;
  478. }
  479. // 删除久远旧记录
  480. $this->model->where(['mark' => 1])->where('create_time', '<=', time() - 60 * 86400)->delete();
  481. // 更新订单数据
  482. DB::beginTransaction();
  483. $updateData = ['transaction_id' => $transactionId, 'result' => json_encode($notifyData, 256), 'pay_at' => $payAt, 'status' => 1, 'update_time' => time()];
  484. if (!$this->model->where(['out_trade_no' => $outTradeNo, 'mark' => 1])->update($updateData)) {
  485. $this->error = 2632;
  486. DB::rollBack();
  487. return false;
  488. }
  489. /* TODO 订单验证和状态处理 */
  490. $orderInfo = [];
  491. // 商城订单支付
  492. if ($scene == 'store') {
  493. $orderInfo = OrderModel::with(['user'])->where(['order_no' => $orderNo, 'mark' => 1])
  494. ->select(['id as order_id', 'user_id', 'order_no', 'total as pay_money', 'pay_at as pay_time', 'remark', 'status'])
  495. ->first();
  496. $orderStatus = isset($orderInfo['status']) ? $orderInfo['status'] : 0;
  497. // 验证订单
  498. if (empty($orderInfo)) {
  499. DB::rollBack();
  500. $this->error = 2629;
  501. return false;
  502. }
  503. // 订单状态
  504. if ($orderStatus != 1) {
  505. DB::rollBack();
  506. $this->error = 2630;
  507. return false;
  508. }
  509. $updateData = ['pay_at' => $payAt, 'transaction_id' => $transactionId, 'status' => 2, 'update_time' => time()];
  510. if (!OrderModel::where(['order_no' => $orderNo, 'mark' => 1])->update($updateData)) {
  511. $this->error = 2633;
  512. DB::rollBack();
  513. return false;
  514. }
  515. }
  516. // 生活充值
  517. else if ($scene == 'pay') {
  518. $orderInfo = PayOrdersModel::with(['meal'])->where(['order_no' => $orderNo, 'mark' => 1])
  519. ->select(['id as order_id', 'user_id','meal_id','product_id', 'order_no','transaction_id','account', 'total as pay_money','area','ytype','city','id_card_no','pay_total', 'pay_at as pay_time', 'remark', 'status'])
  520. ->first();
  521. $orderStatus = isset($orderInfo['status']) ? $orderInfo['status'] : 0;
  522. // 验证订单
  523. if (empty($orderInfo)) {
  524. DB::rollBack();
  525. $this->error = 2629;
  526. return false;
  527. }
  528. // 订单状态
  529. if ($orderStatus != 1) {
  530. DB::rollBack();
  531. $this->error = 2630;
  532. return false;
  533. }
  534. $updateData = ['pay_at' => $payAt, 'transaction_id' => $transactionId, 'status' => 2, 'update_time' => time()];
  535. if (!PayOrdersModel::where(['order_no' => $orderNo, 'mark' => 1])->update($updateData)) {
  536. $this->error = 2633;
  537. DB::rollBack();
  538. return false;
  539. }
  540. }
  541. // 退款
  542. else if ($scene == 'refund') {
  543. $orderInfo = OrderModel::where(['order_no' => $orderNo, 'mark' => 1])
  544. ->select(['id as order_id', 'user_id', 'order_no', 'total as pay_money', 'remark', 'refund_remark', 'pay_at as pay_time', 'refund_status as status'])
  545. ->first();
  546. $refundRemark = isset($orderInfo['refund_remark']) ? $orderInfo['refund_remark'] : 'refund_remark';
  547. $orderStatus = isset($orderInfo['status']) ? $orderInfo['status'] : 0;
  548. // 验证订单
  549. if (empty($orderInfo)) {
  550. DB::rollBack();
  551. $this->error = 2629;
  552. return false;
  553. }
  554. // 订单状态
  555. if ($orderStatus != 2) {
  556. DB::rollBack();
  557. $this->error = 2639;
  558. return false;
  559. }
  560. // 订单打款状态
  561. if ($orderStatus == 1) {
  562. DB::rollBack();
  563. $this->error = 2630;
  564. return false;
  565. }
  566. $updateData = ['refund_status' => 1, 'refund_remark' => $refundRemark ? $refundRemark . ' 已退款成功' : '已退款成功', 'update_time' => time()];
  567. if (!OrderModel::where(['order_no' => $orderNo, 'mark' => 1])->update($updateData)) {
  568. $this->error = 2633;
  569. DB::rollBack();
  570. return false;
  571. }
  572. }
  573. // TODO 场景业务回调处理
  574. $orderUserId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
  575. $this->saveLog("caches:payments:notify_{$scene}:catch_{$orderNo}_{$orderUserId}", ['order' => $orderInfo, 'notify' => $data]);
  576. switch ($scene) {
  577. case 'store': //
  578. break;
  579. case 'pay': //
  580. // 调起 接口充值
  581. $orderId = isset($orderInfo['order_id'])? $orderInfo['order_id'] : 0;
  582. $productId = isset($orderInfo['product_id'])? $orderInfo['product_id'] : '';
  583. $orderNo = isset($orderInfo['order_no'])? $orderInfo['order_no'] : '';
  584. $account = isset($orderInfo['account'])? $orderInfo['account'] : '';
  585. $total = isset($orderInfo['pay_money'])? $orderInfo['pay_money'] : 0;
  586. $payTotal = isset($orderInfo['pay_total'])? $orderInfo['pay_total'] : 0;
  587. $mealData = isset($orderInfo['meal'])? $orderInfo['meal'] : [];
  588. $orderType = isset($orderInfo['type'])?$orderInfo['type']:1;
  589. $electricType = isset($mealData['electric_type'])?$mealData['electric_type']:0;
  590. $city = isset($orderInfo['city'])?$orderInfo['city']:'';
  591. $amount = $total;
  592. $params = [
  593. 'price'=>$payTotal,
  594. 'ytype'=>isset($orderInfo['ytype'])?$orderInfo['ytype']:0,
  595. 'id_card_no'=>isset($orderInfo['id_card_no'])?$orderInfo['id_card_no']:'',
  596. 'area'=>isset($orderInfo['area'])?$orderInfo['area']:'',
  597. ];
  598. if($orderType==2 && $electricType!= 3 && $city){
  599. $params['city'] = $city;
  600. }
  601. $result = DyrPayService::make()->recharge($orderNo, $account, $productId, $amount, $params);
  602. $errno = isset($result['errno'])?$result['errno'] : -1;
  603. $errmsg = isset($result['errmsg'])?$result['errmsg'] : '';
  604. $res = isset($result['data'])?$result['data']:[];
  605. $order_number = isset($res['order_number'])?$res['order_number']:'';
  606. if(empty($result) || $errno != 0 || empty($order_number)){
  607. $this->error = '充值调用失败:'.$errmsg;
  608. // 退款
  609. $orderInfo['money'] = $payTotal;
  610. $orderInfo['transaction_id'] = $transactionId;
  611. $orderInfo['remark'] = '充值失败退款';
  612. $refundStatus = PaymentService::make()->refund($orderInfo,'pay');
  613. $updateData = ['refund_status'=> $refundStatus?1:3,'status'=>5,'transaction_id'=>$transactionId,'pay_at'=>$payAt,'result'=>json_encode(['amount'=>$amount,'params'=>$params,'msg'=>$this->error,'result'=>$result],256),'failed_remark'=> $errmsg,'refund_at'=>date('Y-m-d H:i:s'),'refund_money'=>$refundStatus?$payTotal:0,'update_time'=>time()];
  614. $this->saveLog("caches:payments:notify_{$scene}:pay_failed_{$orderNo}_{$orderUserId}", ['error'=>$this->error,'update'=>$updateData,'order' => $orderInfo,'result'=>$result, 'notify' => $data]);
  615. if(!PayOrdersModel::where(['id'=>$orderId])->update($updateData)){
  616. $this->error = '订单退款状态更新错误~';
  617. }
  618. DB::commit();
  619. return false;
  620. }
  621. $updateData = ['status'=>3,'result'=>json_encode(['amount'=>$amount,'params'=>$params,'result'=>$result],256),'out_trade_no'=>$order_number,'update_time'=>time()+1];
  622. $this->saveLog("caches:payments:notify_{$scene}:pay_success_{$orderNo}_{$orderUserId}", ['error'=> '充值调用成功','id'=>$orderId,'update'=>$updateData,'order' => $orderInfo,'result'=>$result, 'notify' => $data]);
  623. if(!PayOrdersModel::where(['id'=>$orderId])->update($updateData)){
  624. DB::commit();
  625. $this->error = '更新充值订单失败';
  626. $this->saveLog("caches:payments:notify_{$scene}:pay_failed_{$orderNo}_{$orderUserId}", ['error'=>$this->error,'update'=>$updateData,'order' => $orderInfo,'result'=>$result, 'notify' => $data]);
  627. return false;
  628. }
  629. DB::commit();
  630. return true;
  631. default:
  632. break;
  633. }
  634. $this->error = '回调处理成功';
  635. DB::commit();
  636. return true;
  637. } catch (\Exception $exception) {
  638. $this->error = $exception->getMessage();
  639. RedisService::set("caches:payments:notify_{$scene}:catch_" . $orderNo . '_error', ['notify' => $data, 'error' => $exception->getMessage(), 'trace' => $exception->getTrace()], 7200);
  640. return false;
  641. }
  642. }
  643. /**
  644. * 充值回调
  645. * @param $productId
  646. * @param $data
  647. * @return bool
  648. * @throws \Yansongda\Pay\Exception\ContainerException
  649. * @throws \Yansongda\Pay\Exception\InvalidParamsException
  650. * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
  651. */
  652. public function catchPayNotify($productId, $data)
  653. {
  654. $out_trade_num = isset($data['out_trade_num'])?$data['out_trade_num']:'';
  655. $state = isset($data['state'])? $data['state'] : 0;
  656. $charge_kami = isset($data['charge_kami'])?$data['charge_kami'] : '';
  657. $remark = isset($data['remark'])?$data['remark'] : '';
  658. $amount = isset($data['charge_amount'])?floatval($data['charge_amount']) : 0;
  659. if($state==0){
  660. $this->error = '充值处理中~';
  661. return false;
  662. }
  663. $orderInfo = PayOrdersModel::with(['user'])->where(['order_no'=>$out_trade_num,'mark'=>1])->first();
  664. $orderId = isset($orderInfo['id'])? $orderInfo['id'] : 0;
  665. $orderUserId = isset($orderInfo['user_id'])? $orderInfo['user_id'] : 0;
  666. $status = isset($orderInfo['status'])? $orderInfo['status'] : 0;
  667. $total = isset($orderInfo['pay_total'])? $orderInfo['pay_total'] : 0;
  668. $recBonusId = isset($orderInfo['rec_bonus_id'])?$orderInfo['rec_bonus_id'] : 0;
  669. $recBonus = isset($orderInfo['rec_bonus'])?$orderInfo['rec_bonus'] : 0;
  670. if(empty($orderInfo) || $orderId<=0 || $total<=0){
  671. $this->error = '充值订单不存在';
  672. $this->saveLog("caches:payments:payNotify_{$productId}:notify_{$out_trade_num}_error", ['error'=>$this->error,'order' => $orderInfo, 'notify' => $data]);
  673. return false;
  674. }
  675. if($status!=3){
  676. $this->error = '充值订单状态错误,状态非充值中';
  677. $this->saveLog("caches:payments:payNotify_{$productId}:notify_{$out_trade_num}_error", ['error'=>$this->error,'order' => $orderInfo, 'notify' => $data]);
  678. return false;
  679. }
  680. // 成功
  681. $status = 4;
  682. $refundAmount = 0;
  683. if($state == 1){
  684. $this->error = '充值成功~';
  685. }
  686. //部分成功,退部分
  687. else if($state == 3) {
  688. $status = 6;
  689. $refundAmount = moneyFormat($total - $amount,2);
  690. $this->error = '充值成功部分退款';
  691. }
  692. //失败,退全款
  693. else if(in_array($state,[-1,2])){
  694. $status = 5;
  695. $refundAmount = $total;
  696. $this->error = '充值失败退款';
  697. }
  698. // 更新订单状态
  699. DB::beginTransaction();
  700. $this->saveLog("caches:payments:payNotify_{$productId}:notify_{$out_trade_num}", ['error'=>$this->error,'order' => $orderInfo, 'notify' => $data]);
  701. if(!PayOrdersModel::where(['id'=>$orderId])->update(['status'=>$status,'charge_kami'=>$charge_kami,'refund_status'=>$status!=4?2:0,'charge_amount'=>$amount,'update_time'=>time()])){
  702. $this->error = '更新订单状态错误~';
  703. DB::rollBack();
  704. return false;
  705. }
  706. // 是否要退款
  707. if($refundAmount>0){
  708. $orderInfo['money'] = $refundAmount;
  709. $orderInfo['remark'] = $this->error;
  710. $orderInfo['out_trade_no'] = '';
  711. $refundStatus = PaymentService::make()->refund($orderInfo,'pay');
  712. if(!PayOrdersModel::where(['id'=>$orderId])->update(['refund_status'=> $refundStatus?1:3,'failed_remark'=>$remark,'refund_remark'=>$this->error,'refund_money'=>$refundStatus?$refundAmount:0,'refund_at'=>date('Y-m-d H:i:s'),'update_time'=>time()])){
  713. $this->saveLog("caches:payments:payNotify_{$productId}:notify_{$out_trade_num}_error", ['error'=>$this->error,'order' => $orderInfo, 'notify' => $data]);
  714. $this->error = '订单退款状态更新错误~';
  715. DB::rollBack();
  716. return false;
  717. }
  718. }
  719. // 订单完成,代理收益结算
  720. if($status == 4 && $amount>0)
  721. {
  722. // 代理佣金结算
  723. $result = SettleService::make()->agentBonus($orderUserId, $recBonus, $orderInfo, $recBonusId);
  724. if ($result<0) {
  725. DB::rollBack();
  726. $this->error = SettleService::make()->getError();
  727. $this->saveLog("caches:settle:{$out_trade_num}:agent_{$orderUserId}_error",SettleService::make()->getError());
  728. return false;
  729. }
  730. $this->saveLog("caches:settle:{$out_trade_num}:agent_{$orderUserId}",$result);
  731. }
  732. DB::commit();
  733. $this->error = '充值订单处理成功~';
  734. return ['id'=>$orderId,'amount'=>$amount,'status'=>$status,'refund'=>$refundAmount];
  735. }
  736. /**
  737. * 日志
  738. * @param $key
  739. * @param $data
  740. */
  741. public function saveLog($key, $data)
  742. {
  743. if(env('APP_DEBUG')){
  744. RedisService::set($key,$data,env('DEBUG_EXPIRE',7200));
  745. }
  746. }
  747. /**
  748. * 退款请求
  749. * @param $order
  750. * @param string $scene
  751. * @return bool
  752. * @throws \Yansongda\Pay\Exception\ContainerException
  753. * @throws \Yansongda\Pay\Exception\InvalidParamsException
  754. * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
  755. */
  756. public function refund($order, $scene = 'store')
  757. {
  758. $money = isset($order['money']) ? $order['money'] : 0;
  759. $payType = isset($order['pay_type']) && $order['pay_type']? $order['pay_type'] : 10;
  760. $orderNo = isset($order['order_no']) ? $order['order_no'] : '';
  761. $outTradeNo = isset($order['out_trade_no']) ? $order['out_trade_no'] : '';
  762. $transactionId = isset($order['transaction_id']) ? $order['transaction_id'] : '';
  763. $remark = isset($order['remark']) && $order['remark'] ? $order['remark'] : '退款';
  764. $pay = PaymentService::make()->createPay($scene, $payType);
  765. if (empty($pay)) {
  766. DB::rollBack();
  767. $this->error = '创建退款支付失败';
  768. return false;
  769. }
  770. // 保证金退款处理
  771. $refundStatus = false;
  772. switch ($payType) {
  773. case 10: // 微信支付
  774. $data = [
  775. 'out_trade_no' => $outTradeNo?$outTradeNo:$orderNo,
  776. 'out_refund_no' => get_order_num('RF'),
  777. 'transaction_id' => $transactionId,
  778. 'notify_url' => url("/api/notify/{$scene}/{$payType}"),
  779. 'reason' => $remark,
  780. 'amount' => [
  781. 'refund' => intval($money * 100),
  782. 'total' => intval($money * 100),
  783. 'currency' => 'CNY',
  784. ],
  785. ];
  786. // 请求退款
  787. $pay = $pay->refund($data);
  788. RedisService::set("caches:refunds:order:{$orderNo}_wxpay", ['data' => $data, 'pay' => $pay, 'type' => $payType, 'date' => date('Y-m-d H:i:s')], 7200);
  789. if ($pay->status == 'SUCCESS' || $pay->status == 'PROCESSING') {
  790. $refundStatus = true;
  791. } else {
  792. DB::rollBack();
  793. $this->errorData = $data;
  794. $this->error = '微信退款处理失败:'.$pay->message;
  795. return false;
  796. }
  797. break;
  798. case 20: // 支付宝
  799. $data = [
  800. 'out_request_no' => $outTradeNo?$outTradeNo:$orderNo,
  801. 'trade_no' => $transactionId,
  802. 'refund_amount' => $money,
  803. 'query_options' => ['deposit_back_info'],
  804. 'refund_reason' => $remark,
  805. ];
  806. $payResult = $pay->refund($data);
  807. RedisService::set("caches:refunds:order:{$orderNo}_alipay", ['data' => $data, 'pay' => $payResult, 'type' => $payType, 'date' => date('Y-m-d H:i:s')], 7200);
  808. if ($payResult->code == 10000 || intval($payResult->code) == 40004) {
  809. $refundStatus = true;
  810. } else {
  811. $this->errorData = $data;
  812. $this->error = '支付宝退款处理失败:'.$payResult->code;
  813. return false;
  814. }
  815. break;
  816. default:
  817. $this->error = '退款支付类型错误';
  818. return false;
  819. }
  820. $this->error = '退款处理成功';
  821. return $refundStatus;
  822. }
  823. }