MPay.Class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * 和订单支付相关
  4. * Created by PhpStorm.
  5. * User: phperstar
  6. * Date: 2021/4/12
  7. * Time: 11:35 PM
  8. */
  9. namespace JinDouYun\Model\Order;
  10. use Mall\Framework\Core\ResultWrapper;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\StatusCode;
  13. use JinDouYun\Cache\SystemCache;
  14. use JinDouYun\Cache\EnterpriseCache;
  15. use JinDouYun\Model\System\MPaymentSetting;
  16. use JinDouYun\Model\Customer\MMemberBalanceDetail;
  17. use JinDouYun\Model\Finance\MReceived;
  18. use JinDouYun\Model\Finance\MAccount;
  19. use JinDouYun\Model\Finance\MAccountDetail;
  20. use JinDouYun\Dao\Finance\DRefundAccount;
  21. use Util\WeiXin\Pay;
  22. class MPay
  23. {
  24. private $enterpriseId;
  25. private $userCenterId;
  26. private $objMAccount;
  27. private $objDRefundAccount;
  28. public function __construct($enterpriseId, $userCenterId)
  29. {
  30. $this->enterpriseId = $enterpriseId;
  31. $this->userCenterId = $userCenterId;
  32. $this->objMAccount = new MAccount($enterpriseId, $userCenterId);
  33. $this->objMAccountDetail = new MAccountDetail($enterpriseId, $userCenterId);
  34. $this->objDRefundAccount = new DRefundAccount('finance');
  35. }
  36. /**
  37. * 退款
  38. * @param $orderData array 订单数据
  39. * @param $deleteReceived boolean 是否删除收款单
  40. * @param $refundData array 退款数据
  41. * @param $refundWay int 退款方式 默认原路返回
  42. * @return ResultWrapper
  43. */
  44. public function refund($orderData, $deleteReceived = false, $refundData = [], $refundWay = 5)
  45. {
  46. if(!isset($orderData['payType']) || empty($orderData['payType'])){
  47. return ResultWrapper::fail('支付方式为空', ErrorCode::$paramError);
  48. }
  49. // 如果是退款单取退款单数据 / 取消订单退款取订单数据
  50. $refund = [
  51. 'id' => (!empty($refundData)) ? $refundData['id'] :$orderData['id'],
  52. 'no' => (!empty($refundData)) ? $refundData['no'] :$orderData['no'],
  53. 'orderNo' => (!empty($refundData)) ? $refundData['sourceNo'] :$orderData['no'],
  54. 'orderId' => (!empty($refundData)) ? $refundData['originId'] : $orderData['id'],
  55. 'money' => (!empty($refundData)) ? $refundData['money'] : $orderData['payAmount'],
  56. 'financeType' => (!empty($refundData)) ? '退款单退款' : '取消订单退款',
  57. 'shopId' => (!empty($refundData)) ? $refundData['shopId'] : $orderData['shopId'],
  58. 'shopName' => (!empty($refundData)) ? $refundData['shopName'] : $orderData['shopName'],
  59. 'contactUnit' => (!empty($refundData)) ? $refundData['unitName'] : $orderData['customerName'], // 往来单位名称
  60. 'unitId' => (!empty($refundData)) ? $refundData['unitId'] : $orderData['customerId'], // 往来单位名称
  61. ];
  62. $payType = explode(',', $orderData['payType']);
  63. // 后台代客下单 ,订单支付成功后操作
  64. if( in_array(StatusCode::$payType['cashPay'], $payType) && $orderData['payStatus'] == StatusCode::$standard ){
  65. //查询当前收款单的account数据
  66. $this->objDRefundAccount->setTable('qianniao_refund_account_' . $this->enterpriseId . '_' . date('Y', $refundData['createTime']) . '_' . ceil(date('m', $refundData['createTime']) / 3));
  67. $refundResult = $this->objDRefundAccount->select(['refundId'=>$refundData['id']]);
  68. foreach ($refundResult as $value){
  69. $refund['accountId'] = $value['accountId'];
  70. $result = self::updateAccountMoneyAndDetail(StatusCode::$payType['cashPay'], $refund, StatusCode::$delete);
  71. if(!$result->isSuccess()){
  72. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  73. }
  74. unset($refund['accountId']);
  75. }
  76. }
  77. // 组合支付/会员余额/银行打款 支付退回余额
  78. if( in_array(StatusCode::$payType['balance'], $payType) || in_array(StatusCode::$payType['bankLoans'], $payType) || $refundWay == StatusCode::$delete){
  79. //减少余额 新增会员流水记录
  80. $memberBalanceDetailData = [
  81. 'customerId' => $orderData['customerId'],
  82. 'type' => StatusCode::$standard,
  83. 'userCenterId' => $orderData['userCenterId'],
  84. 'purpose' => (!empty($refundData)) ? '退货退款' : '取消订单退款',
  85. 'orderIds' => $refund['orderId'],
  86. 'remark' => '订单no'.$refund['orderNo'],
  87. 'money' => $refund['money'],
  88. 'financeType' => '会员余额退款'
  89. ];
  90. $objMMemberBalanceDetail = new MMemberBalanceDetail($this->enterpriseId,$this->userCenterId);
  91. $memberBalanceDbResult = $objMMemberBalanceDetail->addMemberBalanceDetail($memberBalanceDetailData);
  92. if (!$memberBalanceDbResult->isSuccess()) {
  93. return ResultWrapper::fail($memberBalanceDbResult->getData(), $memberBalanceDbResult->getErrorCode());
  94. }
  95. // 银行打款支付退款同步减银行打款账户
  96. if( in_array(StatusCode::$payType['bankLoans'], $payType) ){
  97. $extends = json_decode($orderData['extends'], true);
  98. if( isset($extends['bankData']['id']) && !empty($extends['bankData']['id'])){
  99. $refund['accountId'] = $extends['bankData']['id'];
  100. $result = self::updateAccountMoneyAndDetail(StatusCode::$payType['bankLoans'], $refund, StatusCode::$delete);
  101. if(!$result->isSuccess()){
  102. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  103. }
  104. unset($refund['accountId']);
  105. }
  106. }
  107. return ResultWrapper::success('退款成功');
  108. }
  109. // 微信支付退回微信
  110. if( $orderData['payType'] == strval(StatusCode::$payType['wxPay']) && $refundWay == StatusCode::$standard){
  111. // 获取当前登录企业的小程序配置
  112. $objSystemCache = new SystemCache();
  113. $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->enterpriseId);
  114. if (empty($miniprogramSetting)) {
  115. return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
  116. }
  117. $miniprogramSetting = json_decode($miniprogramSetting, true);//获取小程序appid
  118. $appid = $miniprogramSetting['appid'];
  119. // 获取微信支付配置项
  120. $objMPaymentSetting = new MPaymentSetting($this->userCenterId, $this->enterpriseId);
  121. $result = $objMPaymentSetting->getPayData(StatusCode::$payType['wxPay']);
  122. if (!$result->isSuccess() || empty($result->getData())) {
  123. return ResultWrapper::fail('获取微信配置错误', ErrorCode::$configEroor);
  124. }
  125. $payment = $result->getData();
  126. $weixinConfigData = [
  127. 'mch_id' => $payment['mchId'],
  128. 'apiPartnerKey' => $payment['apiPartnerKey'],
  129. 'appid' => $appid
  130. ];
  131. $sslData = [
  132. 'cert' => UPLOAD_FILE_PATH .'/'.$payment['apiclient_cert'],
  133. 'key' => UPLOAD_FILE_PATH .'/'. $payment['apiclient_key'],
  134. ];
  135. // 微信退款操作
  136. $objPay =new Pay($appid, $weixinConfigData['mch_id'],$weixinConfigData['apiPartnerKey']);
  137. $result = $objPay->transfers($orderData['outerTradeNo'], $refund['no'],$refund['money'],$orderData['payAmount'],$sslData);
  138. if(!$result->isSuccess()){
  139. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  140. }
  141. // 微信退款完成操作微信账户
  142. $result = self::updateAccountMoneyAndDetail(StatusCode::$payType['wxPay'], $refund, StatusCode::$delete);
  143. if(!$result->isSuccess()){
  144. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  145. }
  146. return ResultWrapper::success($result->getData());
  147. }
  148. // 对收款单做退款流程平账
  149. if($deleteReceived){
  150. }
  151. return ResultWrapper::success('退款操作成功');
  152. }
  153. /**
  154. * 退款操作银行账户余额和流水
  155. * @param int $payTpye 支付方式
  156. * @param aray $refund 退款数据
  157. * @param int $type 收入支出标识 5收入 4支出
  158. *
  159. * @return ResultWrapper
  160. */
  161. public function updateAccountMoneyAndDetail($payTpye, $refund, $type)
  162. {
  163. if(isset($refund['accountId'])){
  164. //获取银行打款账户信息
  165. $accountResult = $this->objMAccount->getAccountInfo($refund['accountId']);
  166. }else{
  167. //获取微信账户信息
  168. $accountResult = $this->objMAccount->getDefaultAccount($payTpye);
  169. }
  170. if (!$accountResult->isSuccess()) {
  171. return ResultWrapper::fail($accountResult->getData(), $accountResult->getErrorCode());
  172. }
  173. $accountInfo = $accountResult->getData();
  174. //账户明细
  175. $accountDetail = [
  176. 'accountId' => $accountInfo['id'],
  177. 'accountCode' => $accountInfo['accountCode'],
  178. 'accountName' => $accountInfo['name'],
  179. 'accountNumber' => $accountInfo['accountNumber'],
  180. 'sourceId' => $refund['id'],
  181. 'sourceNo' => $refund['no'],
  182. 'financeType' => $refund['financeType'],
  183. 'beginBalance' => $accountInfo['money'],
  184. 'shopId' => $refund['shopId'],
  185. 'shopName' => $refund['shopName'],
  186. 'contactUnit' => '客户名称:' . $refund['contactUnit'], // 往来单位名称
  187. 'supplierId' => 0,
  188. 'customerId' => $refund['unitId'],
  189. 'operatorId' => $this->userCenterId,
  190. 'receiveOrPayPerson' => '平台',
  191. 'remark' => $refund['financeType'].$refund['money'].'元',
  192. 'createTime' => time(),
  193. 'updateTime' => time(),
  194. ];
  195. // 根据收入支出类型组装对应的数据
  196. if($type == StatusCode::$standard){
  197. $accountDetail['income'] = $refund['money'];
  198. $accountDetail['expend'] = 0;
  199. $accountDetail['endBalance'] = bcadd($accountInfo['money'], $refund['money'], 2);
  200. $accountChangeMoney = $refund['money'];
  201. }else{
  202. $accountDetail['income'] = 0;
  203. $accountDetail['expend'] = $refund['money'];
  204. $accountDetail['endBalance'] = bcsub($accountInfo['money'], $refund['money'], 2);
  205. $accountChangeMoney = -1 * $refund['money'];
  206. }
  207. $result = $this->objMAccountDetail->addAccountDetail($accountDetail);
  208. if (!$result->isSuccess()) {
  209. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  210. }
  211. $result = $this->objMAccount->updateMoney($accountInfo['id'], $accountChangeMoney);
  212. if (!$result->isSuccess()) {
  213. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  214. }
  215. return ResultWrapper::success($result->getData());
  216. }
  217. }