ProfitSharingService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yunzhong
  5. * Date: 2019/10/21
  6. * Time: 11:04
  7. */
  8. namespace app\common\services\wechat;
  9. use app\common\modules\wechat\models\WechatProfitSharingLog;
  10. use app\common\services\wechat\lib\WxPayApi;
  11. use app\common\services\wechat\lib\WxPayConfig;
  12. use app\common\services\wechat\lib\WxPayException;
  13. use app\common\services\wechat\lib\WxPayProfitSharing;
  14. class ProfitSharingService
  15. {
  16. /**
  17. * @param $data
  18. * @return mixed
  19. * @throws lib\WxPayException
  20. */
  21. public static function addProfitSharing()
  22. {
  23. // $receiver = json_decode([
  24. // 'type' => $data['type'],
  25. // 'account' => $data['openid'],
  26. // 'relation_type' => $data['relation_type'],
  27. // ]);
  28. $config = new WxPayConfig();
  29. $data = [
  30. 'type' => 'MERCHANT_ID',
  31. 'name' => $config->GetMchName(),
  32. 'account' => $config->GetMerchantId(),
  33. 'relation_type' => 'SERVICE_PROVIDER',
  34. ];
  35. $receiver = json_decode($data,256);
  36. $inputObj = new WxPayProfitSharing($config);
  37. $inputObj->SetReceiver($receiver);
  38. $result = WxPayApi::profitsharingaddreceiver($config, $inputObj);
  39. if ($result['return_code'] != 'SUCCESS') {
  40. throw new WxPayException($result['return_msg']);
  41. }
  42. return $result;
  43. }
  44. /**
  45. * @param $data
  46. * @return mixed
  47. * @throws lib\WxPayException
  48. */
  49. public static function deleteProfitSharing($data)
  50. {
  51. $receiver = json_decode([
  52. 'type' => $data['type'],
  53. 'account' => $data['openid'],
  54. ]);
  55. $config = new WxPayConfig();
  56. $inputObj = new WxPayProfitSharing($config);
  57. $inputObj->SetReceiver($receiver);
  58. $result = WxPayApi::profitsharingremovereceiver($config, $inputObj);
  59. if ($result['return_code'] != 'SUCCESS') {
  60. throw new WxPayException($result['return_msg']);
  61. }
  62. return $result;
  63. }
  64. /**
  65. * @param $amount
  66. * @param $transaction_id
  67. * @param $order_id
  68. * @return mixed
  69. * @throws WxPayException
  70. */
  71. public static function profitSharing($amount, $transaction_id, $order_id)
  72. {
  73. $config = new WxPayConfig();
  74. $out_order_no = createNo('PSO', true);
  75. //记录分账信息
  76. $data = [
  77. 'uniacid' => \YunShop::app()->uniacid,
  78. 'order_id' => $order_id,
  79. 'mch_id' => $config->GetMerchantId(),
  80. 'sub_mch_id' => $config->GetSubMerchantId(),
  81. 'appid' => $config->GetAppId(),
  82. 'sub_appid' => $config->GetSubAppId(),
  83. 'transaction_id' => $transaction_id,
  84. 'out_order_no' => $out_order_no,
  85. 'status' => 0,
  86. ];
  87. $receiver = [
  88. [
  89. 'type' => 'MERCHANT_ID',
  90. 'account' => $config->GetMerchantId(),
  91. 'amount' => (int)strval($amount * 100),
  92. 'description' => '门店分账给服务商',
  93. ]
  94. ];
  95. $inputObj = new WxPayProfitSharing($config);
  96. $inputObj->SetTransaction_id($transaction_id);
  97. $inputObj->SetOut_order_no($out_order_no);
  98. $inputObj->SetReceivers(json_encode($receiver,256));
  99. $result = WxPayApi::profitsharing($config, $inputObj);
  100. if ($result['return_code'] != 'SUCCESS') {
  101. $data['status'] = -1;
  102. $data['message'] = $result['return_msg'] ?: $result['return_code'];
  103. } elseif ($result['result_code'] != 'SUCCESS') {
  104. $data['status'] = -1;
  105. $data['message'] = $result['err_code_des'] ?: $result['err_code'];
  106. } else {
  107. $data['status'] = 1;
  108. $data['message'] = 'SUCCESS';
  109. }
  110. $create_data = array_merge($data,$receiver[0]);
  111. $create_data['type'] = 0;
  112. WechatProfitSharingLog::create($create_data);
  113. return $result;
  114. }
  115. /**
  116. * @param $transaction_id
  117. * @return mixed
  118. * @throws WxPayException
  119. */
  120. public static function profitSharingFinish($transaction_id)
  121. {
  122. $config = new WxPayConfig();
  123. $out_order_no = createNo('PSF', true);
  124. //记录分账信息
  125. $data = [
  126. 'uniacid' => \YunShop::app()->uniacid,
  127. 'mch_id' => $config->GetMerchantId(),
  128. 'sub_mch_id' => $config->GetSubMerchantId(),
  129. 'appid' => $config->GetAppId(),
  130. 'sub_appid' => $config->GetSubAppId(),
  131. 'transaction_id' => $transaction_id,
  132. 'out_order_no' => $out_order_no,
  133. 'status' => 0,
  134. 'account' => $config->GetMerchantId(),
  135. 'description' => '完结分账',
  136. ];
  137. $inputObj = new WxPayProfitSharing($config);
  138. $inputObj->SetTransaction_id($transaction_id);
  139. $inputObj->SetOut_order_no($out_order_no);
  140. $inputObj->SetDescription($data['description']);
  141. $result = WxPayApi::profitsharingfinish($config, $inputObj);
  142. if ($result['return_code'] != 'SUCCESS') {
  143. $data['status'] = -1;
  144. $data['message'] = $result['return_msg'] ?: $result['return_code'];
  145. } elseif ($result['result_code'] != 'SUCCESS') {
  146. $data['status'] = -1;
  147. $data['message'] = $result['err_code_des'] ?: $result['err_code'];
  148. } else {
  149. $data['status'] = 1;
  150. $data['message'] = 'SUCCESS';
  151. }
  152. $data['type'] = 1;
  153. WechatProfitSharingLog::updateOrCreate(['transaction_id' => $data['transaction_id']], $data);
  154. return $result;
  155. }
  156. /**
  157. * @param $out_order_no
  158. * @param $transaction_id
  159. * @throws WxPayException
  160. */
  161. public static function profitSharingQuery($out_order_no, $transaction_id)
  162. {
  163. $config = new WxPayConfig();
  164. $inputObj = new WxPayProfitSharing($config);
  165. $inputObj->SetTransaction_id($transaction_id);
  166. $inputObj->SetOut_order_no($out_order_no);
  167. $result = WxPayApi::profitsharingquery($config, $inputObj);
  168. if ($result['return_code'] =! 'SUCCESS') {
  169. $data['status'] = -1;
  170. $data['message'] = $result['return_msg'] ?: $result['return_code'];
  171. } elseif ($result['result_code'] != 'SUCCESS') {
  172. $data['status'] = -1;
  173. $data['message'] = $result['err_code_des'] ?: $result['err_code'];
  174. } else {
  175. $data['status'] = 1;
  176. $data['message'] = $result['result_code'];
  177. }
  178. }
  179. }