| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\utils\Wechat;
- use app\models\Order;
- use app\models\WechatConfig;
- use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
- use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
- use Think\Exception;
- /**
- * 微信分账相关方法
- * Class WechatShare
- * @package app\utils\Wechat
- */
- class WechatShare extends WechatProfit
- {
- /**
- * 与分账方关系类型枚举:服务商
- */
- const RELATION_SERVICE_PROVIDER = 'SERVICE_PROVIDER';
- /**
- * 与分账方关系类型枚举:门店
- */
- const RELATION_STORE = 'STORE';
- /**
- * 与分账方关系类型枚举:员工
- */
- const RELATION_STAFF = 'STAFF';
- /**
- * 与分账方关系类型枚举:店主
- */
- const RELATION_STORE_OWNER = 'STORE_OWNER';
- /**
- * 与分账方关系类型枚举:合作伙伴
- */
- const RELATION_PARTNER = 'PARTNER';
- /**
- * 与分账方关系类型枚举:总部
- */
- const RELATION_HEADQUARTER = 'HEADQUARTER';
- /**
- * 与分账方关系类型枚举:品牌方
- */
- const RELATION_BRAND = 'BRAND';
- /**
- * 与分账方关系类型枚举:分销商
- */
- const RELATION_DISTRIBUTOR = 'DISTRIBUTOR';
- /**
- * 与分账方关系类型枚举:用户
- */
- const RELATION_USER = 'USER';
- /**
- * 与分账方关系类型枚举:供应商
- */
- const RELATION_SUPPLIER = 'SUPPLIER';
- public static $validRelation = [
- self::RELATION_BRAND,
- self::RELATION_SERVICE_PROVIDER,
- self::RELATION_STORE,
- self::RELATION_STAFF,
- self::RELATION_STORE_OWNER,
- self::RELATION_PARTNER,
- self::RELATION_HEADQUARTER,
- self::RELATION_DISTRIBUTOR,
- self::RELATION_USER,
- self::RELATION_SUPPLIER
- ];
- /**
- * 分账接收方类型枚举:商户号 mch_id或者sub_mch_id
- */
- const RECEIVE_MERCHANT_ID = 'MERCHANT_ID';
- /**
- * 分账接收方类型枚举:个人openid
- */
- const RECEIVE_PERSONAL_OPENID = 'PERSONAL_OPENID';
- /**
- * 分账接收方类型枚举:个人sub_openid
- */
- const RECEIVE_PERSONAL_SUB_OPENID = 'PERSONAL_SUB_OPENID';
- public static $validReceive = [
- self::RECEIVE_MERCHANT_ID,
- self::RECEIVE_PERSONAL_OPENID,
- self::RECEIVE_PERSONAL_SUB_OPENID
- ];
- /**
- * 添加分账接收方
- * @param string $account
- * @param string $name
- * @param string $relation_type
- * @param string $type
- * @param boolean $is_app
- * @return array
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public static function addReceivers($account, $relation_type, $type, $name = null, $is_app = false, $store_id = null, $is_platform = 0, $order = null)
- {
- if (!in_array($type, self::$validReceive)) {
- return [
- 'code' => 1,
- 'msg' => '非法的分账接收方类型'
- ];
- }
- if (!in_array($relation_type, self::$validRelation)) {
- return [
- 'code' => 1,
- 'msg' => '非法的与分账方关系类型'
- ];
- }
- if (empty($account)) {
- return [
- 'code' => 1,
- 'msg' => '分账接收方信息不能为空'
- ];
- }
- try {
- if ($order->order_origin == Order::ORDER_SOURCE_APP) {
- parent::init($store_id, $is_platform, 0, WechatConfig::TYPE_CONFIG_APP);
- } else if ($order->order_origin == Order::ORDER_SOURCE_MP) {
- parent::init($store_id, $is_platform, 0, WechatConfig::TYPE_CONFIG_MP);
- } else if ($order->order_origin == Order::ORDER_SOURCE_WEB) {
- parent::init($store_id, $is_platform, 0, WechatConfig::TYPE_CONFIG_H5);
- } else {
- parent::init($store_id, $is_platform);
- }
- $receiver = [
- "type" => $type,
- "account" => $account,
- "relation_type" => $relation_type
- ];
- if ($name) {
- $receiver['name'] = $name;
- }
- $res = self::$wechat_pay->profit_sharing->addReceiver($receiver);
- if (!$res) {
- return [
- 'code' => 1,
- 'msg' => '调用失败',
- ];
- }
- if ($res['return_code'] != 'SUCCESS') {
- return [
- 'code' => 1,
- 'msg' => '添加分账接收方失败:,' . (isset($res['return_msg']) ? $res['return_msg'] : ''),
- 'res' => $res,
- ];
- }
- if ($res['result_code'] != 'SUCCESS') {
- return [
- 'code' => 1,
- 'msg' => '添加分账接收方结果失败:,' . (isset($res['err_code_des']) ? $res['err_code_des'] : ''),
- 'res' => $res,
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '添加成功',
- 'res' => $res
- ];
- } catch (InvalidArgumentException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidConfigException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 删除分账接收方
- * @param string $account
- * @param string $name
- * @param string $relation_type
- * @param string $type
- * @param boolean $is_app
- * @return array
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public static function deleteReceivers($account, $name, $relation_type, $type, $is_app = false)
- {
- if (!in_array($type, self::$validReceive)) {
- return [
- 'code' => 1,
- 'msg' => '非法的分账接收方类型'
- ];
- }
- if (!in_array($relation_type, self::$validRelation)) {
- return [
- 'code' => 1,
- 'msg' => '非法的与分账方关系类型'
- ];
- }
- if (empty($account) || empty($name)) {
- return [
- 'code' => 1,
- 'msg' => '分账接收方信息不能为空'
- ];
- }
- try {
- parent::init();
- $receiver = [
- "type" => $type,
- "account" => $account,
- "name" => $name,
- "relation_type" => $relation_type
- ];
- $res = self::$wechat_pay->profit_sharing->deleteReceiver($receiver);
- if (!$res) {
- return [
- 'code' => 1,
- 'msg' => '调用失败',
- ];
- }
- if ($res['return_code'] != 'SUCCESS') {
- return [
- 'code' => 1,
- 'msg' => '删除分账接收方失败:,' . (isset($res['return_msg']) ? $res['return_msg'] : ''),
- 'res' => $res,
- ];
- }
- if ($res['result_code'] != 'SUCCESS') {
- return [
- 'code' => 1,
- 'msg' => '删除分账接收方结果失败:,' . (isset($res['err_code_des']) ? $res['err_code_des'] : ''),
- 'res' => $res,
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '删除成功',
- 'res' => $res
- ];
- } catch (InvalidArgumentException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidConfigException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 请求分账(单次或多次)
- * @param string $transaction_id
- * @param string $out_trade_no
- * @param array $receivers
- * @param boolean $single
- * @param boolean $is_app
- * @return array
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public static function share($transaction_id, $out_trade_no, $receivers, $single = true, $is_app = false, $store_id = null, $is_platform = 0, $order = null)
- {
- if (!$transaction_id) {
- return [
- 'code' => 1,
- 'msg' => '微信支付订单号不能为空'
- ];
- }
- if (!$out_trade_no) {
- return [
- 'code' => 1,
- 'msg' => '商户订单号不能为空'
- ];
- }
- if (empty($receivers) || !is_array($receivers)) {
- return [
- 'code' => 1,
- 'msg' => '分账接收人信息不能为空'
- ];
- }
- $send_receivers = [];
- foreach ($receivers as $key => $receiver) {
- if (!in_array($receiver['type'], [1, 2, 3])) {
- return [
- 'code' => 1,
- 'msg' => '非法的分账接收方类型'
- ];
- }
- if (empty($receiver['account'])) {
- return [
- 'code' => 1,
- 'msg' => '分账接收方信息不能为空'
- ];
- }
- if ($receiver['amount'] <= 0) {
- return [
- 'code' => 1,
- 'msg' => '分账金额非法'
- ];
- }
- switch ($receiver['type']) {
- case 1:
- $receivers[$key]['type'] = self::RECEIVE_PERSONAL_OPENID;
- $receivers[$key]['description'] = '分账到个人';
- break;
- case 2:
- $receivers[$key]['type'] = self::RECEIVE_MERCHANT_ID;
- $receivers[$key]['description'] = '分账到商户';
- break;
- case 3:
- $receivers[$key]['type'] = self::RECEIVE_PERSONAL_SUB_OPENID;
- $receivers[$key]['description'] = '分账到个人';
- break;
- default:;
- }
- $receivers[$key]['amount'] = intval($receivers[$key]['amount'] * 100);
- $send_receivers[] = [
- 'type' => $receivers[$key]['type'],
- 'account' => $receivers[$key]['account'],
- 'amount' => $receivers[$key]['amount'],
- 'description' => $receivers[$key]['description'],
- // 'name' => $receivers[$key]['name'],
- ];
- }
- try {
- if ($order->order_origin == Order::ORDER_SOURCE_APP) {
- parent::init($store_id, $is_platform, 0, WechatConfig::TYPE_CONFIG_APP);
- } else if ($order->order_origin == Order::ORDER_SOURCE_MP) {
- parent::init($store_id, $is_platform, 0, WechatConfig::TYPE_CONFIG_MP);
- } else if ($order->order_origin == Order::ORDER_SOURCE_WEB) {
- parent::init($store_id, $is_platform, 0, WechatConfig::TYPE_CONFIG_H5);
- } else {
- parent::init($store_id, $is_platform);
- }
- if ($single) {
- $res = self::$wechat_pay->profit_sharing->share($transaction_id, $out_trade_no, $receivers);
- } else {
- \Yii::error([$transaction_id, $out_trade_no, $receivers]);
- $res = self::$wechat_pay->profit_sharing->multiShare($transaction_id, $out_trade_no, $send_receivers);
- }
- if (!$res) {
- return [
- 'code' => 1,
- 'msg' => '调用失败',
- ];
- }
- if ($res['return_code'] != 'SUCCESS') {
- return [
- 'code' => 1,
- 'msg' => isset($res['return_msg']) ? $res['return_msg'] : '',
- 'res' => $res,
- ];
- }
- if ($res['result_code'] != 'SUCCESS') {
- return [
- 'code' => 1,
- 'msg' => isset($res['err_code_des']) ? $res['err_code_des'] : '',
- 'res' => $res,
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '成功',
- 'res' => $res
- ];
- } catch (InvalidArgumentException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidConfigException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 分账查询
- * @param string $transaction_id
- * @param string $out_trade_no
- * @param boolean $is_app
- * @return array
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public static function query($transaction_id, $out_trade_no, $is_app = false)
- {
- if (!$transaction_id) {
- return [
- 'code' => 1,
- 'msg' => '微信支付订单号不能为空'
- ];
- }
- if (!$out_trade_no) {
- return [
- 'code' => 1,
- 'msg' => '商户订单号不能为空'
- ];
- }
- try {
- parent::init();
- $res = self::$wechat_pay->profit_sharing->query($transaction_id, $out_trade_no);
- if (!$res) {
- return [
- 'code' => 1,
- 'msg' => '调用失败',
- ];
- }
- if ($res['return_code'] != 'SUCCESS') {
- return [
- 'code' => 1,
- 'msg' => '查询分账调用失败:,' . (isset($res['return_msg']) ? $res['return_msg'] : ''),
- 'res' => $res,
- ];
- }
- if ($res['result_code'] != 'SUCCESS') {
- return [
- 'code' => 1,
- 'msg' => '查询分账结果失败:,' . (isset($res['err_code_des']) ? $res['err_code_des'] : ''),
- 'res' => $res,
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '成功',
- 'res' => $res
- ];
- } catch (InvalidArgumentException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidConfigException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 分账退回
- * @param string $out_trade_no
- * @param string $out_return_no
- * @param string $return_amount
- * @param string $return_account
- * @param string $description
- * @param boolean $is_app
- * @return array
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public static function returnShare($out_trade_no, $out_return_no, $return_amount, $return_account, $is_app = false, $description = '订单取消')
- {
- if (empty($out_trade_no)) {
- return [
- 'code' => 1,
- 'msg' => '退款订单号不能为空'
- ];
- }
- if (empty($out_return_no)) {
- return [
- 'code' => 1,
- 'msg' => '商户回退单号不能为空'
- ];
- }
- if (intval($return_amount) <= 0) {
- return [
- 'code' => 1,
- 'msg' => '退款金额非法'
- ];
- }
- if (empty($return_account)) {
- return [
- 'code' => 1,
- 'msg' => '回退方账户账号不能为空'
- ];
- }
- try {
- parent::init();
- $res = self::$wechat_pay->profit_sharing->returnShare($out_trade_no, $out_return_no, $return_amount, $return_account, $description);
- if (!$res) {
- return [
- 'code' => 1,
- 'msg' => '调用失败',
- ];
- }
- if ($res['return_code'] != 'SUCCESS') {
- return [
- 'code' => 1,
- 'msg' => '分账退回调用失败:,' . (isset($res['return_msg']) ? $res['return_msg'] : ''),
- 'res' => $res,
- ];
- }
- // 如果返回为处理中,请勿变更商户回退单号,使用相同的参数再次发起分账回退
- if ($res['result'] == 'PROCESSING') {
- return self::returnShare($out_trade_no, $out_return_no, $return_amount, $return_account, $description);
- }
- if ($res['result'] == 'FAILED') {
- if ($res['fail_reason'] == 'ACCOUNT_ABNORMAL') {
- $msg = '分账接收方账户异常';
- }
- if ($res['fail_reason'] == 'TIME_OUT_CLOSED') {
- $msg = '超时关单';
- }
- return [
- 'code' => 1,
- 'msg' => $msg
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '成功',
- 'res' => $res
- ];
- } catch (InvalidArgumentException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidConfigException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 分账完结
- * @param string $transaction_id
- * @param string $out_trade_no
- * @param string $description
- * @param boolean $is_app
- * @return array
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public static function complete($transaction_id, $out_trade_no, $is_app = false, $description = '分账完结', $store_id = null, $is_platform = 0)
- {
- if (!$transaction_id) {
- return [
- 'code' => 1,
- 'msg' => '微信支付订单号不能为空'
- ];
- }
- if (!$out_trade_no) {
- return [
- 'code' => 1,
- 'msg' => '商户订单号不能为空'
- ];
- }
- try {
- parent::init($store_id, $is_platform);
- $params = [
- "transaction_id" => $transaction_id,
- "out_order_no" => $out_trade_no,
- "description" => $description
- ];
- $res = self::$wechat_pay->profit_sharing->markOrderAsFinished($params);
- if (!$res) {
- return [
- 'code' => 1,
- 'msg' => '调用失败',
- ];
- }
- if ($res['return_code'] != 'SUCCESS') {
- return [
- 'code' => 1,
- 'msg' => '分账完结调用失败:,' . (isset($res['return_msg']) ? $res['return_msg'] : ''),
- 'res' => $res,
- ];
- }
- if ($res['result_code'] != 'SUCCESS') {
- return [
- 'code' => 1,
- 'msg' => '分账完结结果失败:,' . (isset($res['err_code_des']) ? $res['err_code_des'] : ''),
- 'res' => $res,
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '请求成功',
- 'res' => $res
- ];
- } catch (InvalidArgumentException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidConfigException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|