CustomRechargeController.php 9.2 KB

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