MpOrder.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\controller\api\store\order;
  3. use app\common\repositories\merchant\apply\MerchantHaikeApplyRepository;
  4. use app\common\repositories\store\order\StoreGroupOrderRepository;
  5. use app\common\repositories\system\config\ConfigValueRepository;
  6. use crmeb\basic\BaseController;
  7. use app\common\repositories\store\order\StoreCartRepository;
  8. use app\common\repositories\store\order\StoreOrderRepository;
  9. use app\traits\HaikeApiRequest;
  10. use think\App;
  11. use think\facade\Db;
  12. use think\facade\Log;
  13. class MpOrder extends BaseController
  14. {
  15. use HaikeApiRequest;
  16. /**
  17. * @var StoreOrderRepository
  18. */
  19. protected $repository;
  20. /**
  21. * StoreOrder constructor.
  22. * @param App $app
  23. * @param StoreOrderRepository $repository
  24. */
  25. public function __construct(App $app, StoreOrderRepository $repository)
  26. {
  27. parent::__construct($app);
  28. $this->repository = $repository;
  29. }
  30. //查询商户状态
  31. public function test(){
  32. $merch_no = (string)$this->request->param("merch_no", '');
  33. $res = $this -> merchantInfo(['merch_no' => $merch_no]);
  34. return app('json')->status('success', $res);
  35. }
  36. //配置微信公众号
  37. public function test1(){
  38. $merch_no = (string)$this->request->param("merch_no", '');
  39. $url = env('HAIKE.HOSTNAME')."/api/v1/wx-appid-conf/add";
  40. $data = [
  41. "agent_no"=> env('HAIKE.AGENT_NO'),
  42. "merch_no"=> $merch_no,
  43. "conf_key"=> "pay_appId",
  44. "conf_value"=> 'wx10669a2eca032216',
  45. ];
  46. $res = $this -> request($data,$url);
  47. return app('json')->status('success', $res);
  48. }
  49. //批量修改商户结算方式申请及变更
  50. public function merchantWithdrawalSetting(){
  51. $haikeApplyRepository = app()->make(MerchantHaikeApplyRepository::class);
  52. $merchant = Db::name("merchant") -> where("withdrawal_setting","D1") -> select();
  53. foreach ($merchant as $key => $value){
  54. $response = $haikeApplyRepository->merchantWithdrawalSetting([
  55. "merch_no"=> $value['merch_no'],
  56. ]);
  57. Log::info($response);
  58. }
  59. }
  60. public function notify(){
  61. try {
  62. $post = $this->request->post();
  63. Db::name("log")->insert(array('data'=>'小程序支付回调:'.json_encode($post)));//日志记录
  64. $bool = $this->doNotify($post);
  65. if (!$bool)
  66. return app('json')->success(['result' => 'ERROR']);
  67. Db::name("log")->insert(array('data'=>'小程序支付回调1:==='));//日志记录
  68. $store_order = Db::name("store_order") -> where("order_sn",$post['out_trade_no']) -> field('group_order_id,order_id') -> find();
  69. Db::name("log")->insert(array('data'=>'小程序支付回调2:'.$store_order['group_order_id']));//日志记录
  70. $groupOrder = app()->make(StoreGroupOrderRepository::class)->getWhere(['group_order_id' => $store_order['group_order_id']]);
  71. Db::name("log")->insert(array('data'=>'小程序支付回调3:'.json_encode($groupOrder)));//日志记录
  72. if (!$groupOrder || $groupOrder->paid == 1)
  73. return app('json')->success(['result' => 'SUCCESS']);
  74. app()->make(StoreOrderRepository::class)->paySuccess($groupOrder,2);
  75. app()->make(StoreOrderRepository::class)->takeOrder($store_order['order_id']);
  76. return app('json')->success(['result' => 'SUCCESS']);
  77. } catch (\Throwable $e) {
  78. Log::info($e->getMessage());
  79. return app('json')->status('error', $e->getMessage());
  80. }
  81. }
  82. /**
  83. * 聚合公众号支付API
  84. * @return mixed
  85. * @author php迷途小书童
  86. * @created 2021/1/16 10:02
  87. */
  88. public function jsapiPay(){
  89. $id = (string)$this->request->param("id", '');
  90. $pay_user_id = (string)$this->request->param("pay_user_id", '');
  91. $order = $this->repository->getDetailByGroupOrderId((int)$id);
  92. if (!$order)
  93. return app('json')->fail('订单不存在');
  94. $orders['trade_type'] = 'WX';
  95. $orders['merch_no'] = Db::name("merchant")->where("mer_id",$order['mer_id'])->value('merch_no');
  96. $orders['out_trade_no'] = $order['order_sn'];
  97. $orders['money'] = $order['pay_price'];
  98. $orders['pay_user_id'] = $pay_user_id;
  99. $orders['appid'] = 'wx10669a2eca032216';
  100. $orders['type'] = 'mp';
  101. $orders['pension'] = $this -> repository -> ch_rebate($order['mer_id'],$order['pay_price']);
  102. //商家返佣比例
  103. $base_commission= merchantConfig($order['mer_id'], 'base_commission');
  104. if(!$base_commission){
  105. //若商家返佣比例不存在 则使用平台配置
  106. $base_commission = systemConfig('extension_one_rate');
  107. }
  108. //sat刷卡手续费比例
  109. $souxufee = systemConfig('yanglao');
  110. $souxufees = bcmul($order['pay_price'], $souxufee * 0.01, 2);
  111. $money = bcsub($order['pay_price'], $souxufees, 2);
  112. $avaiable = bcmul($money, $base_commission * 0.01, 2);
  113. //服务商分账的钱
  114. $service_money = bcsub($avaiable, $orders['pension'], 2);
  115. $orders['service_money'] = $service_money;
  116. $notify_url = '/api/mpPay/notify';
  117. $data = $this->getJsapiPayGateway($orders,$notify_url);
  118. try {
  119. if ($data['return_code'] === 10000) {
  120. $params = [
  121. 'trade_no' => $data['trade_no'],
  122. 'channel_trade_no' => $data['channel_trade_no'],
  123. ];
  124. Db::name("store_order")->where("order_id",$order['order_id'])->update($params);
  125. return app('json')->success($data);
  126. } else {
  127. return app('json')->fail($data['return_msg']);
  128. }
  129. } catch (\Throwable $e) {
  130. return app('json')->status('error', $e->getMessage(), ['out_trade_no' => $order['order_sn']]);
  131. }
  132. }
  133. public function getOrder(StoreOrderRepository $storeOrderRepository){
  134. $mer_id = (string)$this->request->param("mer_id", '');
  135. $status = $this->request->param("status", 1);
  136. $uid = $this->request->uid();
  137. [$page, $limit] = $this->getPage();
  138. $where['mer_id'] = $mer_id;
  139. $where['uid'] = $uid;
  140. $where['status'] = $status;
  141. $where['is_del'] = 0;
  142. $list = $storeOrderRepository->merchantGetList($where, $page, $limit);
  143. return app('json')->success($list);
  144. }
  145. }