PayController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. <?php
  2. namespace mobile\controllers;
  3. use biz\goods\services\CategoryRelateService;
  4. use biz\goods\services\UsageRelationService;
  5. use biz\order\services\OrderService;
  6. use biz\tool\services\CouponService;
  7. use biz\user\services\UserAssetService;
  8. use biz\weixin\services\WxSceneService;
  9. use common\components\qrCodeUtil;
  10. use common\models\xhOrder;
  11. use common\models\xhShop;
  12. use common\services\xhMerchantExtendService;
  13. use Faker\Provider\Base;
  14. use Yii;
  15. use yii\helpers\Json;
  16. use common\services\xhUserAssetService;
  17. use common\services\xhUserService;
  18. use common\services\xhCouponService;
  19. use common\services\xhPayToolService;
  20. use common\services\xhOrderService;
  21. use common\services\xhActiveService;
  22. use common\services\xhActiveTicketClassService;
  23. use common\components\util;
  24. use common\components\stringUtil;
  25. use common\components\configDict;
  26. class PayController extends BaseController
  27. {
  28. public $layout = 'centerMain';
  29. public $withoutLogin = ['success', 'exec-alipay', 'detail', 'get-params', 'receive-success', 'not-login-receive-info', 'not-login-save-receive-info'];
  30. public $enableCsrfValidation = false;
  31. public function actionIndex()
  32. {
  33. $request = Yii::$app->request;
  34. $id = $request->get('shopId', 0);
  35. $sourceType = Yii::$app->request->get('sourceType', 1);//默认1门店
  36. if (empty($id)) {
  37. util::fail('没有门店');
  38. }
  39. $userInfo = [];
  40. $userAsset = [];
  41. $userId = 0;
  42. $level = 0;
  43. $discount = 0;
  44. if ($this->isLogin) {
  45. $userInfo = $this->user;
  46. $userId = $this->userId;
  47. $userAsset = xhUserAssetService::getByUserId($userId);
  48. $level = isset($userAsset['memberLevel']) ? $userAsset['memberLevel'] : 0;
  49. $merchantExtend = xhMerchantExtendService::getByMerchantId($this->merchantId);
  50. $memberIntegral = xhMerchantExtendService::getGradeList($merchantExtend, 'desc');
  51. $discount = isset($memberIntegral[$level]['discount']) ? $memberIntegral[$level]['discount'] : $memberIntegral[$level]['discount'];
  52. }
  53. $usageList = UsageRelationService::getMyUsage();
  54. $categoryList = CategoryRelateService::getCategoryData();
  55. $hasCoupon = CouponService::hasCoupon();
  56. return $this->renderPartial('index', [
  57. 'shopId' => $id,
  58. 'userAsset' => $userAsset,
  59. 'userInfo' => $userInfo,
  60. 'userId' => $userId,
  61. 'level' => $level,
  62. 'discount' => $discount,
  63. 'usageList' => $usageList,
  64. 'categoryList' => $categoryList,
  65. 'hasCoupon' => $hasCoupon,
  66. 'sourceType' => $sourceType,
  67. ]);
  68. }
  69. public function actionSuccess()
  70. {
  71. $get = Yii::$app->request->get();
  72. $orderId = isset($get['orderId']) ? $get['orderId'] : 0;
  73. $payment = OrderService::getById($orderId, true);
  74. if (empty($payment)) {
  75. util::failInfo('订单号未找到');
  76. }
  77. if ($payment->merchantId != $this->merchantId) {
  78. util::stop('非法访问');
  79. }
  80. $payWay = configDict::getConfig('payWayFullName');
  81. /**确认是否可以领卷**/
  82. $userId = $payment->userId;
  83. $payCallback = $payment->payStatus == 1 ? true : false;
  84. $flag = UserAssetService::couldGetCoupon($userId, $payCallback);
  85. Yii::info('flag:' . $flag);
  86. $imgUrl = '';
  87. if ($flag != -1) {
  88. if (empty($payment->getCouponQrCode)) {
  89. $url = WxSceneService::generatePayGetCouponScene($userId);
  90. $logo = Yii::getAlias("@app") . '/../images' . $this->merchant['logo'];
  91. $imgUrl = qrCodeUtil::generate($url, 'pay_get_coupon_url_' . date("YmdH") . '_' . $this->merchantId . '_' . $userId, $logo);
  92. $payment->getCouponQrCode = $imgUrl;
  93. $payment->save();
  94. } else {
  95. $imgUrl = $payment->getCouponQrCode;
  96. }
  97. }
  98. return $this->renderPartial('success', ['payment' => $payment, 'payWay' => $payWay, 'flag' => $flag, 'imgUrl' => $imgUrl]);
  99. }
  100. /**
  101. * 支付订单明细
  102. */
  103. public function actionDetail()
  104. {
  105. $get = Yii::$app->request->get();
  106. $orderId = isset($get['orderId']) ? $get['orderId'] : 0;
  107. $payment = xhOrderService::getById($orderId);
  108. if ($payment['merchantId'] != $this->merchantId) {
  109. util::stop('非法访问');
  110. }
  111. $payWay = configDict::getConfig('payWayFullName');
  112. $couponId = $payment['couponId'];
  113. $coupon = [];
  114. if (!empty($couponId)) {
  115. $coupon = xhCouponService::getById($couponId);
  116. }
  117. return $this->renderPartial('detail', ['payment' => $payment, 'payWay' => $payWay, 'coupon' => $coupon]);
  118. }
  119. public function actionBalancePay()
  120. {
  121. $post = Yii::$app->request->post();
  122. $orderId = isset($post['orderId']) ? $post['orderId'] : 0;
  123. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  124. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  125. $order = xhOrderService::getById($orderId);
  126. if (empty($order) || $order['userId'] != $this->userId) {
  127. util::failInfo('订单资料不合法');
  128. }
  129. if ($this->user['payPassword'] != md5($payPassword)) {
  130. util::failInfo('密码错误 ');
  131. }
  132. $typeList = configDict::getConfig('capitalType');
  133. $capitalType = $typeList['xhOrder']['id'];
  134. $totalFee = $order['actPrice'];
  135. $return = xhPayToolService::balancePay($orderId, $totalFee, $capitalType, $couponId);
  136. echo Json::encode($return);
  137. }
  138. public function actionGetParams()
  139. {
  140. ini_set('date.timezone', 'Asia/Shanghai');
  141. $post = Yii::$app->request->post();
  142. $payWay = isset($post['payWay']) ? $post['payWay'] : '';
  143. $shopId = isset($post['shopId']) ? $post['shopId'] : 0;
  144. //来源 0自建商城 1门店 2微信朋友圈 3淘宝
  145. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  146. $shopInfo = xhShop::find()->where(['id' => $shopId])->one();
  147. if (empty($shopInfo) || $shopInfo->merchantId != $this->merchantId) {
  148. util::failInfo('门店无效');
  149. }
  150. unset($post['_csrf']);
  151. unset($post['payWay']);
  152. $post['userId'] = $this->userId;
  153. $prePrice = round($post['prePrice'], 2);//四舍五入,保留二位小数
  154. $userAsset = xhUserAssetService::getByUserId($this->userId);
  155. $level = isset($userAsset['memberLevel']) ? $userAsset['memberLevel'] : 0;
  156. $merchantExtend = xhMerchantExtendService::getByMerchantId($this->merchantId);
  157. $memberIntegral = xhMerchantExtendService::getGradeList($merchantExtend, 'desc');
  158. Yii::info(json_encode($merchantExtend));
  159. Yii::info(json_encode($memberIntegral));
  160. //$discount = isset($memberIntegral[$level]['discount']) ? $memberIntegral[$level]['discount'] : $memberIntegral[$level]['discount'];
  161. $discount = 0;
  162. $actPrice = $prePrice;
  163. $couponId = isset($post['couponId']) && !empty($post['couponId']) ? $post['couponId'] : 0;
  164. Yii::info($this->userId . ' ' . $level . ' ' . $discount);
  165. //没有优惠券才能使用会员折扣 shish 2019.8.30
  166. if (!empty($discount) && $prePrice >= 1 && empty($couponId)) {
  167. $currentPrice = ($prePrice * $discount) / 100;
  168. $actPrice = substr(sprintf("%.3f", $currentPrice), 0, -2);
  169. }
  170. //没有优惠卷,微信朋友圈付款才给随机优惠金额 shish 2019.9.12
  171. if ($sourceType == 2 && empty($couponId)) {
  172. $discountAmount = OrderService::getRandDiscount($actPrice);
  173. //测试环境直接随机优惠0.01
  174. if (isset(Yii::$app->params['merchant'])) {
  175. $merchant = Yii::$app->params['merchant'];
  176. if ($merchant['merchantName'] == '花美灵') {
  177. $discountAmount = 0.01;
  178. }
  179. }
  180. $actPrice = stringUtil::calcSub($actPrice, $discountAmount);
  181. $post['discountType'] = 2;//优惠类型是:付款随机优惠
  182. $post['discountAmount'] = $discountAmount;
  183. }
  184. $post['actPrice'] = $actPrice;
  185. $post['payStyle'] = 0;
  186. $post['merchantId'] = $this->merchantId;
  187. $now = time();
  188. $expireTime = $now + 300;//订单5分钟后过期
  189. $post['createTime'] = date("Y-m-d H:i:s", $now);
  190. $post['deadline'] = $expireTime;
  191. if (!empty($couponId)) {
  192. $time = time();
  193. $coupon = CouponService::getById($couponId);
  194. if ($coupon['deadline'] <= $time) {
  195. util::failInfo('优惠卷已经过期');
  196. }
  197. if ($coupon['status'] == 1) {
  198. util::failInfo('优惠卷已经使用');
  199. }
  200. if ($coupon['meetAmount'] > $post['prePrice']) {
  201. util::failInfo('消费金额不足' . $coupon['meetAmount'] . '元');
  202. }
  203. $post['actPrice'] = stringUtil::calcSub($post['prePrice'], $coupon['amount']);//浮点相减
  204. $post['discountAmount'] = $coupon['amount'];
  205. $post['discountType'] = 0;
  206. }
  207. if ($post['actPrice'] <= 0) {
  208. util::failInfo('消费金额太低');
  209. }
  210. if ($this->isWeixin) {
  211. $post['modPriceStatus'] = 1;//微信已经提交不能再修改价格
  212. }
  213. $post['sourceType'] = $sourceType;
  214. $post['id'] = stringUtil::generateOrderNo($this->merchantId, $this->userId);
  215. $payment = xhOrderService::add($post);
  216. $orderId = $payment['id'];
  217. if ($payWay == 'balance') {//余额支付
  218. util::successInfo('操作成功', 'A0001', ['orderId' => $orderId]);
  219. }
  220. $name = '购买商品';
  221. $totalFee = $post['actPrice'];
  222. if ($this->isWeixin) {
  223. $userId = $post['userId'];
  224. $user = xhUserService::getById($userId);
  225. if ($user['merchantId'] != $this->merchantId) {
  226. util::failInfo('非法用户');
  227. }
  228. $openId = $user['openId'];
  229. $typeList = configDict::getConfig('capitalType');
  230. $capitalType = $typeList['xhOrder']['id'];
  231. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId;//将流水类型、优惠卷传过去
  232. $weixin = Yii::getAlias("@vendor/weixin");
  233. require_once($weixin . '/lib/WxPay.Api.php');
  234. require_once($weixin . '/example/WxPay.JsApiPay.php');
  235. $input = new \WxPayUnifiedOrder();
  236. $input->SetBody($name);
  237. $input->SetOut_trade_no($orderId);
  238. $input->SetTotal_fee($totalFee * 100);
  239. $input->SetTime_start(date("YmdHis", $now));
  240. $input->SetAttach($attach);
  241. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  242. $input->SetNotify_url($this->frontUrl . '/notice/weixin-callback/');
  243. $input->SetTrade_type("JSAPI");
  244. //服务商 代设置微信支付,要添加的参数设置
  245. if ($this->merchantExtend['generalMerchant'] == 1) {
  246. //$input->SetOpenid($openId);
  247. $input->SetSub_openid($openId);
  248. //自设置 微信支付
  249. } else {
  250. $input->SetOpenid($openId);
  251. }
  252. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $this->merchantExtend);
  253. $tools = new \JsApiPay();
  254. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $this->merchantExtend);
  255. $newParams = json_decode($jsApiParameters, true);
  256. $newParams['orderId'] = $orderId;
  257. util::successInfo('操作成功', 'A0001', $newParams);
  258. }
  259. util::successInfo('操作成功', 'A0001', ['orderId' => $orderId]);
  260. }
  261. public function actionExecAlipay()
  262. {
  263. header("Content-type: text/html; charset=utf-8");
  264. $get = Yii::$app->request->get();
  265. $orderId = isset($get['orderId']) ? $get['orderId'] : 0;
  266. $couponId = isset($get['couponId']) ? $get['couponId'] : 0;
  267. $pay = xhOrderService::getById($orderId);
  268. $merchantId = $pay['merchantId'];
  269. if ($merchantId == 12358) {
  270. $config = array(
  271. //应用ID,您的APPID。
  272. 'app_id' => "2019041663930158",
  273. //商户私钥,您的原始格式RSA私钥
  274. 'merchant_private_key' => "MIIEpAIBAAKCAQEA0NGNBMu5BkHU+ztmGhGdsJC1n2ZbXn4uoaYNni2eriPHJHG8vCWL1vv3W+gxX3twnmdQ57s3hFWfd5PIwMm+8QIwiPucckbmTEm2rC2g4j+UnUiXzICJWjlzlCWiBftrsctKKwjsZTPiuDJzUKmYRD4DYWy1+4Uh6k6x98qbALuiFFrh7esvsaSepZfQ+zBLzMVfrtPABkwEtOAzUL4hz4YW+EFfqyj3mu4LP4M+zBWSKRs0iCzJILG/7JA4OEkDJ89gGZ6LiaITUgffV2tg/IS81aNBfutb3tICBr1PLlKYCV0ois54kqmmP7anNrn/KRFI/k6iLd82e1HEoHhvDwIDAQABAoIBAQCr+PgPTAv8CDl0Ek4bCAj7AaJiPTTgVEDpJc0vSNjXB2YZMIZD2RQaoHXtvgLzZMCx49pwjfHBzZZAL3h0tXHIIIqCNd15C8TcbRTBJe7KhZxKEB/b7ruvj4MNLhUKoi3mRcq2OGofSqTcF8h6VMGu6fd0w8f39YOh6N+Od9BBv8aL7CtBwQQihzatBdr2CyjH50TmlVWfEz2YKoZBGRuMy7EzLZgmR5IzEJC36tOBAg5DORNkEMAuzvnmBQznZCtzMZHW5Ytlh/3kbCUMsrWL6ioox/mel4DoQNnQ1kZ60f7Gny3I+0HiJ+vxVBZ3Nyd8FCVbAlq6avkB3ZGoCx0xAoGBAP/Mhf8X81mLK80TQsSWYPmnjnYkOZ7vIpNc/b6/N+BKAAjWB+0wXJ73nhuzB8cuJBz6TK8RE2uUuiUbiVP1jkrQ2EoCWadndKE1fcLFSSOYlN9rxfINcLicdNAFYu0m8He5cqPpW+06sD8HGOlWeBOyPvitXBjBeOB9dPWBpVkpAoGBAND7kruod1T4ErtOavYv4ErlyN0O6PpQR3MBlnzXnJfKLdaMvc/JaqjZtk14Nl9RnBDzN6d93LHxariyuTHw9mdGAPOpxcUEsUdOw56eRIzhyY8lz0c+lvE32r2NypuzK/2BGP5xR+spe7D4esuhLQhUYjByTWbgLQD+7724QrV3AoGAYuO2ib/AnEVpUYa4sTdRljJoqNOoUwEv5Lh2gF98QoFZMhFMTy37IJmpzhuQTjhQTcOWEbgQQe7lZ6MVnBe6QsIqW7I85rLgK9J6I+oRNGmwZA9OHx2DDlut7R2n+Pas0BwpbaSxnSyrJjKgNtTu5u5p2clraUaibGcT6DWOrsECgYEAspjM5aMrmGoJWBnEP3Da9ic6afD8Gi/RX+/TdA2vvekDE4BkFtfDV1n3+mzpyrwr7DBvN6zQlyICWqYirxOHAOtKlPJaGe3Qs2gUtdH8M4oifzuI0RIkXTGmtqgepsGQrq1NduXI2KgzFSLFjpDHs36qC00j6O9chqVYrYJzQDECgYBQyIWx3BBEUgWCbLTT8QeUdf+EnZSLZzIIQFn88zImhqkTSMbpBmcZpI4oOlWiM8i4XOaEzu+EDFIRAchIbVlDgBV1OprAN5BAez03oyh1JkCDYrsEj7FMqWSmbxiY2uubPshMLEGdPU+I46oJTO6FzMyG6hnQrK+/ne0zH8A5LA==",
  275. //异步通知地址
  276. 'notify_url' => Yii::$app->params['frontUrl'] . "/notice/alipay-async",
  277. //同步跳转
  278. 'return_url' => "",
  279. //编码格式
  280. 'charset' => "UTF-8",
  281. //签名方式
  282. 'sign_type' => "RSA2",
  283. //支付宝网关
  284. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  285. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  286. 'alipay_public_key' => "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkFYqNkoWJIx37fGcUBbnZJ+APf14YHO1cBdp9RFSasxj9gm+pwbtK3J6uRuPygK0K/OCgvtuFJ6mZT3tNUEgSZMjnre5Z7IzQnzIBUYFPUEO85s+/RDr5HnAqgOIbu9GL8GWrU6g6uj+QmW591vXckmsbujPx0S3IP+TMOQZWA0JVvDu+T9yGfv3pacKUgdNvYnas/si3278TL4DeIVjZ4OmbbEPmEhD/OhSVlTSWEOdqLuV4DvEpmrGa8XoyfFEtgV9qSVb454Qwu5Nw/ATc9+ssMbN6ROXzOQbAAfqAe6riNUtKEHGVaCynfUAYNyR6yvJb8+VkMR02nBEDC9QHQIDAQAB",
  287. );
  288. } else {
  289. $config = array(
  290. //应用ID,您的APPID。
  291. 'app_id' => "2017041906821571",
  292. //商户私钥,您的原始格式RSA私钥
  293. 'merchant_private_key' => "MIIEpAIBAAKCAQEAvbeu6o90L+0AilI+mbhuJJH0lUv7hWQCEC+XUc+cWlC9ulKC3cSVScVLJHpUc09aj6v/iD1p9iVfgMxjeADNVKiQC2RJyRshAjzyTq+2bTcODp619iV2y4qW8UM65VcP7Ide3P0uT5xC2ReW0HpkdHZgm4F+P7mh1VhGt9qOP3hjvoraAHhtGACvsGvy8H69HhSAG+CEmFt7f2tKNWJ8xEUvkfhDvys93PfTk4xMWj048hJE2r38+2mWI/Il5dFQHWXXTVl2x9yi7uhEEW42a+7s9Q3HgqEvZiQ2NI1FTeEAPLFBApiEsNLY/smaszwqqfWGAyTMvtQ6T4K96GcTLQIDAQABAoIBAHwYTj33n9RJfnT73x7F2KXrIsUVcmyKQh88QgqtdmRNNA1QM3HESLJ8bu5pZhwW5/HaW8dOBKWRRKsHBnlUbPrXV4FcFDeLm0fPfd+iZ/2AaZ1+ix962f3BpYIiq7+f9zaMRazfnw9L8x31pByyMktLs12EkoQ0dHsMxxUzzKAOiiPnvCn8Pv+Jb2qej8GXgCjefutCVNMWGsdwJtMVXEbpdmMa48Wvw+0In5ZS/UG39YAreFzgDqI9jWHE1ZhkGmB1+8hKpeIlEeVGAeYVveUgssMORdA/YY8SWHk26KSIFXBrmdKjAcRJ8Q7ZHUmzIqAV+IMV+RD4J5mZHeJgdAECgYEA8J+Hp1M6eYbT4CPLXa//yRP6Xo+qSzrKHaRB1LRAjYhvKpx/TQetYpNCJ5bWpa+Jp0T5R21pzuYaJkPuGlNEHA13b5T3r1l2ltomSEdaShvD5EZ9WtErYV5zpSv7FjjIGkbwa0H3A8Ydnvw5z4A9TZjjHLjC2GtVGJReqHGglMECgYEAydddA4NRPxmXvIjjmLb1Ft1qwHuyv3vvWhi50foGZXNEnBcfP/OkfyBDDpOEu34GZBMXK9ykHBs1l93OEFGXh7AwW0Lp5lARahBEfBZgPjq5yPO8TOJZrb5hVcuvoDdGjxpRh93XDnQDhWS/1IvUcBLcIsTcnVvfrkX4NLxF/W0CgYEAxamG+gD4rBQBwMImsRN+/2MV7M//iEUG+0qPeXeI/7rv9wUP3etMlwl48qSKNxj37xxN2ksa/Acxu/VZhu6XqKO3VUX+IWFQdaNGh2F13iLozIDLQOtKw3WfcjOq0xpZ5pwXq0RI8iSw+IUhyD8EHNZW2qU8CiRBhyt6hsywqQECgYBa8+06FAachI/XqWfF/UvcDdJ5AkS9/L8SvmmdsSkItjSIkfLHAqdxkbwl6Vu6kUOX/PJIFZjuAWTZFl4xBFNgFYj01uZHnnT6cnIp6HteD2CAqTSFAMqgfFWoL6zoaYAmJBnxO4oZPTYI+ilnQcts5VLFaChx0GCvS2BZgy2W0QKBgQDjP2VtRq4aVSLUq2z1KIDI7GJBbOM5KSW1OEj4sfEdViozPh3Ar/FQWiij1oCZF+iJdKkonHoDbDd+Q0ykcpQ+5Gv07iiT8wJYFR/0j8g2kfql7bVQhGSBeBMS5sawKR3CvkfW73WX+CNnf0PklTY0kTyt6WRXDnKSeyxFVwSZ7g==",
  294. //异步通知地址
  295. 'notify_url' => Yii::$app->params['frontUrl'] . "/notice/alipay-async",
  296. //同步跳转
  297. 'return_url' => "",
  298. //编码格式
  299. 'charset' => "UTF-8",
  300. //签名方式
  301. 'sign_type' => "RSA2",
  302. //支付宝网关
  303. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  304. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  305. 'alipay_public_key' => "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsz3m9juAR1xew4DY6c34gvbwNg8pZ92f932tseKs+4+pF0e+jTDiUo/xHImNDi1KXt1B+s3LxY9L/sxEksbavwmhgbz/igN1cAEwS2YM+Gnf0csDrxAPJhoKL5FTwxPEQ/8VSgroU+6GlF3LAx4VHa1qfn5MqRPfLroJcyPDyGfNe9vna2FnO4E/PH+hVbvPUAwX3XrUvJIm4OgY0JE3HYFlu+O7F4Ln6+Sl+Zv/ZE2nZAvNiyAwW5iVKhKXLieExqwd0NdrP28baBqkkIY+tmV7butGjB52iK1UbU0+1uiaPL4xBxRy6d0LZmYlvdVHp0JRUuwxBLChGgOCo/76DQIDAQAB",
  306. );
  307. }
  308. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  309. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  310. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  311. //require_once($alipayWap . '/config.php');
  312. $totalFee = $pay['actPrice'];
  313. $out_trade_no = $orderId;//商户订单号,商户网站订单系统中唯一订单号,必填
  314. $subject = '购买商品';//订单名称,必填
  315. $total_amount = $totalFee;//付款金额,必填
  316. $body = '';//商品描述,可空
  317. $timeout_express = "1m";//超时时间
  318. $typeList = configDict::getConfig('capitalType');
  319. $capitalType = $typeList['xhOrder']['id'];
  320. $passbackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&merchantId=' . $merchantId;//将流水类型、优惠卷传过去
  321. $passbackParams = urlencode($passbackParams);
  322. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  323. $payRequestBuilder->setBody($body);
  324. $payRequestBuilder->setSubject($subject);
  325. $payRequestBuilder->setOutTradeNo($out_trade_no);
  326. $payRequestBuilder->setTotalAmount($total_amount);
  327. $payRequestBuilder->setTimeExpress($timeout_express);
  328. //在异步通知时将该参数原样返回
  329. $payRequestBuilder->setPassbackParams($passbackParams);
  330. //同步通知
  331. $returnUrl = Yii::$app->params['frontUrl'] . "/notice/pay-alipay-sync";
  332. //异步通知
  333. $notifyUrl = Yii::$app->params['frontUrl'] . "/notice/alipay-async";
  334. $payResponse = new \AlipayTradeService($config);
  335. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  336. $this->renderPartial('execAlipay', ['result' => $result]);
  337. }
  338. public function actionActive()
  339. {
  340. $get = Yii::$app->request->get();
  341. $id = isset($get['id']) ? $get['id'] : 0;
  342. $active = xhActiveService::getById($id);
  343. if ($active['merchantId'] != $this->merchantId) {
  344. util::stop('非法访问。');
  345. }
  346. $ticketIdList = explode(',', $active['classIdList']);
  347. $ticketId = $ticketIdList[0];//目前阶段,创建的活动,只有一种票
  348. $ticketClass = xhActiveTicketClassService::getById($ticketId);
  349. if (empty($ticketClass) || $ticketClass['merchantId'] != $this->merchantId) {
  350. util::stop('非法访问!');
  351. }
  352. $apply = false;
  353. return $this->render('active', ['ticketClass' => $ticketClass, 'apply' => $apply, 'user' => $this->user, 'active' => $active]);
  354. }
  355. /**
  356. * (必须登录状态)填写收货人信息
  357. */
  358. public function actionReceiveInfo()
  359. {
  360. $get = Yii::$app->request->get();
  361. $payId = $get['payId'];
  362. if (empty($payId)) {
  363. return $this->renderPartial('noGoodsReceiveInfo');
  364. }
  365. $orderData = xhOrderService::getById($payId, $this->merchantId);
  366. if (empty($orderData)) {
  367. util::failInfo('订单号未找到');
  368. }
  369. if ($orderData['userId'] != 0 && $orderData['userId'] != $this->userId) {//当 某用户操作后,就只能该用户访问
  370. util::failInfo('不是您的订单');
  371. }
  372. $orderData['reachTime'] = substr($orderData['reachTime'], 0, 10);
  373. $orderData['isPayed'] = 0;
  374. $waitForPay = configDict::getConfig('payStatus', 'waitForPay');
  375. if ($orderData['payStatus'] != $waitForPay) {
  376. $orderData['isPayed'] = 1;
  377. }
  378. return $this->renderPartial('receiveInfo', ['orderData' => $orderData, 'merchant' => $this->merchant, 'isLogin' => $this->isLogin]);
  379. }
  380. /**
  381. * (必须登录状态)保存收货人信息成功
  382. */
  383. public function actionSaveReceiveInfo()
  384. {
  385. $payAdd = [];
  386. $post = Yii::$app->request->post();
  387. //dump($post);die;
  388. $payId = $post['payId'];
  389. $paymentData = xhOrderService::getById($payId, $this->merchantId);
  390. if (empty($paymentData)) {
  391. util::failInfo('订单号未找到');
  392. }
  393. if ($paymentData['userId'] != 0 && $paymentData['userId'] != $this->userId) {//当 快速下单时,字段userId为0(未保存指定用户);用户操作后,就只能该用户访问
  394. util::failInfo('不是您的订单');
  395. }
  396. if ($paymentData['userId'] == 0) {//保存用户id
  397. $payAdd['userId'] = $this->userId;
  398. }
  399. xhOrderService::formatPost($payAdd, $post);
  400. unset($payAdd['orderName']);//不更新商品名称
  401. unset($payAdd['actPrice']);//不更新价格
  402. $re = xhOrderService::updateById($payId, $payAdd);
  403. $data['payStatus'] = $paymentData['payStatus'];//支付状态
  404. $data['orderId'] = $payId;
  405. if ($re['status']) {
  406. util::successInfo('保存成功', 'A0001', $data);
  407. } else {
  408. util::failInfo('保存成功', 'A0003', $data);
  409. }
  410. }
  411. /**
  412. * 支付并填写收花信息、保存收货人信息 -- 成功页
  413. */
  414. public function actionReceiveSuccess()
  415. {
  416. $direction = Yii::$app->request->get('direction');
  417. switch ($direction) {
  418. case 'paySuccess':
  419. return $this->renderPartial('paySuccess');
  420. case 'updateSuccess':
  421. return $this->renderPartial('updateSuccess');
  422. }
  423. }
  424. /**
  425. * (未登录状态)填写收货人信息
  426. */
  427. public function actionNotLoginReceiveInfo()
  428. {
  429. $get = Yii::$app->request->get();
  430. $payId = $get['payId'];
  431. $payWay = isset($get['payWay']) ? $get['payWay'] : '';
  432. if (empty($payId)) {
  433. return $this->renderPartial('noGoodsReceiveInfo');
  434. }
  435. $orderData = xhOrderService::getById($payId, $this->merchantId);
  436. if (!empty($payWay) && ($payWay == 'zfb' || $payWay == 'weixin')) {//微信 或 支付宝跳转的链接
  437. if ($orderData['payStatus'] != 1) {
  438. util::failInfo('订单号支付失败');
  439. }
  440. }
  441. if (empty($orderData)) {
  442. util::failInfo('订单号未找到');
  443. }
  444. if ($orderData['userId'] != 0 && $orderData['userId'] != $this->userId) {//当 某用户操作后,就只能该用户访问
  445. util::failInfo('不是您的订单');
  446. }
  447. //session校验
  448. $orderId = Yii::$app->session->get('orderId_' . $payId);
  449. if (empty($orderId)) {
  450. util::failInfo('订单号有效期已过,请联系卖家');
  451. }
  452. if ($orderId != $payId) {
  453. util::failInfo('订单号不匹配');
  454. }
  455. $orderData['reachTime'] = substr($orderData['reachTime'], 0, 10);
  456. $orderData['isPayed'] = 0;
  457. $waitForPay = configDict::getConfig('payStatus', 'waitForPay');
  458. if ($orderData['payStatus'] != $waitForPay) {
  459. $orderData['isPayed'] = 1;
  460. if ($this->isWeixin) {
  461. } else {
  462. }
  463. }
  464. return $this->renderPartial('receiveInfo', ['orderData' => $orderData, 'merchant' => $this->merchant, 'isLogin' => $this->isLogin]);
  465. }
  466. /**
  467. * (未登录状态)保存收货人信息成功
  468. */
  469. public function actionNotLoginSaveReceiveInfo()
  470. {
  471. $payAdd = [];
  472. $post = Yii::$app->request->post();
  473. $payId = $post['payId'];
  474. $paymentData = xhOrderService::getById($payId, $this->merchantId);
  475. if (empty($paymentData)) {
  476. util::failInfo('订单号未找到');
  477. }
  478. if ($paymentData['userId'] != 0 && $paymentData['userId'] != $this->userId) {//当 快速下单时,字段userId为0(未保存指定用户);用户操作后,就只能该用户访问
  479. util::failInfo('不是您的订单');
  480. }
  481. //session校验
  482. $orderId = Yii::$app->session->get('orderId_' . $payId);
  483. if (empty($orderId)) {
  484. util::failInfo('订单号有效期已过,请联系卖家');
  485. }
  486. if ($orderId != $payId) {
  487. util::failInfo('订单号不匹配');
  488. }
  489. $payAdd['userId'] = 0;
  490. xhOrderService::formatPost($payAdd, $post);
  491. unset($payAdd['orderName']);//不更新商品名称
  492. unset($payAdd['actPrice']);//不更新价格
  493. $re = xhOrderService::updateById($payId, $payAdd);
  494. if ($re) {
  495. util::successInfo('保存成功');
  496. } else {
  497. util::failInfo('表单数据未变更', 'A0003');
  498. }
  499. }
  500. //取我的优惠卷 shish 2019.8.30
  501. public function actionGetMyCoupon()
  502. {
  503. $get = Yii::$app->request->get();
  504. $couponList = CouponService::getAvailableCouponList($get);
  505. util::success($couponList);
  506. }
  507. }