CustomRechargeController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace hd\controllers;
  3. use bizGhs\custom\classes\AccountMoneyClass;
  4. use bizGhs\custom\classes\CustomClass;
  5. use bizGhs\custom\classes\CustomRechargeClass;
  6. use bizGhs\ghs\classes\GhsRechargeClass;
  7. use bizHd\ghs\classes\GhsClass;
  8. use bizHd\shop\classes\ShopClass;
  9. use common\components\lakala\Lakala;
  10. use common\components\dict;
  11. use common\components\noticeUtil;
  12. use common\components\orderSn;
  13. use common\components\util;
  14. use bizHd\wx\classes\WxOpenClass;
  15. use Yii;
  16. class CustomRechargeController extends BaseController
  17. {
  18. public $guestAccess = ['recharge'];
  19. //充值 ssh 20240507
  20. public function actionRecharge()
  21. {
  22. //util::fail('请下午2点后再试');
  23. ini_set('date.timezone', 'Asia/Shanghai');
  24. $post = Yii::$app->request->post();
  25. $payWay = $post['payWay'] ?? 0;
  26. $actPrice = $post['actPrice'] ?? 0;
  27. if ($actPrice <= 0) {
  28. util::fail('请输入数值');
  29. }
  30. $actPrice = floor($actPrice * 100) / 100;//只保留2位小数
  31. $totalFee = $actPrice;
  32. $ghsId = $post['ghsId'] ?? 0;
  33. $salt = $post['salt'] ?? '';
  34. $ghs = GhsClass::getLockById($ghsId);
  35. if (empty($ghs)) {
  36. util::fail('没有找到供货商哦!');
  37. }
  38. if ($ghs->salt != $salt) {
  39. util::fail('不是你的供货商哦');
  40. }
  41. $customId = $ghs->customId ?? 0;
  42. $custom = CustomClass::getLockById($customId);
  43. if (empty($custom)) {
  44. util::fail('供货商信息不完整那');
  45. }
  46. //检查余额是否有问题
  47. //GhsClass::checkBalance($ghs, $custom);
  48. // 花店自助充值建单前:挂账并入净 balance
  49. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  50. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  51. $capitalType = dict::getDict('capitalType', 'customRechargeToGhs', 'id');
  52. $customOrderSn = orderSn::getGhsCustomRechargeSn();
  53. $customShopId = $custom->shopId ?? 0;
  54. $customMainId = $custom->mainId ?? 0;
  55. $customBalance = $custom->balance ?? 0;
  56. $customName = $custom->name ?? '';
  57. $ghsShopId = $ghs->shopId ?? 0;
  58. $ghsMainId = $ghs->mainId ?? 0;
  59. $ghsBalance = $ghs->balance ?? 0;
  60. $ghsName = $ghs->name ?? '';
  61. $ghsShop = ShopClass::getById($ghsShopId, true);
  62. if (empty($ghsShop)) {
  63. util::fail('供货商信息不完全哦');
  64. }
  65. $customStaffId = 0;
  66. $customStaffName = '';
  67. if (isset($this->shopAdmin) && !empty($this->shopAdmin)) {
  68. $staff = $this->shopAdmin;
  69. $customStaffId = $staff->id ?? 0;
  70. $customStaffName = $staff->name ?? '';
  71. }
  72. $cData = [
  73. 'orderSn' => $customOrderSn,
  74. 'onlinePay' => 2,
  75. 'payWay' => $payWay,
  76. 'shopId' => $ghsShopId,
  77. 'mainId' => $ghsMainId,
  78. 'amount' => $actPrice,
  79. 'balance' => $customBalance,
  80. 'side' => 0,
  81. 'staffId' => 0,
  82. 'staffName' => '',
  83. 'payStatus' => 0,
  84. 'status' => 0,
  85. 'remark' => '',
  86. 'ghsId' => $ghsId,
  87. 'ghsShopId' => $ghsShopId,
  88. 'ghsName' => $ghsName,
  89. 'customId' => $customId,
  90. 'customShopId' => $customShopId,
  91. 'customName' => $customName,
  92. 'remark' => '',
  93. ];
  94. $cRecharge = CustomRechargeClass::add($cData, true);
  95. $cRechargeId = $cRecharge->id ?? 0;
  96. $ghsOrderSn = orderSn::getGhsRechargeSn();
  97. $gData = [
  98. 'orderSn' => $ghsOrderSn,
  99. 'onlinePay' => 2,
  100. 'payWay' => $payWay,
  101. 'shopId' => $customShopId,
  102. 'mainId' => $customMainId,
  103. 'amount' => $actPrice,
  104. 'balance' => $ghsBalance,
  105. 'side' => 0,
  106. 'staffId' => $customStaffId,
  107. 'staffName' => $customStaffName,
  108. 'payStatus' => 0,
  109. 'status' => 0,
  110. 'remark' => '',
  111. 'ghsId' => $ghsId,
  112. 'ghsShopId' => $ghsShopId,
  113. 'ghsName' => $ghsName,
  114. 'customId' => $customId,
  115. 'customShopId' => $customShopId,
  116. 'customName' => $customName,
  117. 'remark' => '',
  118. ];
  119. $gRecharge = GhsRechargeClass::add($gData, true);
  120. $gRechargeId = $gRecharge->id ?? 0;
  121. $cRecharge->ghsRechargeId = $gRechargeId;
  122. $cRecharge->save();
  123. $gRecharge->customRechargeId = $cRechargeId;
  124. $gRecharge->save();
  125. $subject = '充值销账 单号' . $customOrderSn;
  126. if ($payWay == 0) {
  127. $openId = '';
  128. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  129. $openId = $post['currentMiniOpenId'];
  130. }
  131. if (empty($openId)) {
  132. if (isset($this->admin) && !empty($this->admin)) {
  133. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  134. $openId = $this->admin->miniOpenId;
  135. }
  136. }
  137. }
  138. if (empty($openId)) {
  139. $currentShop = ShopClass::getById($customShopId, true);
  140. $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
  141. $currentMobile = $currentShop->mobile ?? '';
  142. noticeUtil::push("花店向供货商充值时,发现没有openId。{$currentShopName} {$currentMobile}", '15280215347');
  143. util::fail('用户信息不完整');
  144. }
  145. $merchantExtend = WxOpenClass::getWxInfo();
  146. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  147. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  148. $params = [
  149. 'appid' => 'OP00002119',
  150. 'serial_no' => '018b08cfddbd',
  151. 'merchant_no' => $ghsShop->lklSjNo,
  152. 'term_no' => $ghsShop->lklScanTermNo,
  153. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  154. 'lklCertificatePath' => $lklCertificatePath,
  155. ];
  156. $laResource = new Lakala($params);
  157. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  158. $wxParams = [
  159. 'orderSn' => $customOrderSn,
  160. 'amount' => $totalFee,
  161. 'capitalType' => $capitalType,
  162. 'notifyUrl' => $notifyUrl,
  163. 'wxAppId' => $merchantExtend['miniAppId'],
  164. 'openId' => $openId,
  165. 'subject' => $subject,
  166. 'couponId' => 0,
  167. ];
  168. $response = $laResource->driveWxPay($wxParams);
  169. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  170. $errMsg = $response['msg'] ?? '';
  171. $errCode = $response['code'] ?? '';
  172. noticeUtil::push('编号19881507:' . $errCode . ' | ' . $errMsg . ' | ' . $ghsName . ' | ' . $customName . ' | ' . $actPrice, '15280215347');
  173. if($errCode == 'BBS11109'){
  174. util::fail('超过单笔限额,请分二笔支付');
  175. }
  176. util::fail('支付失败');
  177. }
  178. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  179. $appId = $newParams['app_id'] ?? '';
  180. $nonceStr = $newParams['nonce_str'] ?? '';
  181. $paySign = $newParams['pay_sign'] ?? '';
  182. $package = $newParams['package'] ?? '';
  183. $signType = $newParams['sign_type'] ?? '';
  184. $timeStamp = $newParams['time_stamp'] ?? '';
  185. $new = [
  186. 'id' => $gRechargeId,
  187. 'appId' => $appId,
  188. 'nonceStr' => $nonceStr,
  189. 'paySign' => $paySign,
  190. 'package' => $package,
  191. 'signType' => $signType,
  192. 'timeStamp' => $timeStamp,
  193. 'orderSn' => $customOrderSn,
  194. ];
  195. util::success($new);
  196. } elseif ($payWay == 1) {
  197. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  198. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  199. $params = [
  200. 'appid' => 'OP00002119',
  201. 'serial_no' => '018b08cfddbd',
  202. 'merchant_no' => $ghsShop->lklSjNo,
  203. 'term_no' => $ghsShop->lklB2BTermNo,
  204. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  205. 'lklCertificatePath' => $lklCertificatePath,
  206. ];
  207. $laResource = new Lakala($params);
  208. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
  209. $cashParams = [
  210. 'orderSn' => $customOrderSn,
  211. 'amount' => $totalFee,
  212. 'capitalType' => $capitalType,
  213. 'notifyUrl' => $notifyUrl,
  214. 'subject' => $subject,
  215. ];
  216. $response = $laResource->cashierPay($cashParams);
  217. if (isset($response['code']) == false || $response['code'] != '000000') {
  218. util::fail('支付失败了');
  219. }
  220. $url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  221. if (empty($url)) {
  222. util::fail('支付失败');
  223. }
  224. util::success(['url' => $url]);
  225. } else {
  226. util::fail('暂不支持的付款方式');
  227. }
  228. }
  229. }