OrderController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. namespace client\controllers;
  3. use biz\goods\services\CategoryService;
  4. use biz\goods\services\GoodsCategoryService;
  5. use biz\goods\services\GoodsService;
  6. use biz\merchant\services\MerchantExtendService;
  7. use biz\merchant\services\MerchantService;
  8. use biz\merchant\services\ShopService;
  9. use biz\order\services\OrderGoodsService;
  10. use biz\order\services\OrderService;
  11. use biz\promote\services\CouponService;
  12. use biz\saas\services\RegionService;
  13. use biz\user\services\UserAssetService;
  14. use biz\user\services\UserService;
  15. use common\components\configDict;
  16. use common\components\stringUtil;
  17. use common\services\xhPayToolService;
  18. use Yii;
  19. use yii\web\Controller;
  20. use common\components\util;
  21. class OrderController extends BaseController
  22. {
  23. //下单要用到的相关信息 shish 2019.12.6
  24. public function actionOrderRelate()
  25. {
  26. $regionTree = RegionService::tree();
  27. $shop = ShopService::getDefaultShopInfo($this->merchant);
  28. $freight = MerchantExtendService::getFreight($this->merchantExtend);
  29. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree];
  30. util::success($out);
  31. }
  32. //商城下单操作 shish 2019.12.3
  33. public function actionCreateOrder()
  34. {
  35. $post = Yii::$app->request->post();
  36. $goodsId = isset($post['goodsId']) ? $post['goodsId'] : 0;
  37. $goodsStyleId = isset($post['goodsStyleId']) ? $post['goodsStyleId'] : 0;
  38. $goodsNum = isset($post['goodsNum']) && $post['goodsNum'] > 0 ? $post['goodsNum'] : 1;
  39. if (empty($goodsId)) {
  40. util::fail('请选择商品');
  41. }
  42. $goodsInfo = GoodsService::getGoodsInfo($goodsId);
  43. if (empty($goodsInfo)) {
  44. util::fail('没有找到商品');
  45. }
  46. //确认是否要配送的商品
  47. $needSend = isset($goodsInfo['needSend']) && $goodsInfo['needSend'] == 1 ? 1 : 0;
  48. $post['userId'] = $this->userId;
  49. $post['payWay'] = $this->isWeixin == true ? 0 : 1;
  50. $defaultShopId = MerchantService::getDefaultShopId($this->merchant);
  51. if (empty($defaultShopId)) {
  52. util::fail('没有找到门店');
  53. }
  54. $shopInfo = ShopService::getById($defaultShopId);
  55. //计算运费
  56. $post['sendDistance'] = isset($post['sendDistance']) ? $post['sendDistance'] : 0;
  57. $sendCost = 0;
  58. if ($needSend == 1) {
  59. $lat = $post['receiveLat'];//收货人纬度
  60. $lng = $post['receiveLong'];//收货人经度
  61. $jsStatDistance = $post['sendDistance'];
  62. $sendCost = ShopService::getSendCost($lat, $lng, $jsStatDistance, $shopInfo);
  63. }
  64. $post['sendCost'] = $sendCost;
  65. $post['merchantId'] = $this->merchantId;
  66. $post['shopId'] = $defaultShopId;
  67. //计算总金额
  68. $unitPrice = $goodsInfo['price'];
  69. $goodsStyleList = isset($goodsInfo['goodsStyleList']) ? $goodsInfo['goodsStyleList'] : [];
  70. if (!empty($goodsStyleId)) {
  71. if (empty($goodsStyleList)) {
  72. util::fail('商品款式没有找到');
  73. }
  74. $styleIdList = array_keys($goodsStyleList);
  75. if (in_array($goodsId, $styleIdList) == false) {
  76. util::fail('商品款式没有找到!');
  77. }
  78. $unitPrice = $goodsStyleList['price'];
  79. }
  80. $goodsPrice = $unitPrice * $goodsNum;
  81. $prePrice = stringUtil::calcAdd($sendCost, $goodsPrice);
  82. $actPrice = $prePrice;
  83. $showPrice = $prePrice;
  84. //计算优惠金额
  85. $couponId = isset($post['couponId']) && !empty($post['couponId']) ? $post['couponId'] : 0;
  86. //没有使用优惠券,默认使用会员优惠
  87. if (empty($couponId)) {
  88. $discountInfo = UserAssetService::getDiscount($this->userId);
  89. if (isset($discountInfo['member'])) {
  90. $discount = $discountInfo['member']['discount'];
  91. $actPrice = ($actPrice * ($discount * 10000)) / 10000;
  92. $showPrice = $actPrice;
  93. }
  94. } else {
  95. //验证优惠券有效性
  96. $discountAmount = CouponService::valid($couponId, $actPrice, $this->userId);
  97. //优惠券在支付成功后变更状态
  98. $showPrice = stringUtil::calcSub($actPrice, $discountAmount);
  99. }
  100. $now = time();
  101. $expireTime = $now + 1800;//订单30分钟后过期
  102. $post['deadline'] = $expireTime;
  103. $post['prePrice'] = $prePrice;
  104. $post['actPrice'] = $actPrice;
  105. $order = OrderService::addOrder($post);
  106. $orderId = $order['id'];
  107. $data = [
  108. 'orderId' => $orderId,
  109. 'goodsId' => $goodsId,
  110. 'userId' => $this->userId,
  111. 'merchantId' => $this->merchantId,
  112. 'title' => $goodsInfo['goodsName'],
  113. 'cover' => isset($goodsInfo['originImgList']) && !empty($goodsInfo['originImgList']) ? current($goodsInfo['originImgList']) : '',
  114. 'unitPrice' => $unitPrice,
  115. 'num' => $goodsNum,
  116. 'goodsStyleName' => isset($goodsInfo['goodsStyleList'][$goodsStyleId]['title']) ? $goodsInfo['goodsStyleList'][$goodsStyleId]['title'] : '',
  117. 'goodsStyleId' => $goodsStyleId,
  118. 'createTime' => date("Y-m-d H:i:s"),
  119. ];
  120. OrderGoodsService::add($data);//创建订单商品列表
  121. util::success(['orderId' => $orderId, 'totalPrice' => $showPrice, 'couponId' => $couponId]);
  122. }
  123. //余额支付 shish 2019.12.6
  124. public function actionBalancePay()
  125. {
  126. $post = Yii::$app->request->post();
  127. $orderId = isset($post['orderId']) ? $post['orderId'] : 0;
  128. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  129. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  130. $order = OrderService::getById($orderId);
  131. //dd(password_hash('123456', PASSWORD_BCRYPT));
  132. //验证支付密码是否正确
  133. UserService::validPayPassword($payPassword, $this->user);
  134. //验证订单有效性
  135. OrderService::valid($order, $this->userId);
  136. $typeList = configDict::getConfig('capitalType');
  137. $capitalType = $typeList['xhOrder']['id'];
  138. $totalFee = $order['actPrice'];
  139. xhPayToolService::balancePay($orderId, $totalFee, $capitalType, $couponId);
  140. util::success('支付成功');
  141. }
  142. //获取商城下单要用的微信支付参数 shish 2019.21.3
  143. public function actionWxPay()
  144. {
  145. ini_set('date.timezone', 'Asia/Shanghai');
  146. $post = Yii::$app->request->post();
  147. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  148. $orderId = isset($post['orderId']) ? $post['orderId'] : 0;
  149. $order = OrderService::getById($orderId);
  150. //验证订单有效性
  151. OrderService::valid($order, $this->userId);
  152. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  153. $totalFee = $order['actPrice'];
  154. $openId = $this->user['openId'];
  155. $typeList = configDict::getConfig('capitalType');
  156. $capitalType = $typeList['xhOrder']['id'];
  157. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType;//将流水类型、代金劵传过去
  158. $now = time();
  159. $expireTime = $now + 1800;//订单30分钟后过期
  160. $wx = Yii::getAlias("@vendor/weixin");
  161. require_once($wx . '/lib/WxPay.Api.php');
  162. require_once($wx . '/example/WxPay.JsApiPay.php');
  163. $input = new \WxPayUnifiedOrder();
  164. $input->SetBody($name);
  165. $input->SetOut_trade_no($orderId);
  166. $input->SetTotal_fee($totalFee * 100);
  167. $input->SetTime_start(date("YmdHis", $now));
  168. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  169. $input->SetTime_expire(date("YmdHis", $expireTime));
  170. $input->SetNotify_url($this->frontUrl . '/notice/weixin-callback/');
  171. $input->SetTrade_type("JSAPI");
  172. //服务商 代设置微信支付,要添加的参数设置
  173. if ($this->merchantExtend['generalMerchant'] == 1) {
  174. $input->SetSub_openid($openId);
  175. //自设置 微信支付
  176. } else {
  177. $input->SetOpenid($openId);
  178. }
  179. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $this->merchantExtend);
  180. $tools = new \JsApiPay();
  181. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $this->merchantExtend);
  182. $newParams = json_decode($jsApiParameters, true);
  183. //已请求微信不能修改价格
  184. $updateData = [];
  185. $updateData['modPriceStatus'] = 1;
  186. if (empty($order['deadline'])) {
  187. $updateData['deadline'] = $expireTime;
  188. }
  189. OrderService::updateById($orderId, $updateData);
  190. util::success($newParams);
  191. }
  192. //快速付款订单
  193. public function actionFastWxPay()
  194. {
  195. ini_set('date.timezone', 'Asia/Shanghai');
  196. $post = Yii::$app->request->post();
  197. $payWay = isset($post['payWay']) ? $post['payWay'] : '';
  198. $shopId = isset($post['shopId']) ? $post['shopId'] : 0;
  199. //来源 0自建商城 1门店 2微信朋友圈 3淘宝
  200. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  201. $shopInfo = xhShop::find()->where(['id' => $shopId])->one();
  202. if (empty($shopInfo) || $shopInfo->merchantId != $this->merchantId) {
  203. util::fail('门店无效');
  204. }
  205. unset($post['_csrf']);
  206. unset($post['payWay']);
  207. $post['userId'] = $this->userId;
  208. $prePrice = round($post['prePrice'], 2);//四舍五入,保留二位小数
  209. $userAsset = xhUserAssetService::getByUserId($this->userId);
  210. $level = isset($userAsset['memberLevel']) ? $userAsset['memberLevel'] : 0;
  211. $merchantExtend = xhMerchantExtendService::getByMerchantId($this->merchantId);
  212. $memberIntegral = xhMerchantExtendService::getGradeList($merchantExtend, 'desc');
  213. Yii::info(json_encode($merchantExtend));
  214. Yii::info(json_encode($memberIntegral));
  215. $discount = isset($memberIntegral[$level]['discount']) ? $memberIntegral[$level]['discount'] : $memberIntegral[$level]['discount'];
  216. $actPrice = $prePrice;
  217. $couponId = isset($post['couponId']) && !empty($post['couponId']) ? $post['couponId'] : 0;
  218. Yii::info($this->userId . ' ' . $level . ' ' . $discount);
  219. //没有优惠券才能使用会员折扣 shish 2019.8.30
  220. if (!empty($discount) && $prePrice >= 1 && empty($couponId)) {
  221. $currentPrice = ($prePrice * $discount) / 100;
  222. $actPrice = substr(sprintf("%.3f", $currentPrice), 0, -2);
  223. }
  224. //没有优惠卷,微信朋友圈付款才给随机优惠金额 shish 2019.9.12
  225. if ($sourceType == 2 && empty($couponId)) {
  226. $discountAmount = OrderService::getRandDiscount($actPrice);
  227. //测试环境直接随机优惠0.01
  228. if (isset(Yii::$app->params['merchant'])) {
  229. $merchant = Yii::$app->params['merchant'];
  230. if ($merchant['merchantName'] == '花美灵') {
  231. $discountAmount = 0.01;
  232. }
  233. }
  234. $actPrice = stringUtil::calcSub($actPrice, $discountAmount);
  235. $post['discountType'] = 2;//优惠类型是:付款随机优惠
  236. $post['discountAmount'] = $discountAmount;
  237. }
  238. $post['actPrice'] = $actPrice;
  239. $post['payStyle'] = 0;
  240. $post['merchantId'] = $this->merchantId;
  241. $now = time();
  242. $expireTime = $now + 300;//订单5分钟后过期
  243. $post['createTime'] = date("Y-m-d H:i:s", $now);
  244. $post['deadline'] = $expireTime;
  245. if (!empty($couponId)) {
  246. $time = time();
  247. $coupon = CouponService::getById($couponId);
  248. if ($coupon['deadline'] <= $time) {
  249. util::fail('优惠卷已经过期');
  250. }
  251. if ($coupon['status'] == 1) {
  252. util::fail('优惠卷已经使用');
  253. }
  254. if ($coupon['meetAmount'] > $post['prePrice']) {
  255. util::fail('消费金额不足' . $coupon['meetAmount'] . '元');
  256. }
  257. $post['actPrice'] = stringUtil::calcSub($post['prePrice'], $coupon['amount']);//浮点相减
  258. $post['discountAmount'] = $coupon['amount'];
  259. $post['discountType'] = 0;
  260. }
  261. if ($post['actPrice'] <= 0) {
  262. util::fail('消费金额太低');
  263. }
  264. if ($this->isWeixin) {
  265. $post['modPriceStatus'] = 1;//微信已经提交不能再修改价格
  266. }
  267. $post['sourceType'] = $sourceType;
  268. $post['id'] = stringUtil::generateOrderNo($this->merchantId, $this->userId);
  269. $payment = xhOrderService::add($post);
  270. $orderId = $payment['id'];
  271. if ($payWay == 'balance') {//余额支付
  272. util::success(['orderId' => $orderId]);
  273. }
  274. $name = '购买商品';
  275. $totalFee = $post['actPrice'];
  276. if ($this->isWeixin) {
  277. $userId = $post['userId'];
  278. $user = xhUserService::getById($userId);
  279. if ($user['merchantId'] != $this->merchantId) {
  280. util::fail('非法用户');
  281. }
  282. $openId = $user['openId'];
  283. $typeList = configDict::getConfig('capitalType');
  284. $capitalType = $typeList['xhOrder']['id'];
  285. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId;//将流水类型、优惠卷传过去
  286. $wx = Yii::getAlias("@vendor/weixin");
  287. require_once($wx . '/lib/WxPay.Api.php');
  288. require_once($wx . '/example/WxPay.JsApiPay.php');
  289. $input = new \WxPayUnifiedOrder();
  290. $input->SetBody($name);
  291. $input->SetOut_trade_no($orderId);
  292. $input->SetTotal_fee($totalFee * 100);
  293. $input->SetTime_start(date("YmdHis", $now));
  294. $input->SetAttach($attach);
  295. $input->SetTime_expire(date("YmdHis", $expireTime));//设置订单有效期5分钟
  296. $input->SetNotify_url($this->frontUrl . '/notice/weixin-callback/');
  297. $input->SetTrade_type("JSAPI");
  298. //服务商 代设置微信支付,要添加的参数设置
  299. if ($this->merchantExtend['generalMerchant'] == 1) {
  300. //$input->SetOpenid($openId);
  301. $input->SetSub_openid($openId);
  302. //自设置 微信支付
  303. } else {
  304. $input->SetOpenid($openId);
  305. }
  306. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $this->merchantExtend);
  307. $tools = new \JsApiPay();
  308. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $this->merchantExtend);
  309. $newParams = json_decode($jsApiParameters, true);
  310. $newParams['orderId'] = $orderId;
  311. util::success($newParams);
  312. }
  313. util::success(['orderId' => $orderId]);
  314. }
  315. //快捷支付使用支付宝付款 shish 2019.12.3
  316. public function actionFastZfbPay()
  317. {
  318. header("Content-type: text/html; charset=utf-8");
  319. $get = Yii::$app->request->get();
  320. $orderId = isset($get['orderId']) ? $get['orderId'] : 0;
  321. $couponId = isset($get['couponId']) ? $get['couponId'] : 0;
  322. $pay = xhOrderService::getById($orderId);
  323. $merchantId = $pay['merchantId'];
  324. if ($merchantId == 12358) {
  325. $config = array(
  326. //应用ID,您的APPID。
  327. 'app_id' => "2019041663930158",
  328. //商户私钥,您的原始格式RSA私钥
  329. '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==",
  330. //异步通知地址
  331. 'notify_url' => Yii::$app->params['frontUrl'] . "/notice/alipay-async",
  332. //同步跳转
  333. 'return_url' => "",
  334. //编码格式
  335. 'charset' => "UTF-8",
  336. //签名方式
  337. 'sign_type' => "RSA2",
  338. //支付宝网关
  339. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  340. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  341. 'alipay_public_key' => "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkFYqNkoWJIx37fGcUBbnZJ+APf14YHO1cBdp9RFSasxj9gm+pwbtK3J6uRuPygK0K/OCgvtuFJ6mZT3tNUEgSZMjnre5Z7IzQnzIBUYFPUEO85s+/RDr5HnAqgOIbu9GL8GWrU6g6uj+QmW591vXckmsbujPx0S3IP+TMOQZWA0JVvDu+T9yGfv3pacKUgdNvYnas/si3278TL4DeIVjZ4OmbbEPmEhD/OhSVlTSWEOdqLuV4DvEpmrGa8XoyfFEtgV9qSVb454Qwu5Nw/ATc9+ssMbN6ROXzOQbAAfqAe6riNUtKEHGVaCynfUAYNyR6yvJb8+VkMR02nBEDC9QHQIDAQAB",
  342. );
  343. } else {
  344. $config = array(
  345. //应用ID,您的APPID。
  346. 'app_id' => "2017041906821571",
  347. //商户私钥,您的原始格式RSA私钥
  348. '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==",
  349. //异步通知地址
  350. 'notify_url' => Yii::$app->params['frontUrl'] . "/notice/alipay-async",
  351. //同步跳转
  352. 'return_url' => "",
  353. //编码格式
  354. 'charset' => "UTF-8",
  355. //签名方式
  356. 'sign_type' => "RSA2",
  357. //支付宝网关
  358. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  359. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  360. 'alipay_public_key' => "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsz3m9juAR1xew4DY6c34gvbwNg8pZ92f932tseKs+4+pF0e+jTDiUo/xHImNDi1KXt1B+s3LxY9L/sxEksbavwmhgbz/igN1cAEwS2YM+Gnf0csDrxAPJhoKL5FTwxPEQ/8VSgroU+6GlF3LAx4VHa1qfn5MqRPfLroJcyPDyGfNe9vna2FnO4E/PH+hVbvPUAwX3XrUvJIm4OgY0JE3HYFlu+O7F4Ln6+Sl+Zv/ZE2nZAvNiyAwW5iVKhKXLieExqwd0NdrP28baBqkkIY+tmV7butGjB52iK1UbU0+1uiaPL4xBxRy6d0LZmYlvdVHp0JRUuwxBLChGgOCo/76DQIDAQAB",
  361. );
  362. }
  363. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  364. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  365. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  366. //require_once($alipayWap . '/config.php');
  367. $totalFee = $pay['actPrice'];
  368. $out_trade_no = $orderId;//商户订单号,商户网站订单系统中唯一订单号,必填
  369. $subject = '购买商品';//订单名称,必填
  370. $total_amount = $totalFee;//付款金额,必填
  371. $body = '';//商品描述,可空
  372. $timeout_express = "1m";//超时时间
  373. $typeList = configDict::getConfig('capitalType');
  374. $capitalType = $typeList['xhOrder']['id'];
  375. $passbackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&merchantId=' . $merchantId;//将流水类型、优惠卷传过去
  376. $passbackParams = urlencode($passbackParams);
  377. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  378. $payRequestBuilder->setBody($body);
  379. $payRequestBuilder->setSubject($subject);
  380. $payRequestBuilder->setOutTradeNo($out_trade_no);
  381. $payRequestBuilder->setTotalAmount($total_amount);
  382. $payRequestBuilder->setTimeExpress($timeout_express);
  383. //在异步通知时将该参数原样返回
  384. $payRequestBuilder->setPassbackParams($passbackParams);
  385. //同步通知
  386. $returnUrl = Yii::$app->params['frontUrl'] . "/notice/pay-alipay-sync";
  387. //异步通知
  388. $notifyUrl = Yii::$app->params['frontUrl'] . "/notice/alipay-async";
  389. $payResponse = new \AlipayTradeService($config);
  390. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  391. util::success($result);
  392. }
  393. /**
  394. * 订单 - 支付宝支付
  395. */
  396. public function actionZfbPay()
  397. {
  398. header("Content-type: text/html; charset=utf-8");
  399. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  400. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  401. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  402. require_once($alipayWap . '/config.php');
  403. $get = Yii::$app->request->get();
  404. $orderId = isset($get['orderId']) ? $get['orderId'] : 0;
  405. $couponId = isset($get['couponId']) ? $get['couponId'] : 0;
  406. $orderData = xhOrderService::getById($orderId);
  407. $waitForPay = configDict::getConfig('payStatus', 'waitForPay');
  408. if ($orderData['payStatus'] != $waitForPay) {
  409. echo '已经支付成功';
  410. util::end();
  411. }
  412. $merchantId = $orderData['merchantId'];
  413. $out_trade_no = $orderId;//商户订单号,商户网站订单系统中唯一订单号,必填
  414. $subject = $orderData['orderName'];//订单名称,必填
  415. $total_amount = $orderData['actPrice'];;//付款金额,必填
  416. $body = '';//商品描述,可空
  417. $timeout_express = "1m";//超时时间
  418. $typeList = configDict::getConfig('capitalType');
  419. $capitalType = $typeList['xhOrder']['id'];
  420. $passbackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&merchantId=' . $merchantId;//将流水类型、代金劵传过去
  421. $passbackParams = urlencode($passbackParams);
  422. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  423. $payRequestBuilder->setBody($body);
  424. $payRequestBuilder->setSubject($subject);
  425. $payRequestBuilder->setOutTradeNo($out_trade_no);
  426. $payRequestBuilder->setTotalAmount($total_amount);
  427. $payRequestBuilder->setTimeExpress($timeout_express);
  428. $payRequestBuilder->setPassbackParams($passbackParams);//在异步通知时将该参数原样返回
  429. $returnUrl = Yii::$app->params['frontUrl'] . "/notice/order-alipay-sync";
  430. $payResponse = new \AlipayTradeService($config);
  431. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $config['notify_url']);
  432. util::success($result);
  433. }
  434. }