PayController.php 23 KB

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