PayController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace mini\controllers;
  3. use biz\user\services\UserService;
  4. use common\components\configDict;
  5. use common\components\stringUtil;
  6. use common\components\util;
  7. use common\services\xhMerchantExtendService;
  8. use common\services\xhMerchantService;
  9. use common\services\xhOrderService;
  10. use common\services\xhPayToolService;
  11. use common\services\xhRechargeService;
  12. use common\services\xhUserAssetService;
  13. use common\services\xhUserService;
  14. use Yii;
  15. use yii\web\Controller;
  16. use yii\helpers\Json;
  17. use linslin\yii2\curl;
  18. use common\components\jsSDK;
  19. class PayController extends BaseController
  20. {
  21. public $enableCsrfValidation = false;
  22. public $secretKey = 'wx3hj5a26af5c17fb86921f3';
  23. public function actionBalancePay()
  24. {
  25. $account = $this->merchantId;
  26. $merchant = $this->merchant;
  27. $payAmount = Yii::$app->request->get('payAmount', 2000);
  28. $shopId = $merchant['defaultShopId'];
  29. $user = $this->userInfo;
  30. ini_set('date.timezone', 'Asia/Shanghai');
  31. if ($payAmount < 0) {
  32. util::failInfo('付款金额太少');
  33. }
  34. $userId = $user['id'];
  35. $userAsset = xhUserAssetService::getByUserId($userId);
  36. $level = isset($userAsset['memberLevel']) ? $userAsset['memberLevel'] : 0;
  37. $merchantExtend = xhMerchantExtendService::getByMerchantId($account);
  38. $memberIntegral = xhMerchantExtendService::getGradeList($merchantExtend, 'desc');
  39. $discount = isset($memberIntegral[$level]['discount']) ? $memberIntegral[$level]['discount'] : $memberIntegral[$level]['discount'];
  40. $prePrice = $payAmount;
  41. $actPrice = $payAmount;
  42. //折扣后保留一位小数
  43. if (!empty($discount) && $prePrice >= 1) {
  44. $currentPrice = ($prePrice * $discount) / 100;
  45. $actPrice = substr(sprintf("%.3f", $currentPrice), 0, -2);
  46. }
  47. $userId = $user['id'];
  48. $payData['userId'] = $userId;
  49. $payData['prePrice'] = $prePrice;
  50. $payData['actPrice'] = $actPrice;
  51. $payData['payStyle'] = 0;
  52. $payData['shopId'] = $shopId;
  53. $payData['merchantId'] = $merchant['id'];
  54. $now = time();
  55. $expireTime = $now + 300;//订单5分钟后过期
  56. $payData['createTime'] = date("Y-m-d H:i:s", $now);
  57. $payData['addTime'] = time();
  58. $payData['deadline'] = $expireTime;
  59. $payData['id'] = stringUtil::generateOrderNo($account, $userId);
  60. $payData['sourceType'] = 1;//订单来源:0商城订单 1付款订单
  61. $payment = xhOrderService::add($payData);
  62. $orderId = $payment['id'];
  63. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  64. $order = xhOrderService::getById($orderId);
  65. $typeList = configDict::getConfig('capitalType');
  66. $capitalType = $typeList['xhOrder']['id'];
  67. $totalFee = $order['actPrice'];
  68. $return = xhPayToolService::balancePay($orderId, $totalFee, $capitalType, $couponId);
  69. if ($return['code'] == 'A0001') {
  70. util::sendSuccess();
  71. }
  72. util::failInfo($return['msg']);
  73. }
  74. //小程序快速付款页 shish 2019.7.26
  75. public function actionGetPayParams()
  76. {
  77. $merchant = $this->merchant;
  78. $shopId = $merchant['defaultShopId'];
  79. $account = $this->merchantId;
  80. $extend = xhMerchantExtendService::getByMerchantId($account);
  81. if (empty($extend)) {
  82. util::failInfo('商家信息有误');
  83. }
  84. $user = $this->userInfo;
  85. if (empty($user)) {
  86. util::failInfo('没有用户信息');
  87. }
  88. ini_set('date.timezone', 'Asia/Shanghai');
  89. $payAmount = Yii::$app->request->get('payAmount', 2000);
  90. if ($payAmount < 0) {
  91. util::failInfo('付款金额太少');
  92. }
  93. $userId = $user['id'];
  94. $userAsset = xhUserAssetService::getByUserId($userId);
  95. $level = isset($userAsset['memberLevel']) ? $userAsset['memberLevel'] : 0;
  96. $memberIntegral = xhMerchantExtendService::getGradeList($extend, 'desc');
  97. $discount = isset($memberIntegral[$level]['discount']) ? $memberIntegral[$level]['discount'] : $memberIntegral[$level]['discount'];
  98. $prePrice = $payAmount;
  99. $actPrice = $payAmount;
  100. //折扣后保留一位小数
  101. if (!empty($discount) && $prePrice >= 1) {
  102. $currentPrice = ($prePrice * $discount) / 100;
  103. $actPrice = substr(sprintf("%.3f", $currentPrice), 0, -2);
  104. }
  105. $userId = $user['id'];
  106. $payData['userId'] = $userId;
  107. $payData['prePrice'] = $prePrice;
  108. $payData['actPrice'] = $actPrice;
  109. $payData['payStyle'] = 0;
  110. $payData['shopId'] = $shopId;
  111. $payData['merchantId'] = $merchant['id'];
  112. $now = time();
  113. $expireTime = $now + 300;//订单5分钟后过期
  114. $payData['createTime'] = date("Y-m-d H:i:s", $now);
  115. $payData['addTime'] = time();
  116. $payData['deadline'] = $expireTime;
  117. $payData['id'] = stringUtil::generateOrderNo($account, $userId);
  118. $payData['sourceType'] = 1;//订单来源:0商城订单 1付款订单
  119. $payment = xhOrderService::add($payData);
  120. $orderId = $payment['id'];
  121. Yii::$app->params['userId'] = $userId;
  122. Yii::$app->params['merchantId'] = $merchant['id'];
  123. UserService::updateVisitTime();
  124. $name = '买单';
  125. $totalFee = $actPrice;
  126. $openId = $user['miniOpenId'];
  127. if (empty($openId)) {
  128. util::failInfo('miniOpenId empty');
  129. }
  130. $typeList = configDict::getConfig('capitalType');
  131. $capitalType = $typeList['xhOrder']['id'];
  132. $attach = 'capitalType=' . $capitalType;//将流水类型、代金劵传过去
  133. $weixin = Yii::getAlias("@vendor/weixin");
  134. require_once($weixin . '/lib/WxPay.Api.php');
  135. require_once($weixin . '/example/WxPay.JsApiPay.php');
  136. $input = new \WxPayUnifiedOrder();
  137. $input->SetBody($name);
  138. $input->SetOut_trade_no($orderId);
  139. $input->SetTotal_fee($totalFee * 100);
  140. $input->SetTime_start(date("YmdHis", $now));
  141. $input->SetAttach($attach);
  142. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  143. $input->SetNotify_url(Yii::$app->params['miniUrl'] . '/notice/mini-pay-callback/');
  144. $input->SetTrade_type("JSAPI");
  145. //服务商 代设置微信支付,要添加的参数设置
  146. if ($extend['generalMerchant'] == 1) {
  147. //$input->SetOpenid($openId);
  148. $input->SetSub_openid($openId);
  149. //自设置 微信支付
  150. } else {
  151. $input->SetOpenid($openId);
  152. }
  153. //这里用的wxAppId是小程序的appId
  154. $extend['wxAppId'] = $extend['miniAppId'];
  155. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $extend);
  156. $tools = new \JsApiPay();
  157. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $extend);
  158. $newParams = json_decode($jsApiParameters, true);
  159. $newParams['orderId'] = $orderId;
  160. util::successInfo('操作成功', 'A0001', $newParams);
  161. }
  162. //小程序商品发起支付需要的参数
  163. public function actionGetMiniPayParams()
  164. {
  165. $id = Yii::$app->request->get('id');
  166. $merchant = $this->merchant;
  167. $extend = xhMerchantExtendService::getByMerchantId($this->merchantId);
  168. $payment = xhOrderService::getById($id);
  169. $orderId = $payment['id'];
  170. Yii::$app->params['userId'] = $this->userId;
  171. Yii::$app->params['merchantId'] = $merchant['id'];
  172. UserService::updateVisitTime();
  173. $name = '买单';
  174. $totalFee = $payment['actPrice'];
  175. $openId = $this->userInfo['miniOpenId'];
  176. if (empty($openId)) {
  177. util::failInfo('miniOpenId empty');
  178. }
  179. $now = time();
  180. $expireTime = $now + 300;//订单5分钟后过期
  181. $typeList = configDict::getConfig('capitalType');
  182. $capitalType = $typeList['xhOrder']['id'];
  183. $attach = 'capitalType=' . $capitalType;//将流水类型、代金劵传过去
  184. $weixin = Yii::getAlias("@vendor/weixin");
  185. require_once($weixin . '/lib/WxPay.Api.php');
  186. require_once($weixin . '/example/WxPay.JsApiPay.php');
  187. $input = new \WxPayUnifiedOrder();
  188. $input->SetBody($name);
  189. $input->SetOut_trade_no($orderId);
  190. $input->SetTotal_fee($totalFee * 100);
  191. $input->SetTime_start(date("YmdHis", $now));
  192. $input->SetAttach($attach);
  193. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  194. $input->SetNotify_url(Yii::$app->params['miniUrl'] . '/notice/mini-pay-callback/');
  195. $input->SetTrade_type("JSAPI");
  196. //服务商 代设置微信支付,要添加的参数设置
  197. if ($extend['generalMerchant'] == 1) {
  198. //$input->SetOpenid($openId);
  199. $input->SetSub_openid($openId);
  200. //自设置 微信支付
  201. } else {
  202. $input->SetOpenid($openId);
  203. }
  204. //这里用的wxAppId是小程序的appId
  205. $extend['wxAppId'] = $extend['miniAppId'];
  206. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $extend);
  207. $tools = new \JsApiPay();
  208. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $extend);
  209. $newParams = json_decode($jsApiParameters, true);
  210. $newParams['orderId'] = $orderId;
  211. util::successInfo('操作成功', 'A0001', $newParams);
  212. }
  213. public function actionGetRechargeParams()
  214. {
  215. $account = $this->merchantId;
  216. $merchant = $this->merchant;
  217. $extend = xhMerchantExtendService::getByMerchantId($account);
  218. if (empty($extend)) {
  219. util::failInfo('没有商家信息...');
  220. }
  221. $user = $this->userInfo;
  222. if (empty($user)) {
  223. util::failInfo('没有用户信息');
  224. }
  225. ini_set('date.timezone', 'Asia/Shanghai');
  226. $rechargeAmount = Yii::$app->request->get('rechargeAmount', 0);
  227. if ($rechargeAmount <= 0) {
  228. util::failInfo('请填写充值金额');
  229. }
  230. $rechargeAmount = round($rechargeAmount, 2);//四舍五入,保留二位小数
  231. $userId = $user['id'];
  232. $rechargeData['userId'] = $userId;
  233. $rechargeData['actPrice'] = $rechargeAmount;
  234. $rechargeData['payStyle'] = 0;
  235. $rechargeData['merchantId'] = $merchant['id'];
  236. $now = time();
  237. $expireTime = $now + 300;//订单5分钟后过期
  238. $rechargeData['createTime'] = date("Y-m-d H:i:s", $now);
  239. $rechargeData['addTime'] = time();
  240. $rechargeData['deadline'] = $expireTime;
  241. $rechargeData['id'] = stringUtil::generateOrderNo($account, $userId);
  242. $rechargeData['sourceType'] = 1;//订单来源:0商城订单 1付款订单
  243. $payment = xhRechargeService::add($rechargeData);
  244. $orderId = $payment['id'];
  245. Yii::$app->params['userId'] = $userId;
  246. Yii::$app->params['merchantId'] = $merchant['id'];
  247. UserService::updateVisitTime();
  248. $name = '充值';
  249. $totalFee = $rechargeAmount;
  250. $openId = $user['miniOpenId'];
  251. if (empty($openId)) {
  252. util::failInfo('miniOpenId empty');
  253. }
  254. $typeList = configDict::getConfig('capitalType');
  255. $capitalType = $typeList['xhRecharge']['id'];
  256. $attach = 'capitalType=' . $capitalType;//将流水类型、代金劵传过去
  257. $weixin = Yii::getAlias("@vendor/weixin");
  258. require_once($weixin . '/lib/WxPay.Api.php');
  259. require_once($weixin . '/example/WxPay.JsApiPay.php');
  260. $input = new \WxPayUnifiedOrder();
  261. $input->SetBody($name);
  262. $input->SetOut_trade_no($orderId);
  263. $input->SetTotal_fee($totalFee * 100);
  264. $input->SetTime_start(date("YmdHis", $now));
  265. $input->SetAttach($attach);
  266. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  267. $input->SetNotify_url(Yii::$app->params['miniUrl'] . '/notice/weixin-recharge-callback/');
  268. $input->SetTrade_type("JSAPI");
  269. //服务商 代设置微信支付,要添加的参数设置
  270. if ($extend['generalMerchant'] == 1) {
  271. //$input->SetOpenid($openId);
  272. $input->SetSub_openid($openId);
  273. //自设置 微信支付
  274. } else {
  275. $input->SetOpenid($openId);
  276. }
  277. //这里用的wxAppId是小程序的appId
  278. $extend['wxAppId'] = $extend['miniAppId'];
  279. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $extend);
  280. $tools = new \JsApiPay();
  281. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $extend);
  282. $newParams = json_decode($jsApiParameters, true);
  283. $newParams['orderId'] = $orderId;
  284. util::sendJson($newParams);
  285. }
  286. }