OrderController.php 21 KB

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