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() ]; } } }