PaymentService.php 37 KB

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