BalanceController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/4/2
  6. * Time: 下午5:37
  7. */
  8. namespace app\frontend\modules\finance\controllers;
  9. use app\common\exceptions\AppException;
  10. use app\common\exceptions\ShopException;
  11. use app\common\facades\Setting;
  12. use app\common\helpers\Cache;
  13. use app\common\services\credit\ConstService;
  14. use app\common\services\finance\BalanceChange;
  15. use app\common\events\payment\RechargeComplatedEvent;
  16. use app\common\services\password\PasswordService;
  17. use app\common\services\PayFactory;
  18. use app\common\components\ApiController;
  19. use app\frontend\modules\finance\models\Balance;
  20. use app\frontend\modules\finance\models\Balance as BalanceCommon;
  21. use app\frontend\modules\finance\models\BalanceTransfer;
  22. use app\frontend\modules\finance\models\BalanceConvertLove;
  23. use app\frontend\modules\finance\models\BalanceRecharge;
  24. use app\frontend\modules\finance\payment\types\RechargePaymentTypes;
  25. use app\frontend\modules\finance\services\BalanceRechargeSetService;
  26. use app\frontend\modules\finance\services\BalanceRecordService;
  27. use app\frontend\modules\finance\services\BalanceService;
  28. use app\backend\modules\member\models\Member;
  29. use Illuminate\Support\Facades\DB;
  30. class BalanceController extends ApiController
  31. {
  32. private $memberInfo;
  33. private $model;
  34. public $transactionActions = ['transfer'];
  35. private $money;
  36. protected $publicAction = ['alipay'];
  37. protected $ignoreAction = ['alipay'];
  38. public $memberModel;
  39. /**
  40. * @var BalanceService
  41. */
  42. public $balanceSet;
  43. public $uniacid;
  44. public function preAction()
  45. {
  46. parent::preAction();
  47. $this->balanceSet = new BalanceService();
  48. $this->uniacid = \YunShop::app()->uniacid;
  49. }
  50. /**
  51. * 余额首页数据
  52. * @return \Illuminate\Http\JsonResponse
  53. */
  54. public function index()
  55. {
  56. $data = (new BalanceService())->getIndexData();
  57. return $this->successJson('成功', $data);
  58. }
  59. /**
  60. * 会员余额页面信息,(余额设置+会员余额值)
  61. * @return \Illuminate\Http\JsonResponse
  62. */
  63. public function balance()
  64. {
  65. if ($memberInfo = $this->getMemberInfo()) {
  66. $result = (new BalanceService())->getBalanceSet();
  67. $result['credit2'] = $memberInfo->credit2;
  68. $result['buttons'] = app('Payment')->setPaymentTypes(new RechargePaymentTypes())->getPaymentButton();
  69. $result['typename'] = '充值';
  70. // $result['love_name'] = (app('plugins')->isEnabled('designer') == 1) ? LOVE_NAME : '爱心值';
  71. $result['love_name'] = LOVE_NAME;
  72. $result['convert'] = (new BalanceService())->convertSet();
  73. $result['remark'] = $this->getRechargeRemark();
  74. return $this->successJson('获取数据成功', $result);
  75. }
  76. return $this->errorJson('未获取到会员数据');
  77. }
  78. public function memberBalance()
  79. {
  80. if ($memberInfo = $this->getMemberInfo()) {
  81. /**
  82. * @var Member $memberInfo
  83. */
  84. return $this->successJson('获取数据成功', [
  85. 'credit2' => $memberInfo->credit2,
  86. 'has_password' => $memberInfo->yzMember->hasPayPassword(),
  87. 'need_password' => $this->needTransferPassword(),
  88. ]);
  89. }
  90. return $this->errorJson('未获取到会员数据');
  91. }
  92. /**
  93. * 会员余额转化爱心值
  94. * @return \Illuminate\Http\JsonResponse
  95. */
  96. public function conver()
  97. {
  98. if (!$this->balanceSet->convertSet()) {
  99. return $this->errorJson('未开启余额转化');
  100. }
  101. $memberInfo = $this->getMemberInfo();
  102. if ($memberInfo) {
  103. $result = (new BalanceService())->getBalanceSet();
  104. $result['credit2'] = $memberInfo->credit2;
  105. $result['rate'] = $this->balanceSet->convertRate();
  106. return $this->successJson('获取数据成功', $result);
  107. }
  108. return $this->errorJson('未获取到会员数据');
  109. }
  110. //余额充值+充值优惠
  111. public function recharge()
  112. {
  113. if (empty(\YunShop::request()->recharge_money) || \YunShop::request()->recharge_money == 'NaN') {
  114. return $this->errorJson('充值金额不能为空,请填写充值金额');
  115. }
  116. $result = (new BalanceService())->rechargeSet() ? $this->rechargeStart() : '未开启余额充值';
  117. if ($result === true) {
  118. $type = intval(\YunShop::request()->pay_type);
  119. $verify = (new BalanceRechargeSetService())->verifyRecharge($type, \YunShop::request()->recharge_money);
  120. if ($verify !== true) {
  121. return $this->errorJson($verify);
  122. }
  123. $array = [
  124. PayFactory::PAY_WEACHAT,
  125. PayFactory::PAY_YUN_WEACHAT,
  126. PayFactory::PAY_Huanxun_Quick,
  127. PayFactory::PAY_Huanxun_Wx,
  128. PayFactory::WFT_PAY,
  129. PayFactory::WFT_ALIPAY,
  130. PayFactory::PAY_WECHAT_HJ,
  131. PayFactory::PAY_ALIPAY_HJ,
  132. PayFactory::PAY_WECHAT_JUEQI,
  133. PayFactory::WECHAT_NATIVE,
  134. PayFactory::WECHAT_H5,
  135. PayFactory::XFPAY_ALIPAY,
  136. PayFactory::XFPAY_WECHAT,
  137. PayFactory::WECHAT_MIN_PAY,
  138. PayFactory::LESHUA_ALIPAY,
  139. PayFactory::LESHUA_WECHAT,
  140. PayFactory::LSP_PAY,
  141. PayFactory::CONVERGE_ALIPAY_H5_PAY,
  142. PayFactory::EPLUS_WECHAT_PAY,
  143. PayFactory::EPLUS_MINI_PAY,
  144. PayFactory::EPLUS_ALI_PAY,
  145. ];
  146. if (in_array($this->model->type, [PayFactory::EPLUS_ALI_PAY, PayFactory::EPLUS_WECHAT_PAY, PayFactory::EPLUS_MINI_PAY])) {
  147. $user = \Yunshop\EplusPay\services\SettingService::getUser(\YunShop::app()->getMemberId());
  148. if (!$user || !$user->is_bind_mobile) {
  149. return $this->errorJson('请先绑定智E+账户手机号', ['eplus_bind_mobile' => 1]);
  150. }
  151. }
  152. if (in_array($type, $array)) {
  153. return $this->successJson('支付接口对接成功', array_merge(['ordersn' => $this->model->ordersn], $this->payOrder()));
  154. }
  155. //头条支付
  156. if (in_array($type, [PayFactory::PAY_WECHAT_TOUTIAO, PayFactory::PAY_ALIPAY_TOUTIAO])) {
  157. $data['ordersn'] = $this->model->ordersn;
  158. $data['orderInfo'] = $this->payOrder();
  159. return $this->successJson('支付接口对接成功', $data);
  160. }
  161. //app支付宝支付添加新支付配置
  162. if ($type == PayFactory::PAY_APP_ALIPAY) {
  163. $isnewalipay = \Setting::get('shop_app.pay.newalipay');
  164. return $this->successJson('支付接口对接成功', ['ordersn' => $this->model->ordersn, 'isnewalipay' => $isnewalipay]);
  165. } else {
  166. return $this->successJson('支付接口对接成功', ['ordersn' => $this->model->ordersn]);
  167. }
  168. }
  169. //app支付宝新旧版值
  170. //处理报错返回信息格式不对
  171. $res = json_decode(json_encode($result), true);
  172. $res_text = '';
  173. foreach ($res as $item) {
  174. $res_text .= $item[0];
  175. }
  176. $result = $res_text ?: $result;
  177. return $this->errorJson($result);
  178. }
  179. //余额充值,如果是支付宝支付需要二次请求 alipay 支付接口
  180. public function alipay()
  181. {
  182. $orderSn = \YunShop::request()->order_sn;
  183. $this->model = BalanceRecharge::ofOrderSn($orderSn)->withoutGlobalScope('member_id')->first();
  184. if ($this->model) {
  185. return $this->successJson('支付接口对接成功', $this->payOrder());
  186. }
  187. return $this->errorJson('充值订单不存在');
  188. }
  189. public function cloudWechatPay()
  190. {
  191. $orderSn = \YunShop::request()->ordersn;
  192. $this->model = BalanceRecharge::ofOrderSn($orderSn)->withoutGlobalScope('member_id')->first();
  193. if ($this->model) {
  194. return $this->successJson('支付接口对接成功', $this->payOrder());
  195. }
  196. return $this->errorJson('充值订单不存在');
  197. }
  198. public function wechatPayJueqi()
  199. {
  200. $orderSn = \YunShop::request()->order_pay_id;
  201. $this->model = BalanceRecharge::ofOrderSn($orderSn)->withoutGlobalScope('member_id')->first();
  202. if ($this->model) {
  203. return $this->successJson('支付接口对接成功', $this->payOrder());
  204. }
  205. return $this->errorJson('充值订单不存在');
  206. }
  207. //余额转让
  208. public function transfer()
  209. {
  210. $result = (new BalanceService())->transferSet() ? $this->transferStart() : '未开启余额转让';
  211. return $result === true ? $this->successJson('转让成功') : $this->errorJson($result);
  212. }
  213. //余额转化爱心值
  214. public function convertLoveValue()
  215. {
  216. $result = (new BalanceService())->convertSet() ? $this->convertStart() : '未开启余额转化';
  217. return $result === true ? $this->successJson('转化成功') : $this->errorJson($result);
  218. }
  219. /**
  220. * 余额明细页面数据
  221. * @return \Illuminate\Http\JsonResponse
  222. */
  223. public function record()
  224. {
  225. $search = request()->search;
  226. $date = date('Y-m', strtotime($search['date']));
  227. $record_data = (new BalanceRecordService())->getRecordData();
  228. // 选择的日期 && 日期参数存在 && 小于等于第一页。这想条件符合证明第一页没有数据就直接返回
  229. if(!$record_data['record_list']['data'][$date] && $search['date'] && request()->page <= 1){
  230. return $this->errorJson("暂无数据!");
  231. }elseif (false === $record_data){
  232. return $this->errorJson("暂无数据!");
  233. }
  234. return $this->successJson("成功", $record_data);
  235. }
  236. /**
  237. * 获取余额的业务类型
  238. * 根据当前会员的余额明细表所拥有的服务类型
  239. * @return \Illuminate\Http\JsonResponse
  240. */
  241. public function getServiceTypeList()
  242. {
  243. $member_id = \YunShop::app()->getMemberId();
  244. $redis_key = "ServiceTypeList:".$member_id;
  245. if(Cache::has($redis_key)){
  246. $service_type_arr = Cache::get($redis_key);
  247. }else{
  248. $service_type_arr = (new ConstService(''))->sourceComment();
  249. $service_type_key = Balance::getServiceType();
  250. $service_type_arr = $service_type_key->map(function ($serviceKey) use ($service_type_arr){
  251. return [
  252. 'id' => $serviceKey,
  253. 'name' => $service_type_arr[$serviceKey],
  254. ];
  255. })->values();
  256. //根据当前会员的余额明细查出所拥有的服务类型,存进缓存
  257. Cache::put($redis_key, $service_type_arr, 1440);
  258. }
  259. return $this->successJson('成功', $service_type_arr);
  260. }
  261. //余额转换爱心值
  262. public function convertStart()
  263. {
  264. if (!class_exists('\Yunshop\Love\Common\Services\LoveChangeService')) {
  265. return $this->errorJson('未开启爱心值插件');
  266. }
  267. if (!$this->getMemberInfo()) {
  268. return '未获取到会员信息';
  269. }
  270. if (\YunShop::request()->convert_amount <= 0) {
  271. return '转化金额必须大于零';
  272. }
  273. if ($this->memberInfo->credit2 < \Yunshop::request()->convert_amount) {
  274. return '转化余额不能大于您的余额';
  275. }
  276. $this->model = new BalanceConvertLove();
  277. $this->model->fill($this->getConvertData());
  278. $validator = $this->model->validator();
  279. if ($validator->fails()) {
  280. return $validator->messages();
  281. }
  282. if ($this->model->save()) {
  283. //$result = (new BalanceService())->balanceChange($this->getChangeBalanceDataToTransfer());
  284. $result = (new BalanceChange())->convert($this->getChangeConverData());
  285. if ($result === true) {
  286. if ($this->awardMemberLove() !== true) {
  287. (new BalanceChange())->convertCancel($this->getConvertCancel()); //爱心值交易失败,回滚余额
  288. $this->errorJson('转化失败');
  289. }
  290. $this->model->status = BalanceConvertLove::CONVERT_STATUS_SUCCES;
  291. if ($this->model->save()) {
  292. return true;
  293. }
  294. }
  295. return '修改转化状态失败';
  296. }
  297. return '转化写入出错,请联系管理员';
  298. }
  299. private function getMemberModel()
  300. {
  301. $memberModel = Member::where('uid', \YunShop::app()->getMemberId())->first();
  302. if ($memberModel) {
  303. return $memberModel;
  304. }
  305. throw new AppException('未获取到会员信息');
  306. }
  307. private function needTransferPassword()
  308. {
  309. return (new PasswordService())->isNeed('balance', 'transfer');
  310. }
  311. /**
  312. * 获取充值按钮
  313. *
  314. * @return array
  315. */
  316. private function getPayTypeButtons()
  317. {
  318. $event = new RechargeComplatedEvent([]);
  319. event($event);
  320. $result = $event->getData();
  321. $type = \YunShop::request()->type;
  322. if ($type == 2) {
  323. $button = [];
  324. foreach ($result as $item) {
  325. if ($item['value'] == 1 || $item['value'] == 28 || $item['value'] == 33 || $item['value'] == 78) {
  326. $button[] = $item;
  327. }
  328. }
  329. return $button;
  330. }
  331. //头条小程序
  332. if ($type == 11) {
  333. $button = [];
  334. foreach ($result as $item) {
  335. if ($item['value'] == 51 || $item['value'] == 52) {
  336. $button[] = $item;
  337. }
  338. }
  339. return $button;
  340. } else {
  341. foreach ($result as $key => $item) {
  342. if ($item['value'] == 51 || $item['value'] == 52) {
  343. unset($result[$key]);
  344. }
  345. // 如果是其他浏览器访问,商云客微信支付按钮不显示
  346. if ($type == 5 && $item['value'] == 78) {
  347. unset($result[$key]);
  348. }
  349. }
  350. }
  351. return $result;
  352. }
  353. //获取会员信息
  354. private function getMemberInfo()
  355. {
  356. return $this->memberInfo = Member::where('uid', \YunShop::app()->getMemberId())->first();
  357. }
  358. //充值开始
  359. private function rechargeStart()
  360. {
  361. if (!$this->getMemberInfo()) {
  362. return '未获取到会员数据,请重试!';
  363. }
  364. $this->model = new BalanceRecharge();
  365. $this->model->fill($this->getRechargeData());
  366. $validator = $this->model->validator();
  367. if ($validator->fails()) {
  368. return $validator->messages();
  369. }
  370. if ($this->model->save()) {
  371. return true;
  372. }
  373. return '充值写入失败,请联系管理员';
  374. }
  375. private function recipient()
  376. {
  377. return request()->input('recipient');
  378. }
  379. private function amount()
  380. {
  381. return request()->input('transfer_money');
  382. }
  383. protected function memberId()
  384. {
  385. return \YunShop::app()->getMemberId();
  386. }
  387. protected function password()
  388. {
  389. return request()->input('password');
  390. }
  391. //余额转让开始
  392. private function transferStart()
  393. {
  394. if ($this->needTransferPassword()) (new PasswordService())->checkPayPassword($this->memberId(), $this->password());
  395. if (!$this->getMemberInfo()) {
  396. return '未获取到会员信息';
  397. }
  398. if ($this->amount() <= 0) {
  399. return '转让金额必须大于零';
  400. }
  401. if ($this->memberInfo->credit2 < $this->amount()) {
  402. return '转让余额不能大于您的余额';
  403. }
  404. if ($this->memberInfo->uid == $this->recipient()) {
  405. return '转让者不能是自己';
  406. }
  407. if (!Member::getMemberInfoById($this->recipient())) {
  408. return '被转让者不存在';
  409. }
  410. if ((new BalanceService())->teamTransferSet()) {
  411. if (!(new BalanceService())->teamTransfer($this->recipient())) {
  412. return '转让者不是团队成员';
  413. }
  414. }
  415. $this->model = new BalanceTransfer();
  416. $this->model->fill($this->getTransferData());
  417. $validator = $this->model->validator();
  418. if ($validator->fails()) {
  419. return $validator->messages();
  420. }
  421. if ($this->model->save()) {
  422. //$result = (new BalanceService())->balanceChange($this->getChangeBalanceDataToTransfer());
  423. $result = (new BalanceChange())->transfer($this->getChangeBalanceDataToTransfer());
  424. if ($result === true) {
  425. $this->model->status = BalanceTransfer::TRANSFER_STATUS_SUCCES;
  426. if ($this->model->save()) {
  427. return true;
  428. }
  429. }
  430. return '修改转让状态失败';
  431. }
  432. return '转让写入出错,请联系管理员';
  433. }
  434. private function getConvertData()
  435. {
  436. return array(
  437. 'uniacid' => \Yunshop::app()->uniacid,
  438. 'member_id' => \Yunshop::app()->getMemberId(),
  439. 'covert_amount' => \Yunshop::request()->convert_amount,
  440. 'status' => BalanceConvertLove::CONVERT_STATUS_ERROR,
  441. 'order_sn' => $this->getTransferOrderSN(),
  442. 'remark' => '余额转化爱心值',
  443. );
  444. }
  445. private function getChangeConverData()
  446. {
  447. return array(
  448. 'member_id' => $this->model->member_id,
  449. 'remark' => '会员【ID:' . $this->model->member_id . '】余额转化爱心值会员【ID:' . $this->model->member_id . '】' . $this->model->covert_amount . '元',
  450. 'source' => ConstService::SOURCE_CONVERT,
  451. 'relation' => $this->model->order_sn,
  452. 'operator' => ConstService::OPERATOR_MEMBER,
  453. 'operator_id' => $this->model->member_id,
  454. 'change_value' => $this->model->covert_amount,
  455. );
  456. }
  457. private function getConvertCancel()
  458. {
  459. return array(
  460. 'member_id' => $this->model->member_id,
  461. 'remark' => '会员【ID:' . $this->model->member_id . '】余额转化失败【ID:' . $this->model->member_id . '】' . $this->model->covert_amount . '元',
  462. 'source' => ConstService::SOURCE_CONVERT_CANCEL,
  463. 'relation' => $this->getTransferOrderSN(),
  464. 'operator' => ConstService::OPERATOR_MEMBER,
  465. 'operator_id' => $this->model->member_id,
  466. 'change_value' => $this->model->covert_amount,
  467. );
  468. }
  469. /**
  470. * 转化爱心值
  471. * @return bool
  472. */
  473. private function awardMemberLove()
  474. {
  475. //统一走爱心值交易类型接口
  476. $_LoveChangeService = new \Yunshop\Love\Common\Services\LoveChangeService('usable');
  477. $data = [
  478. 'member_id' => $this->model->member_id,
  479. 'change_value' => $this->calculateLoveValue(),
  480. 'operator' => ConstService::OPERATOR_MEMBER,
  481. 'operator_id' => $this->model->member_id,
  482. 'remark' => '会员【ID:' . $this->model->member_id . '】余额转化爱心值会员【ID:' . $this->model->member_id . '】' . $this->model->covert_amount . '元',
  483. 'relation' => $this->model->order_sn,
  484. ];
  485. $result = $_LoveChangeService->conver($data);
  486. if ($result !== true) {
  487. DB::rollBack();
  488. return false;
  489. }
  490. DB::commit();
  491. return true;
  492. }
  493. /**
  494. * 计算爱心值
  495. * @return string
  496. */
  497. private function calculateLoveValue()
  498. {
  499. return bcdiv(bcmul($this->model->covert_amount, $this->balanceSet->convertRate(), 2), 100, 2);
  500. }
  501. //余额转让详细记录数据
  502. private function getChangeBalanceDataToTransfer()
  503. {
  504. return array(
  505. 'member_id' => $this->model->transferor,
  506. 'remark' => '会员【ID:' . $this->model->transferor . '】余额转让会员【ID:' . $this->model->recipient . '】' . $this->model->money . '元',
  507. 'source' => ConstService::SOURCE_TRANSFER,
  508. 'relation' => $this->model->order_sn,
  509. 'operator' => ConstService::OPERATOR_MEMBER,
  510. 'operator_id' => $this->model->transferor,
  511. 'change_value' => $this->model->money,
  512. 'recipient' => $this->model->recipient,
  513. );
  514. }
  515. private function getTransferData()
  516. {
  517. return array(
  518. 'uniacid' => \YunShop::app()->uniacid,
  519. 'transferor' => \YunShop::app()->getMemberId(),
  520. 'recipient' => \YunShop::request()->recipient,
  521. 'money' => trim(\YunShop::request()->transfer_money),
  522. 'status' => BalanceTransfer::TRANSFER_STATUS_ERROR,
  523. 'order_sn' => $this->getTransferOrderSN(),
  524. );
  525. }
  526. /**
  527. * 生成唯一转让订单号
  528. * @return string
  529. */
  530. private function getTransferOrderSN()
  531. {
  532. $orderSn = createNo('TS', true);
  533. while (1) {
  534. if (!BalanceTransfer::ofOrderSn($orderSn)->first()) {
  535. break;
  536. }
  537. $orderSn = createNo('TS', true);
  538. }
  539. return $orderSn;
  540. }
  541. //充值记录表data数据
  542. private function getRechargeData()
  543. {
  544. //$change_money = substr(\YunShop::request()->recharge_money, 0, strpos(\YunShop::request()->recharge_money, '.')+3);
  545. $change_money = \YunShop::request()->recharge_money;
  546. if (\YunShop::request()->pay_type == PayFactory::PAY_APP_ALIPAY) {
  547. //支付宝APP支付充值金额超过6位数,支付宝会自动对超过6位的小数点后的数值进行四舍五入
  548. $length = strlen(intval($change_money));
  549. if ($length >= 5 && $change_money > intval($change_money)) {
  550. throw new ShopException('APP支付宝充值超5位数的金额不能拥有小数,请重新填写');
  551. }
  552. }
  553. return array(
  554. 'uniacid' => \YunShop::app()->uniacid,
  555. 'member_id' => $this->memberInfo->uid,
  556. 'old_money' => $this->memberInfo->credit2 ?: 0,
  557. 'money' => floatval($change_money),
  558. 'new_money' => $change_money + $this->memberInfo->credit2,
  559. 'ordersn' => BalanceRecharge::createOrderSn('RV', 'ordersn'),
  560. 'type' => intval(\YunShop::request()->pay_type),
  561. 'status' => BalanceRecharge::PAY_STATUS_ERROR,
  562. 'remark' => '会员前端充值',
  563. );
  564. }
  565. /**
  566. * 会员余额充值支付接口
  567. */
  568. private function payOrder()
  569. {
  570. $pay = PayFactory::create($this->model->type);
  571. $result = $pay->doPay($this->payData(),$this->model->type);
  572. \Log::info('++++++++++++++++++', $result);
  573. if ($this->model->type == 1) {
  574. $result['js'] = json_decode($result['js'], 1);
  575. }
  576. if (in_array($this->model->type,[PayFactory::PAY_WECHAT_HJ, PayFactory::PAY_ALIPAY_HJ])) {
  577. if ($result['msg'] !== '成功') {
  578. throw new AppException($result['msg']);
  579. }
  580. }
  581. if (in_array($this->model->type, [PayFactory::CONVERGE_ALIPAY_H5_PAY])) {
  582. if ($result['code'] != 200) {
  583. throw new AppException($result['msg']);
  584. }
  585. }
  586. if (in_array($this->model->type, [PayFactory::EPLUS_MINI_PAY, PayFactory::EPLUS_WECHAT_PAY])) {
  587. $result = json_decode($result['payInfo'], true) ?: [];
  588. if ($result['timeStamp']) {
  589. $result['timestamp'] = $result['timeStamp'];
  590. }
  591. }
  592. if ($this->model->type == PayFactory::EPLUS_ALI_PAY) {
  593. $result = ['pay_url'=>$result['payInfo'] ?: ''];
  594. }
  595. \Log::debug('余额充值 result', $result);
  596. return $result;
  597. }
  598. /**
  599. * 支付请求数据
  600. *
  601. * @return array
  602. * @Author yitian
  603. */
  604. private function payData()
  605. {
  606. $array = array(
  607. 'subject' => '会员充值',
  608. 'body' => '会员充值金额' . $this->model->money . '元:' . \YunShop::app()->uniacid,
  609. 'amount' => $this->model->money,
  610. 'member_id' => \YunShop::app()->getMemberId(),
  611. 'order_no' => $this->model->ordersn,
  612. 'extra' => ['type' => 2],
  613. 'ask_for' => 'recharge',
  614. );
  615. if ($this->model->type == PayFactory::PAY_CLOUD_ALIPAY) {
  616. $array['extra'] = ['type' => 2, 'pay' => 'cloud_alipay'];
  617. }
  618. if ($this->model->type == PayFactory::PAY_Huanxun_Quick) {
  619. $array['extra'] = ['type' => 2, 'pay' => 'quick'];
  620. }
  621. return $array;
  622. }
  623. /**
  624. * 获取充值活动说明
  625. * @return array
  626. */
  627. private function getRechargeRemark()
  628. {
  629. $balance_set = Setting::get('finance.balance');
  630. $shop_set = Setting::get('shop');
  631. if (!$this->balanceSet->rechargeSet() || !$this->balanceSet->rechargeActivityStatus()) {
  632. if ($balance_set['charge_reward_swich'] == 1) {
  633. //活动中的赠送积分不受控
  634. return [
  635. 'reward_points' => ['rate' => $balance_set['charge_reward_rate'] ?: 100, 'name' => $shop_set['credit1'] ? $shop_set['credit1'] : '积分'],
  636. ];
  637. }
  638. return [];//未开启活动或余额充值
  639. }
  640. $data = [
  641. 'recharge_activity_start' => date('Y-m-d H:i:s', $this->balanceSet->rechargeActivityStartTime()),
  642. 'recharge_activity_end' => date('Y-m-d H:i:s', $this->balanceSet->rechargeActivityEndTime()),
  643. 'recharge_activity_fetter' => $this->balanceSet->rechargeActivityFetter(),//最多参与次数
  644. 'proportion_status' => $this->balanceSet->proportionStatus(),//0-固定金额,1-百分比
  645. 'sale' => $this->balanceSet->rechargeSale(),
  646. ];
  647. if ($balance_set['charge_reward_swich'] == 1) {
  648. $data['reward_points'] = ['rate' => $balance_set['charge_reward_rate'] ?: 100, 'name' => $shop_set['credit1'] ? $shop_set['credit1'] : '积分'];
  649. }
  650. return $data;
  651. }
  652. }