CustomRechargeController.php 9.3 KB

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