CustomRechargeController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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::getById($ghsId, true);
  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::getById($customId, true);
  43. if (empty($custom)) {
  44. util::fail('供货商信息不完整那');
  45. }
  46. // 花店自助充值建单前:挂账并入净 balance
  47. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  48. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  49. $capitalType = dict::getDict('capitalType', 'customRechargeToGhs', 'id');
  50. $customOrderSn = orderSn::getGhsCustomRechargeSn();
  51. $customShopId = $custom->shopId ?? 0;
  52. $customMainId = $custom->mainId ?? 0;
  53. $customBalance = $custom->balance ?? 0;
  54. $customName = $custom->name ?? '';
  55. $ghsShopId = $ghs->shopId ?? 0;
  56. $ghsMainId = $ghs->mainId ?? 0;
  57. $ghsBalance = $ghs->balance ?? 0;
  58. $ghsName = $ghs->name ?? '';
  59. $ghsShop = ShopClass::getById($ghsShopId, true);
  60. if (empty($ghsShop)) {
  61. util::fail('供货商信息不完全哦');
  62. }
  63. $customStaffId = 0;
  64. $customStaffName = '';
  65. if (isset($this->shopAdmin) && !empty($this->shopAdmin)) {
  66. $staff = $this->shopAdmin;
  67. $customStaffId = $staff->id ?? 0;
  68. $customStaffName = $staff->name ?? '';
  69. }
  70. $cData = [
  71. 'orderSn' => $customOrderSn,
  72. 'onlinePay' => 2,
  73. 'payWay' => $payWay,
  74. 'shopId' => $ghsShopId,
  75. 'mainId' => $ghsMainId,
  76. 'amount' => $actPrice,
  77. 'balance' => $customBalance,
  78. 'side' => 0,
  79. 'staffId' => 0,
  80. 'staffName' => '',
  81. 'payStatus' => 0,
  82. 'status' => 0,
  83. 'remark' => '',
  84. 'ghsId' => $ghsId,
  85. 'ghsShopId' => $ghsShopId,
  86. 'ghsName' => $ghsName,
  87. 'customId' => $customId,
  88. 'customShopId' => $customShopId,
  89. 'customName' => $customName,
  90. 'remark' => '',
  91. ];
  92. $cRecharge = CustomRechargeClass::add($cData, true);
  93. $cRechargeId = $cRecharge->id ?? 0;
  94. $ghsOrderSn = orderSn::getGhsRechargeSn();
  95. $gData = [
  96. 'orderSn' => $ghsOrderSn,
  97. 'onlinePay' => 2,
  98. 'payWay' => $payWay,
  99. 'shopId' => $customShopId,
  100. 'mainId' => $customMainId,
  101. 'amount' => $actPrice,
  102. 'balance' => $ghsBalance,
  103. 'side' => 0,
  104. 'staffId' => $customStaffId,
  105. 'staffName' => $customStaffName,
  106. 'payStatus' => 0,
  107. 'status' => 0,
  108. 'remark' => '',
  109. 'ghsId' => $ghsId,
  110. 'ghsShopId' => $ghsShopId,
  111. 'ghsName' => $ghsName,
  112. 'customId' => $customId,
  113. 'customShopId' => $customShopId,
  114. 'customName' => $customName,
  115. 'remark' => '',
  116. ];
  117. $gRecharge = GhsRechargeClass::add($gData, true);
  118. $gRechargeId = $gRecharge->id ?? 0;
  119. $cRecharge->ghsRechargeId = $gRechargeId;
  120. $cRecharge->save();
  121. $gRecharge->customRechargeId = $cRechargeId;
  122. $gRecharge->save();
  123. $subject = '充值销账 单号' . $customOrderSn;
  124. if ($payWay == 0) {
  125. $openId = '';
  126. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  127. $openId = $post['currentMiniOpenId'];
  128. }
  129. if (empty($openId)) {
  130. if (isset($this->admin) && !empty($this->admin)) {
  131. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  132. $openId = $this->admin->miniOpenId;
  133. }
  134. }
  135. }
  136. if (empty($openId)) {
  137. $currentShop = ShopClass::getById($customShopId, true);
  138. $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
  139. $currentMobile = $currentShop->mobile ?? '';
  140. noticeUtil::push("花店向供货商充值时,发现没有openId。{$currentShopName} {$currentMobile}", '15280215347');
  141. util::fail('用户信息不完整');
  142. }
  143. $merchantExtend = WxOpenClass::getWxInfo();
  144. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  145. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  146. $params = [
  147. 'appid' => 'OP00002119',
  148. 'serial_no' => '018b08cfddbd',
  149. 'merchant_no' => $ghsShop->lklSjNo,
  150. 'term_no' => $ghsShop->lklScanTermNo,
  151. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  152. 'lklCertificatePath' => $lklCertificatePath,
  153. ];
  154. $laResource = new Lakala($params);
  155. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  156. $wxParams = [
  157. 'orderSn' => $customOrderSn,
  158. 'amount' => $totalFee,
  159. 'capitalType' => $capitalType,
  160. 'notifyUrl' => $notifyUrl,
  161. 'wxAppId' => $merchantExtend['miniAppId'],
  162. 'openId' => $openId,
  163. 'subject' => $subject,
  164. 'couponId' => 0,
  165. ];
  166. $response = $laResource->driveWxPay($wxParams);
  167. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  168. $errMsg = $response['msg'] ?? '';
  169. $errCode = $response['code'] ?? '';
  170. noticeUtil::push('编号19881507:' . $errCode . ' | ' . $errMsg . ' | ' . $ghsName . ' | ' . $customName . ' | ' . $actPrice, '15280215347');
  171. if($errCode == 'BBS11109'){
  172. util::fail('超过单笔限额,请分二笔支付');
  173. }
  174. util::fail('支付失败');
  175. }
  176. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  177. $appId = $newParams['app_id'] ?? '';
  178. $nonceStr = $newParams['nonce_str'] ?? '';
  179. $paySign = $newParams['pay_sign'] ?? '';
  180. $package = $newParams['package'] ?? '';
  181. $signType = $newParams['sign_type'] ?? '';
  182. $timeStamp = $newParams['time_stamp'] ?? '';
  183. $new = [
  184. 'id' => $gRechargeId,
  185. 'appId' => $appId,
  186. 'nonceStr' => $nonceStr,
  187. 'paySign' => $paySign,
  188. 'package' => $package,
  189. 'signType' => $signType,
  190. 'timeStamp' => $timeStamp,
  191. 'orderSn' => $customOrderSn,
  192. ];
  193. util::success($new);
  194. } elseif ($payWay == 1) {
  195. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  196. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  197. $params = [
  198. 'appid' => 'OP00002119',
  199. 'serial_no' => '018b08cfddbd',
  200. 'merchant_no' => $ghsShop->lklSjNo,
  201. 'term_no' => $ghsShop->lklB2BTermNo,
  202. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  203. 'lklCertificatePath' => $lklCertificatePath,
  204. ];
  205. $laResource = new Lakala($params);
  206. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
  207. $cashParams = [
  208. 'orderSn' => $customOrderSn,
  209. 'amount' => $totalFee,
  210. 'capitalType' => $capitalType,
  211. 'notifyUrl' => $notifyUrl,
  212. 'subject' => $subject,
  213. ];
  214. $response = $laResource->cashierPay($cashParams);
  215. if (isset($response['code']) == false || $response['code'] != '000000') {
  216. util::fail('支付失败了');
  217. }
  218. $url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  219. if (empty($url)) {
  220. util::fail('支付失败');
  221. }
  222. util::success(['url' => $url]);
  223. } else {
  224. util::fail('暂不支持的付款方式');
  225. }
  226. }
  227. }