| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- <?php
- namespace app\utils\Allinpay;
- use app\models\Option;
- use app\constants\OptionSetting;
- use app\models\Order;
- use app\models\OrderUnion;
- use app\models\SaasUser;
- use app\utils\OrderNo;
- use app\models\AccountLog;
- use app\modules\admin\models\SaasForm;
- use app\models\SaasDistribution;
- use app\utils\Wechat\WechatMini;
- use app\utils\Alipay\Alipay;
- use app\utils\Allinpay\AppUtil;
- class Allinpay {
- public static $api_host_url = 'https://vsp.allinpay.com/';
- // public static $api_host_url = 'https://syb-test.allinpay.com/';
- public static $notify_url = 'allinpay/notify';
- public static $h5_url = 'order/order/order';
- public static $confs = [];
- public static function conf($store_id = 0, $refresh = 0) {
- if (isset(self::$confs[$store_id]) && !$refresh) {
- return self::$confs[$store_id];
- }
- $confDef = [
- "cusid" => "",
- "appid" => "",
- "rsa_private_key" => "",
- "rsa_public_key" => "",
- 'app_alipay_mini_id' => '',
- 'app_wx_gh_id' => '',
- 'app_wx_gh_id_type' => 0,
- 'wx_other_mini' => 0,
- 'wx_other_mini_appId' => 'wxef277996acc166c3', //https://aipboss.allinpay.com/know/devhelp/main.php?pid=38#mid=1050
- ];
- $conf = Option::get(OptionSetting::ALLINPAY, $store_id, 'store')['value'];
- if ($conf) {
- $conf = array_merge($confDef, json_decode($conf, true));
- } else {
- $conf = $confDef;
- }
- self::$confs[$store_id] = $conf;
- return $conf;
- }
- public static function saveConf($store_id = 0, $config = []) {
- $oldConf = self::conf($store_id, 1);
- $conf = array_merge($oldConf, $config);
- $set = Option::set(OptionSetting::ALLINPAY, json_encode($conf), $store_id, 'store');
- self::conf($store_id, 1);
- return $set;
- }
- public static function request($url, $params) {
- $ch = curl_init();
- $this_header = array("content-type: application/x-www-form-urlencoded;charset=UTF-8");
- curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); //如果不加验证,就设false,商户自行处理
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- $output = curl_exec($ch);
- curl_close($ch);
- return $output;
- }
- public static function apiData($store_id, $params) {
- $conf = self::conf($store_id);
- $params["cusid"] = $conf['cusid'];
- $params["appid"] = $conf['appid'];
- // $params["version"] = '11';
- $params["randomstr"] = microtime(true);
- $params["signtype"] = 'RSA';
- $params["sign"] = urlencode(AppUtil::Sign($params, $conf['rsa_private_key'])); //签名
- debug_log([$params["randomstr"], "请求参数:", $params], __CLASS__ . '.log');
- return $params;
- }
- public static function api($store_id, $path, $params) {
- $conf = self::conf($store_id);
- $params = self::apiData($store_id, $params);
- $paramsStr = AppUtil::ToUrlParams($params);
- $url = self::$api_host_url . $path;
- $rsp = self::request($url, $paramsStr);
- $rspArray = json_decode($rsp, true);
- debug_log([$params["randomstr"], "请求返回:", $rspArray, $rsp], __CLASS__ . '.log');
- if($rspArray['retcode'] !== 'SUCCESS'){
- return [
- 'code' => 1,
- 'msg' => $rspArray['retmsg'],
- 'data' => $rspArray,
- ];
- }
- if ($conf['rsa_public_key'] && !AppUtil::validSign($rspArray, $conf['rsa_public_key'])) {
- debug_log([$params["randomstr"], "验签错误:"], __CLASS__ . '.log');
- return [
- 'code' => 1,
- 'msg' => '验签错误。' . $rspArray['errmsg'],
- 'data' => $rspArray,
- ];
- }
- return [
- 'code' => 0,
- 'msg' => 'ok',
- 'data' => $rspArray,
- ];
- }
- public static function pay($pay_type, $order, $type, $goods_names = null, $total_pay_price = 0, $is_app = false, $balance_price = 0, $is_h5 = false, $is_official = false) {
- if (!$order || !in_array($type, OrderNo::$validOrderType) || ($type != OrderNo::ORDER_UNION && !$goods_names)) {
- return [
- 'code' => 1,
- 'msg' => '订单信息或订单类型错误'
- ];
- }
- $store_id = $order->store_id;
- $conf = self::conf($store_id);
- $sub_appid = '';
- $open_id = '';
- if($pay_type == Order::PAY_TYPE_ALLINPAY_WX){
- if ($is_app) {
- $open_id = get_user()->wechat_app_open_id;
- } else if ($is_official) {
- $open_id = get_user()->wechat_platform_open_id;
- } else {
- $open_id = get_user()->wechat_open_id;
- }
- $mini = WechatMini::getWechatConfig($store_id);
- $mini && $sub_appid = $mini->getConfig()['app_id'];
- }
- if($pay_type == Order::PAY_TYPE_ALLINPAY_ALIPAY){
- $open_id = get_user()->alipay_open_id;
- Alipay::init();
- Alipay::$alipay_conf && $sub_appid = Alipay::$alipay_conf['app_id'];
- }
-
-
-
- if ($type == OrderNo::ORDER_UNION) {
- $goods_title = count($order) . '笔订单合并支付';
- $out_trade_no = OrderNo::getOrderNo(OrderNo::ORDER_UNION);
- $total_fee = $balance_price > 0 ? floatval($total_pay_price - $balance_price) * 100 : $total_pay_price * 100;
- $order_union = new OrderUnion();
- $order_union->store_id = get_store_id();
- $order_union->user_id = get_user()->id;
- $order_union->order_no = $out_trade_no;
- $order_union->price = $total_pay_price;
- $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 {
- $pay_price = $order->pay_price;
- // if ($type != OrderNo::ORDER_RECHARGE) {
- // if (isset($order->is_delivery) && $order->is_delivery == 1) {
- // $deliveryInfo = DeliveryInfo::find()->where(['order_no' => $order->order_no])->one();
- // if ($deliveryInfo) {
- // $pay_price += $deliveryInfo->fee;
- // }
- // }
- // }
- $goods_title = mb_substr($goods_names, 0, 20);
- $out_trade_no = $order->order_no;
- $total_fee = $balance_price > 0 ? floatval($pay_price - $balance_price) * 100 : $pay_price * 100;
- }
- # 支付设置
- $payment_params = array(
- "trxamt"=> $total_fee,
- // "version"=> 11,
- // "reqsn"=> $out_trade_no,
- "unireqsn"=> $out_trade_no,
- // "paytype"=> $pay_channel,
- "body"=> $goods_title,
- 'acct' => $open_id,
- 'notify_url' => pay_notify_url(self::$notify_url),
- // 'sub_appid' => $sub_appid,
- );
- if($pay_type == Order::PAY_TYPE_ALLINPAY_WX){
- if ($is_h5) {
- $pay_channel = 'W02';
- $payment_params['front_url'] = pay_notify_url(self::$h5_url);
- } else if ($is_app) {
- $pay_channel = 'W03';
- } else {
- $pay_channel = 'W06';
- }
- }
- if($pay_type == Order::PAY_TYPE_ALLINPAY_ALIPAY){
- $pay_channel = 'A01';
- }
- $payment_params['paytype'] = $pay_channel;
- // // 是否走分账
- // $pay_mode = '';
- // if (is_profit_sharing()) {
- // $pay_mode = 'delay';
- // }
- // //强制走分账流程,减少判断逻辑
- // $pay_mode = 'delay';
-
- if($order->allinpay_payment_id){
- $info = self::api($store_id, 'apiweb/tranx/query', ['trxid' => $order->allinpay_payment_id]);
- if ($info['code']){
- return $info;
- } else {
- $info = $info['data'];
- if($info['trxstatus'] === '0000'){
- return [
- 'code' => 1,
- 'msg' => '支付状态异常,已支付',
- ];
- }
- }
- }
-
- if($conf['wx_other_mini']){
- $payment_params['version'] = 12;
- $return = [
- 'code' => 0,
- 'msg' => 'success',
- 'wx_other_mini' => $conf['wx_other_mini'],
- 'wx_other_mini_appId' => $conf['wx_other_mini_appId'],
- 'res' => self::apiData($store_id, $payment_params),
- 'data' => $payment_params,
- '$payment_params' => $payment_params,
- ];
- $return['order_no'] = $payment_params['out_trade_no'];
- $return['body'] = $goods_title;
- return $return;
- }
- $pay = self::api($store_id, 'apiweb/unitorder/pay', $payment_params);
- # 对支付结果进行处理
- if ($pay['code']){
- return $pay;
- } else {
- $pay = $pay['data'];
- if($pay['trxstatus'] !== '0000'){
- return [
- 'code' => 1,
- 'msg' => '交易失败1,' . (string)$pay['errmsg'],
- ];
- }
- $payinfo = $pay['payinfo'];
- if($pay_type == Order::PAY_TYPE_ALLINPAY_WX){
- $payinfo = json_decode($pay['payinfo'], true);
- }
- //成功处理
- $return = [
- 'code' => 0,
- 'msg' => 'success',
- 'res' => $pay,
- 'data' => $payinfo,
- '$payment_params' => $payment_params,
- ];
- if ($type == OrderNo::ORDER_UNION) {
- foreach ($order as $value) {
- $value->order_union_id = $value->id;
- $value->allinpay_payment_id = $pay['trxid'];
- $value->allinpay_trxcode = $pay['trxcode'];
- $value->save();
- }
- $return['order_no'] = $payment_params['out_trade_no'];
- $return['body'] = $goods_title;
- }else{
- $order->allinpay_payment_id = $pay['trxid'];
- $order->allinpay_trxcode = $pay['trxcode'];
- $order->save();
- }
- if ($is_h5) {
- $return['res']['mweb_url'] = $pay['payinfo'] . '&redirect_url=' . \Yii::$app->request->hostInfo . '/h5/#/order/order/order';
- }
- return $return;
- }
- }
- /**
- * 退款
- * @param Object $order
- * @param string $orderRefundNo
- * @param string $type
- * @param integer $refundFee
- * @param $refund_account
- * @return array
- */
- public static function orderRefund($order, $type, $refundFee, $orderRefundNo, $refund_account = null) {
- $store_id = $order->store_id;
- $info = self::api($store_id, 'apiweb/tranx/query', ['trxid' => $order->allinpay_payment_id]);
- if ($info['code']){
- return $info;
- } else {
- $info = $info['data'];
- if($info['trxstatus'] !== '0000'){
- return [
- 'code' => 1,
- 'msg' => '支付状态异常,未支付',
- ];
- }
- }
- $refund_params = [
- "oldtrxid"=> $order->allinpay_payment_id,
- "reqsn"=> $orderRefundNo,
- "trxamt"=> $refundFee * 100,
- "remark"=> "退款",
- ];
- # 发起退款
- $refund = self::refund($store_id, $refund_params);
- // 联合支付,退余额
- if ($order->is_combine_pay == 1 && $order->combine_money > 0) {
- AccountLog::saveLog($order->user_id, $order->combine_money, AccountLog::TYPE_BALANCE, AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_REFUND_ORDER, $order->id, "商城订单退款,订单号:{$order->order_no}");
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $refund->result,
- ];
- }
- public static function refund($store_id, $refund_params) {
- # 发起撤销
- $cancel = self::api($store_id, 'apiweb/tranx/cancel', $refund_params);
- if (!$cancel['code']){
- $cancel = $cancel['data'];
- if($cancel['trxstatus'] === '0000'){
- return [
- 'code' => 0,
- 'msg' => '退款成功。',
- ];
- }
- }
- # 发起退款
- $refund = self::api($store_id, 'apiweb/tranx/refund', $refund_params);
- if (!$refund['code']){
- $refund = $refund['data'];
- if($refund['trxstatus'] !== '0000'){
- return [
- 'code' => 1,
- 'msg' => '退款成功。',
- ];
- }
- }
- return [
- 'code' => 1,
- 'msg' => '退款失败1,' . (string)$cancel['errmsg'] . ';' . (string)$refund['errmsg'],
- ];
- }
- }
|