CustomRechargeController.php 9.0 KB

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