| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\utils\Alipay;
- use app\models\Option;
- use app\models\OrderUnion;
- use app\models\SharingReceiver;
- use app\models\StoreAliMini;
- use app\models\StoreMiniToken;
- use app\models\User;
- use app\utils\OrderNo;
- use Symfony\Component\HttpFoundation\Response;
- use Yansongda\Pay\Exceptions\GatewayException;
- use Yansongda\Pay\Exceptions\InvalidConfigException;
- use Yansongda\Pay\Exceptions\InvalidSignException;
- use Yansongda\Pay\Pay;
- use Yansongda\Supports\Collection;
- use yii\base\BaseObject;
- use yii\helpers\Json;
- /**
- * Class Alipay 证书公钥模式
- * 支付宝支付相关操作
- * @package app\utils
- */
- class AlipayUnion
- {
- const FLAG_REFUND = 'refund';
- const FLAG_NORMAL = 'wap';
- const FLAG_TRANSFER = 'transfer';
- private static $validFlag = [
- self::FLAG_NORMAL,
- self::FLAG_REFUND,
- self::FLAG_TRANSFER
- ];
- /**
- * @var \Yansongda\Pay\Gateways\Alipay
- */
- public static $alipay;
- public static $store_mini;
- public static $seller_id;
- /**
- * @var string
- * 回调url
- */
- private static $notify_url = 'alipay/notify';
- /**
- * @param $return_url
- * @param $level
- * 获取支付宝实例
- */
- private function __construct($return_url = 'www.baidu.com', $is_app = false, $store_id = 0)
- {
- if (!$is_app) {
- $store_mini = StoreAliMini::find()->where(['store_id' => $store_id ?: get_store_id()])->select('id, auth_token, userid')->orderBy('id desc')->asArray()->one();
- if (empty($store_mini)) {
- $store_mini = StoreAliMini::find()->where(['and', ['<>', 'auth_token', ''], ['<>', 'userid', '']])->orderBy('id desc')->asArray()->one();
- if (empty($store_mini)) {
- return [
- 'code' => 1,
- 'msg' => "获取不到商户的支付信息=-="
- ];
- }
- }
- self::$store_mini = $store_mini;
- }
- // if ($is_app) {
- // $store_token = StoreMiniToken::find()->where(['batch_status' => [2, 6], 'store_id' => get_store_id()])->select('app_auth_token')->asArray()->one();
- // $store_mini['auth_token'] = $store_token['app_auth_token'];
- // }
- $alipay_appid = Option::get('alipay_appid', '0', 'saas')['value'];
- $alipay_public_key = Option::get('alipay_public_key', '0', 'saas')['value'];
- $alipay_app_private_key = Option::get('alipay_app_private_key', '0', 'saas')['value'];
- //三方平台信息
- $config = [
- 'app_id' => $alipay_appid,
- 'notify_url' => pay_notify_url(self::$notify_url),
- 'return_url' => $return_url,
- 'private_key' => $alipay_app_private_key,
- 'log' => [ // optional
- 'file' => \Yii::$app->basePath . '/runtime/logs/alipay.log',
- 'level' => 'debug', // 建议生产环境等级调整为 info,开发环境为 debug
- 'type' => 'single', // optional, 可选 daily.
- 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
- ],
- 'ali_public_key' => $alipay_public_key,
- 'app_auth_token' => $store_mini['auth_token']
- // 'app_cert_public_key' => \Yii::$app->basePath. '/utils/Alipay/CRT/app_cert_public_key.crt', // 应用公钥证书路径
- // 'ali_public_key' => \Yii::$app->basePath. '/utils/Alipay/CRT/alipay_cert_public_key.crt', // 支付宝公钥证书路径
- // 'alipay_root_cert' => \Yii::$app->basePath. '/utils/Alipay/CRT/alipay_root_cert.crt', // 支付宝根证书路径
- // 'mode' => 'dev' // optional,设置此参数,将进入沙箱模式
- ];
- if ((int)$store_id === 0) {
- $keys = [
- 'alipay_platform'
- ];
- $data = Option::get($keys, 0, 'saas');
- if(isset($data[0]['value'])) {
- $alipay_config = json_decode($data[0]['value'],true);
- $alipay_appid = $alipay_config['app_id'];
- $alipay_public_key = $alipay_config['alipay_public_key'];
- $alipay_app_private_key = $alipay_config['app_private_key'];
- self::$seller_id = $alipay_config['user_id'];
- }
- $config = [
- 'app_id' => $alipay_appid,
- 'notify_url' => pay_notify_url(self::$notify_url),
- 'return_url' => $return_url,
- 'private_key' => $alipay_app_private_key,
- 'log' => [ // optional
- 'file' => \Yii::$app->basePath . '/runtime/logs/alipay.log',
- 'level' => 'debug', // 建议生产环境等级调整为 info,开发环境为 debug
- 'type' => 'single', // optional, 可选 daily.
- 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
- ],
- 'ali_public_key' => $alipay_public_key,
- // 'app_auth_token' => $store_mini['auth_token']
- // 'app_cert_public_key' => \Yii::$app->basePath. '/utils/Alipay/CRT/app_cert_public_key.crt', // 应用公钥证书路径
- // 'ali_public_key' => \Yii::$app->basePath. '/utils/Alipay/CRT/alipay_cert_public_key.crt', // 支付宝公钥证书路径
- // 'alipay_root_cert' => \Yii::$app->basePath. '/utils/Alipay/CRT/alipay_root_cert.crt', // 支付宝根证书路径
- // 'mode' => 'dev' // optional,设置此参数,将进入沙箱模式
- ];
- }
- self::$alipay = Pay::alipay($config);
- }
- /**
- * 静态方法调用construct方法
- * @param $return_url
- */
- public static function init($return_url = '', $is_app = 'false', $store_id = 0) {
- new self($return_url, $is_app, $store_id);
- }
- /**
- * 扫码支付
- * @param $order
- * @param $subject
- * @param $return_url
- * @return Collection | array
- */
- public static function scan($order, $subject, $return_url = '', $user_id = null, $store_id = 0) {
- self::init($return_url, 0, $store_id);
- $store_mini = self::$store_mini;
- if ((int)$store_id !== 0) {
- $seller_id = $store_mini['userid'];
- } else {
- $seller_id = self::$seller_id;
- }
- /** 逻辑开始 **/
- $order = [
- 'body' => $subject,
- 'out_trade_no' => $order->order_no,
- 'total_amount' => $order->pay_price,
- 'subject' => $subject,
- 'buyer_id' => $user_id,
- 'seller_id' => $seller_id
- ];
- try {
- $res = self::$alipay->mini($order);
- if ($res && $res->code === '10000' && $res->msg === 'Success') {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res->toArray()
- ];
- }
- return [
- 'code' => $res->code,
- 'msg' => $res->msg,
- ];
- } catch (GatewayException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidSignException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidConfigException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 转账
- * @param $order
- * @param $mail
- * @param $return_url
- * @return Collection | array
- */
- public static function transfer($order, $mail, $return_url = '') {
- if (empty($mail)) {
- return [
- 'code' => 1,
- 'msg' => 'Params are wrong'
- ];
- }
- self::init($return_url);
- /** 逻辑开始 **/
- $order = [
- 'out_biz_no' => $order->order_no,
- 'total_amount' => $order->pay_price,
- 'product_code' => 'TRANS_ACCOUNT_NO_PWD',
- 'payee_info' => [
- 'identity' => $mail,
- 'identity_type' => 'ALIPAY_LOGON_ID',
- ],
- ];
- try {
- $res = self::$alipay->transfer($order);
- if ($res && $res->code === '10000' && $res->msg === 'Success') {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res->toArray()
- ];
- }
- return [
- 'code' => $res->code,
- 'msg' => $res->msg,
- ];
- } catch (GatewayException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidSignException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidConfigException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * app支付
- * @param $order
- * @param User $user
- * @param $subject
- * @param $balance_price
- * @param $return_url
- * @return Response | false | string | array
- */
- public static function app($order, $subject, $user, $balance_price = 0, $return_url = '') {
- self::init($return_url, true);
-
- /** 逻辑开始 **/
- $order = [
- 'body' => $subject,
- 'out_trade_no' => $order->order_no,
- 'total_amount' => $balance_price > 0 ? ($order->pay_price - $balance_price) : $order->pay_price,
- 'subject' => $subject,
- 'extend_params' => [
- 'royalty_freeze' => 'true'
- ]
- ];
- \Yii::error($order);
- try {
- $res = self::$alipay->app($order)->getContent();
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res,
- 'res' => $res
- ];
- }
- /**
- * 批量转账
- * @param $params
- * @return Collection | array
- */
- public static function batchTransfer($params) {
- self::init('');
- /** 逻辑开始 **/
- $order = [
- 'out_batch_no' => $params['out_batch_no'],
- 'product_code' => 'BATCH_API_TO_ACC_NO_PWD',
- 'biz_scene' => 'MESSAGE_BATCH_PAY',
- 'total_trans_amount' => $params['total_trans_amount'],
- 'total_count' => 1,
- 'trans_order_list' => [
- [
- 'out_biz_no' => $params['out_batch_no'],
- 'trans_amount' => $params['total_trans_amount'],
- 'payee_info' => [
- 'identity' => $params['account'],
- 'identity_type' => 'ALIPAY_LOGON_ID',
- 'name' => $params['name']
- ],
- 'remark' => $params['title']
- ],
- ],
- 'order_title' => $params['title'],
- 'remark' => '转账',
- ];
- try {
- $res = self::$alipay->batchtransfer($order);
- \Yii::error($res);
- if ($res && $res->code === '10000' && $res->msg === 'Success') {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res->toArray()
- ];
- }
- return [
- 'code' => $res->code,
- 'msg' => $res->msg,
- ];
- } catch (GatewayException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidSignException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidConfigException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 退款
- * @param $order
- * @param $return_url
- * @param $refund_fee
- * @return Collection | array
- */
- public static function refund($order, $refund_fee, $return_url = '') {
- self::init($return_url);
- /** 逻辑开始 **/
- $data = [
- 'out_trade_no' => $order->order_no,
- 'refund_amount' => $refund_fee,
- ];
- try {
- $res = self::$alipay->refund($data);
- if ($res && $res->code === '10000' && $res->msg === 'Success') {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res->toArray()
- ];
- }
- return [
- 'code' => $res->code,
- 'msg' => $res->msg,
- ];
- } catch (GatewayException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidSignException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidConfigException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 小程序支付
- * @param $order
- * @param $subject
- * @param User $user
- * @param $return_url
- * @param $is_food
- * @return Collection | array
- */
- public static function mini($order, $subject, $user, $return_url = '', $type = '', $total_pay_price = 0, $balance_price = 0) {
- self::init($return_url);
- if (!$order || ($type != OrderNo::ORDER_UNION && empty($subject)) || !$user) {
- return [
- 'code' => 1,
- 'msg' => '参数有误'
- ];
- }
- if ($type == OrderNo::ORDER_UNION) {
- $order = [
- 'body' => '订单合并支付',
- 'out_trade_no' => OrderNo::getOrderNo(OrderNo::ORDER_UNION),
- 'total_amount' => $balance_price > 0 ? $total_pay_price - $balance_price : $total_pay_price,
- 'subject' => count($order) . '笔订单合并支付',
- 'buyer_id' => $user->ali_user_id
- ];
- $order_union = new OrderUnion();
- $order_union->store_id = get_store_id();
- $order_union->user_id = get_saas_user()->id;
- $order_union->order_no = $order['out_trade_no'];
- $order_union->price = $order['total_amount'];
- $order_union->is_pay = 0;
- $order_union->created_at = time();
- $order_union->is_delete = 0;
- $order_id_list = [];
- foreach ($order as $value) {
- $order_id_list[] = $value->id;
- }
- $order_union->order_id_list = json_encode($order_id_list);
- if (!$order_union->save()) {
- foreach ($order_union->errors as $error) {
- return [
- 'code' => 1,
- 'msg' => $error
- ];
- }
- }
- } else {
- $order = [
- 'body' => $subject,
- 'out_trade_no' => $order->order_no,
- 'total_amount' => $balance_price > 0 ? ($order->pay_price - $balance_price) : $order->pay_price,
- 'subject' => $subject,
- 'buyer_id' => $user->ali_user_id
- ];
- }
- try {
- $res = self::$alipay->mini($order);
- if ($res && $res->code === '10000' && $res->msg === 'Success') {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res->toArray()
- ];
- }
- return [
- 'code' => $res->code,
- 'msg' => $res->msg,
- ];
- } catch (GatewayException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidSignException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidConfigException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 绑定关系
- * @param string $out_trade_no
- * @param array $receiver_list
- * @return array
- */
- public static function bind($out_trade_no, $receiver_list) {
- // 绑定关系必须为真实姓名。个人账户为个人真实姓名,企业账户为企业名称
- \Yii::warning($receiver_list);
- $valid_receive_type = array_keys(SharingReceiver::$validAlipayReceiveType);
- foreach ($receiver_list as $index => $receiver) {
- if (empty($receiver['account']) || !in_array($receiver['type'], $valid_receive_type) || empty($receiver['name'])) {
- unset($receiver_list[$index]);
- \Yii::warning('<========================> 订单号:' . $out_trade_no . '添加分账关系校验参数错误:' . json_encode($receiver));
- continue;
- }
- $receiver_list[$index]['type'] = SharingReceiver::$validAlipayReceiveType[$receiver['type']];
- }
- if (empty($receiver_list)) {
- return [
- 'code' => 1,
- 'msg' => '接收方为空'
- ];
- }
- self::init(get_store_id());
- $params = [
- 'receiver_list' => $receiver_list,
- 'out_request_no' => $out_trade_no
- ];
- /** 逻辑开始 **/
- try {
- $res = self::$alipay->bind($params)->get('alipay_trade_royalty_relation_bind_response');
- \Yii::warning($res);
- if ($res && $res['code'] === '10000' && $res['msg'] === 'Success' && $res['result_code'] == 'SUCCESS') {
- return [
- 'code' => 0,
- 'msg' => '绑定关系成功'
- ];
- }
- return [
- 'code' => 1,
- 'msg' => $res['sub_msg'],
- 'res' => $res
- ];
- } catch (GatewayException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidSignException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidConfigException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * settle 分账结算
- * @param integer $page
- * @param integer $page_size
- * @param string $out_trade_no
- * @param array $parameters
- * @param string $is_end
- * @return array
- */
- public static function settle($out_trade_no, $alipay_trade_no, $parameters, $store_id, $is_end = 'false') {
- if (empty($out_trade_no) || empty($alipay_trade_no) || empty($parameters)) {
- return [
- 'code' => 1,
- 'msg' => '参数有误'
- ];
- }
- $params = [
- 'out_request_no' => $out_trade_no,
- 'trade_no' => $alipay_trade_no,
- 'royalty_parameters' => $parameters,
- 'extend_params' => [
- 'royalty_finish' => $is_end
- ]
- ];
- self::init($store_id);
- /** 逻辑开始 **/
- try {
- $res = self::$alipay->settle($params)->get('alipay_trade_order_settle_response');
- \Yii::warning($res);
- if ($res && $res['code'] === '10000' && $res['msg'] === 'Success') {
- return [
- 'code' => 0,
- 'msg' => 'success'
- ];
- }
- return [
- 'code' => 1,
- 'msg' => $res['sub_msg'],
- 'res' => $res
- ];
- } catch (GatewayException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidSignException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidConfigException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 同步订单
- * @param array $params 订单信息
- * @return array
- */
- public static function order_sync($params, $store_id) {
- if (empty($params)) {
- return [
- 'code' => 1,
- 'msg' => '参数有误'
- ];
- }
- self::init($store_id);
- /** 逻辑开始 **/
- try {
- $res = self::$alipay->orderSync($params)->get('alipay_merchant_order_sync_response');
- \Yii::warning($res);
- if ($res && $res['code'] === '10000' && $res['msg'] === 'Success') {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res
- ];
- }
- return [
- 'code' => 1,
- 'msg' => $res['sub_msg'],
- 'res' => $res
- ];
- } catch (GatewayException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidSignException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- } catch (InvalidConfigException $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|