getFind(array('id' => $Order['storeid']))['storetag']; $item ['total_amount'] = $Order['trade_amount']; if (!empty($Order['face_token'])) { $item ['scene'] = 'security_code'; $item ['auth_code'] = $Order['face_token']; } else { $item ['auth_code'] = $Order['code']; $item ['scene'] = 'bar_code'; } $aliCodePay = $this->alCodePay($item, $Order, $bill_id, $config); if ($aliCodePay['status'] == 1) { //收款成功 $save['trade_no'] = $aliCodePay['trade_no']; $save['order_state'] = 'SUCCESS'; $save['trade_date'] = time(); $save['extend_content'] = json_encode($aliCodePay); $PayOrderModel = D('PayOrder'); $update = $PayOrderModel->updateOrder(array('id' => $Order['id']), $save); if ($update) { $data['status'] = 1; $data['msg'] = '收款成功'; $data['order_id'] = $Order['out_trade_no']; A('Sellapi/NewPay')->changeOrder($bill_id, 4); $this->ajaxReturn($data); } else { $data['status'] = 0; $data['msg'] = '收款成功,订单更新失败,请联系服务商'; $this->ajaxReturn($data); } } else { //收款失败 $data['status'] = 0; $data['msg'] = $aliCodePay['msg']; $this->ajaxReturn($data); } } protected function alCodePay($item, $Order, $bill_id, $config) { vendor('AopSDK.AopClient'); if ($Order["m_pay_type"] == 1) { if (empty($config['alipay_appid']) || empty($config['alipay_private_key']) || empty($config['alipay_public_key'])) { $data['status'] = 0; $data['msg'] = '商家支付宝参数未配置'; $this->ajaxReturn($data); } $aop = new \AopClient(); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = $config['alipay_appid']; $aop->rsaPrivateKey = $config['alipay_private_key']; $aop->alipayrsaPublicKey = $config['alipay_public_key']; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; } else { $id = $config["merchantid"]; $partnerid = M("retail_merchant")->where(array('id' => $id))->getField("partnerid"); $AliPay = M("systemconfig")->where(array("partnerid" => $partnerid))->find(); if (!empty($AliPay['alipay_pid'])) { $item ['extend_params'] = array('sys_service_provider_id' => $AliPay['alipay_pid']); } if (empty($AliPay['alipay_appid']) || empty($AliPay['alipay_private_key']) || empty($AliPay['alipay_public_key'])) { $data['status'] = 0; $data['msg'] = '商家支付宝参数未配置'; $this->ajaxReturn($data); } $aop = new \AopClient (); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = $AliPay['alipay_appid']; $aop->rsaPrivateKey = $AliPay['alipay_private_key']; $aop->alipayrsaPublicKey = $AliPay['alipay_public_key']; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; } vendor('AopSDK.request.AlipayTradePayRequest'); $request = new \AlipayTradePayRequest(); $request->setBizContent(json_encode($item)); if ($Order["m_pay_type"] == 1) { $result = $aop->execute($request); } else { $result = $aop->execute($request, "", $config["alipay_token"]); } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; if (!empty($resultCode) && $resultCode == 10000) { //交易成功 $result = json_decode(json_encode($result), true)['alipay_trade_pay_response']; $result['status'] = 1; return $result; } else if (!empty($resultCode) && $resultCode == 10003) { //需要用户输入支付密码 $this->searchAndUpdateOrder($Order, $bill_id); } else { //交易失败 $result1['status'] = 0; $result1['msg'] = '交易失败'; $result1['error_msg'] = json_encode($result); return $result1; } } //支付金额过大用户输入支付密码,反复请求支付结果,超时就撤销订单,会有返回值 protected function searchAndUpdateOrder($Order, $bill_id) { $res = $this->inquire_order($Order); if ($res['trade_status'] == 'WAIT_BUYER_PAY') { //用户支付中 $time = get_runtime(); if ($time < 40) { $this->searchAndUpdateOrder($Order, $bill_id); } else { //超时,撤销订单 $data = $this->revocation($Order); $this->ajaxReturn($data); } } else if ($res['trade_status'] == 'TRADE_SUCCESS') { //支付成功 $PayOrderModel = D('PayOrder'); $save['trade_no'] = $res['trade_no']; $save['order_state'] = 'SUCCESS'; $save['trade_date'] = strtotime($res['send_pay_date']); $save['extend_content'] = json_encode($res); $up = $PayOrderModel->updateOrder(array('out_trade_no' => $Order['out_trade_no']), $save); if ($up) { $data['status'] = 1; $data['msg'] = '收款成功'; $data['order_id'] = $Order['out_trade_no']; A('Sellapi/NewPay')->changeOrder($bill_id, 4); $this->ajaxReturn($data); } else { $data['status'] = 0; $data['msg'] = '支付成功但订单更新失败,请联系管理员'; $this->ajaxReturn($data); } } else { $data['status'] = 0; $data['msg'] = '用户取消支付'; $this->ajaxReturn($data); } } //订单查询接口 /* $out_trade_no 商家订单号 * */ protected function inquire_order($Order) { $config = D('Payset')->getFindConfig($Order['storeid']);//获取商户微信配置信息 vendor('AopSDK.AopClient'); $aop = new \AopClient (); vendor('AopSDK.request.AlipayTradeQueryRequest'); if ($Order["m_pay_type"] == 1) { $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = $config['alipay_appid']; $aop->rsaPrivateKey = $config['alipay_private_key']; $aop->alipayrsaPublicKey = $config['alipay_public_key']; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; $request = new \AlipayTradeQueryRequest (); $item['out_trade_no'] = $Order['out_trade_no']; $request->setBizContent(json_encode($item)); $result = $aop->execute($request); } else { $id = $config["merchantid"]; $partnerid = M("retail_merchant")->where(array("id" => $id))->getField("partnerid"); $AliPay = M("systemconfig")->where(array('partnerid' => $partnerid))->find(); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = $AliPay['alipay_appid']; $aop->rsaPrivateKey = $AliPay['alipay_private_key']; $aop->alipayrsaPublicKey = $AliPay['alipay_public_key']; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; $request = new \AlipayTradeQueryRequest (); $item['out_trade_no'] = $Order['out_trade_no']; $request->setBizContent(json_encode($item)); $result = $aop->execute($request, "", $config["alipay_token"]); } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; if (!empty($resultCode) && $resultCode == 10000) { return json_decode(json_encode($result), true)['alipay_trade_query_response']; } else { $data['status'] = 0; $data['msg'] = '订单查询失败'; $this->ajaxReturn($data); } } /* * 支付撤销接口 * * */ public function revocation($Order) { $config = D('Payset')->getFindConfig($Order['storeid']);//获取商户微信配置信息 vendor('AopSDK.AopClient'); $aop = new \AopClient (); vendor('AopSDK.request.AlipayTradeQueryRequest'); if ($Order["m_pay_type"] == 1) { $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = $config['alipay_appid']; $aop->rsaPrivateKey = $config['alipay_private_key']; $aop->alipayrsaPublicKey = $config['alipay_public_key']; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; $request = new \AlipayTradeCancelRequest (); $item['out_trade_no'] = $Order['out_trade_no']; $request->setBizContent(json_encode($item)); $result = $aop->execute($request); } else { $id = $config["merchantid"]; $partnerid = M("retail_merchant")->where(array("id" => $id))->getField("partnerid"); $AliPay = M("systemconfig")->where(array('partnerid' => $partnerid))->find(); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = $AliPay['alipay_appid']; $aop->rsaPrivateKey = $AliPay['alipay_private_key']; $aop->alipayrsaPublicKey = $AliPay['alipay_public_key']; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; $request = new \AlipayTradeCancelRequest (); $item['out_trade_no'] = $Order['out_trade_no']; $request->setBizContent(json_encode($item)); $result = $aop->execute($request, "", $config["alipay_token"]); } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; if (!empty($resultCode) && $resultCode == 10000) { $save['order_state'] = 'REVOKED'; $save['revokedate'] = time(); $PayOrderModel = D('PayOrder'); $up = $PayOrderModel->updateOrder(array('out_trade_no' => $Order['out_trade_no']), $save); if ($up) { $data['status'] = 0; $data['msg'] = '订单超时并已撤销'; } else { $data['status'] = 0; $data['msg'] = '订单超时并已撤销,但信息更新失败,请联系管理员'; } } else { $data['status'] = 0; $data['msg'] = '超时但撤销订单失败,请用户别支付'; } return $data; } /*---------------------------2019-5-17 组合支付开始--------------------------*/ /** * 准备下单 * @param $payOrder * @param $config * @return mixed * @throws \Exception */ public function toAliPay_v1($payOrder, $config) { $aliCodePay = $this->alCodePay_v1($payOrder, $config); if ($aliCodePay['status'] == 1) { //收款成功 $save['trade_no'] = $aliCodePay['trade_no']; $save['order_state'] = 'SUCCESS'; $save['tradedate'] = time(); $save['extend_content'] = json_encode($aliCodePay); $PayOrderModel = D('PayOrder'); $update = $PayOrderModel->updateOrder(array('id' => $payOrder['id']), $save); if ($update) { $result['status'] = 1; $result['msg'] = '收款成功'; } else { $result['status'] = 0; $result['msg'] = '收款成功,订单更新失败,请联系服务商'; } } else { //收款失败 $result['status'] = 0; $result['msg'] = $aliCodePay['msg']; } return $result; } /** * 去下单 * @param $payOrder * @param $config * @return bool|mixed|\SimpleXMLElement * @throws \Exception */ protected function alCodePay_v1($payOrder, $config) { $item ['out_trade_no'] = $payOrder['out_trade_no']; $item ['subject'] = $config['body'] ? $config['body'] : '买单'; $item ['total_amount'] = $payOrder['trade_amount']; if (!empty($payOrder['face_token'])) { $item ['scene'] = 'security_code'; $item ['auth_code'] = $payOrder['face_token']; } else { $item ['auth_code'] = $payOrder['code']; $item ['scene'] = 'bar_code'; } vendor('AopSDK.AopClient'); if ($payOrder["m_pay_type"] == 1) { if (empty($config['alipay_appid']) || empty($config['alipay_private_key']) || empty($config['alipay_public_key'])) { $res['status'] = 0; $res['msg'] = '商家支付宝参数未配置'; return $res; } $aop = new \AopClient(); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = $config['alipay_appid']; $aop->rsaPrivateKey = $config['alipay_private_key']; $aop->alipayrsaPublicKey = $config['alipay_public_key']; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; } else { $AliPay = $config['systemConfig']; if (!empty($AliPay['alipay_pid'])) { $item ['extend_params'] = array('sys_service_provider_id' => $AliPay['alipay_pid']); } if (empty($AliPay['alipay_appid']) || empty($AliPay['alipay_private_key']) || empty($AliPay['alipay_public_key'])) { $res['status'] = 0; $res['msg'] = '商家支付宝参数未配置'; return $res; } $aop = new \AopClient (); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = $AliPay['alipay_appid']; $aop->rsaPrivateKey = $AliPay['alipay_private_key']; $aop->alipayrsaPublicKey = $AliPay['alipay_public_key']; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; } vendor('AopSDK.request.AlipayTradePayRequest'); $request = new \AlipayTradePayRequest(); $request->setBizContent(json_encode($item)); if ($payOrder["m_pay_type"] == 1) { $result = $aop->execute($request); } else { $result = $aop->execute($request, "", $config["alipay_token"]); } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; if (!empty($resultCode) && $resultCode == 10000) { //交易成功 $result = json_decode(json_encode($result), true)['alipay_trade_pay_response']; $result['status'] = 1; return $result; } else if (!empty($resultCode) && $resultCode == 10003) { //需要用户输入支付密码 $result = $this->searchAndUpdateOrder_v1($payOrder, $config); return $result; } else { //交易失败 $result1['status'] = 0; $result1['msg'] = '交易失败'; $result1['error_msg'] = json_encode($result); return $result1; } } /** * 支付金额过大用户输入支付密码,反复请求支付结果,超时就撤销订单 * @param $payOrder * @param $config * @return mixed * @throws \Exception */ protected function searchAndUpdateOrder_v1($payOrder, $config) { $canReturn = false; while (!$canReturn) { $res = $this->inquireOrder_v1($payOrder, $config); if ($res['trade_status'] == 'WAIT_BUYER_PAY') { //用户支付中 $time = get_runtime(); if ($time > 40) { $res = $this->revocation_v1($payOrder, $config); $result = $res; $canReturn = true; } } else if ($res['trade_status'] == 'TRADE_SUCCESS') { //支付成功 $PayOrderModel = D('PayOrder'); $save['trade_no'] = $res['trade_no']; $save['order_state'] = 'SUCCESS'; $save['tradedate'] = strtotime($res['send_pay_date']); $save['extend_content'] = json_encode($res); $up = $PayOrderModel->updateOrder(array('out_trade_no' => $payOrder['out_trade_no']), $save); if ($up) { $result['status'] = 1; $result['msg'] = '收款成功'; $canReturn = true; } else { $result['status'] = 2; $result['msg'] = '支付成功但订单更新失败,请联系管理员'; $canReturn = true; } } else { $result['status'] = 0; $result['msg'] = '用户取消支付'; $canReturn = true; } } return $result; } /** * 订单查询接口 * @param $payOrder * @param $config * @return mixed * @throws \Exception */ protected function inquireOrder_v1($payOrder, $config) { vendor('AopSDK.AopClient'); $aop = new \AopClient (); vendor('AopSDK.request.AlipayTradeQueryRequest'); if ($payOrder["m_pay_type"] == 1) { $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = $config['alipay_appid']; $aop->rsaPrivateKey = $config['alipay_private_key']; $aop->alipayrsaPublicKey = $config['alipay_public_key']; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; $request = new \AlipayTradeQueryRequest (); $item['out_trade_no'] = $payOrder['out_trade_no']; $request->setBizContent(json_encode($item)); $result = $aop->execute($request); } else { $AliPay = $config['systemConfig']; $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = $AliPay['alipay_appid']; $aop->rsaPrivateKey = $AliPay['alipay_private_key']; $aop->alipayrsaPublicKey = $AliPay['alipay_public_key']; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; $request = new \AlipayTradeQueryRequest (); $item['out_trade_no'] = $payOrder['out_trade_no']; $request->setBizContent(json_encode($item)); $result = $aop->execute($request, "", $config["alipay_token"]); } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; if (!empty($resultCode) && $resultCode == 10000) { return json_decode(json_encode($result), true)['alipay_trade_query_response']; } else { $data['status'] = 0; $data['msg'] = '订单查询失败'; $this->ajaxReturn($data); } } /* * 支付撤销接口 * * */ public function revocation_v1($payOrder, $config) { vendor('AopSDK.AopClient'); $aop = new \AopClient (); if ($payOrder["m_pay_type"] == 1) { $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = $config['alipay_appid']; $aop->rsaPrivateKey = $config['alipay_private_key']; $aop->alipayrsaPublicKey = $config['alipay_public_key']; } else { $AliPay = $config['systemConfig']; $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = $AliPay['alipay_appid']; $aop->rsaPrivateKey = $AliPay['alipay_private_key']; $aop->alipayrsaPublicKey = $AliPay['alipay_public_key']; } $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; vendor('AopSDK.request.AlipayTradeCancelRequest'); $request = new \AlipayTradeCancelRequest (); $item['out_trade_no'] = $payOrder['out_trade_no']; $request->setBizContent(json_encode($item)); if ($payOrder["m_pay_type"] == 1) { $result = $aop->execute($request); } else { $result = $aop->execute($request, "", $config["alipay_token"]); } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; if (!empty($resultCode) && $resultCode == 10000) { $save['order_state'] = 'REVOKED'; $save['revokedate'] = time(); $PayOrderModel = D('PayOrder'); $up = $PayOrderModel->updateOrder(array('out_trade_no' => $payOrder['out_trade_no']), $save); if ($up) { $res['status'] = 0; $res['msg'] = '订单超时并已撤销'; } else { $res['status'] = 0; $res['msg'] = '订单超时并已撤销,但信息更新失败,请联系管理员'; } } else { $res['status'] = 0; $res['msg'] = '超时但撤销订单失败,请用户别支付'; } return $res; } public function toAliRefund_v1($payOrder, $config) { vendor('AopSDK.AopClient'); $aop = new \AopClient (); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; if ($payOrder['m_pay_type'] == 1) { $aop->appId = $config['alipay_appid']; $aop->rsaPrivateKey = $config['alipay_private_key']; $aop->alipayrsaPublicKey = $config['alipay_public_key']; } else { $aop->appId = $config['systemConfig']['alipay_appid']; $aop->rsaPrivateKey = $config['systemConfig']['alipay_private_key']; $aop->alipayrsaPublicKey = $config['systemConfig']['alipay_public_key']; } $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; vendor('AopSDK.request.AlipayTradeRefundRequest'); $request = new \AlipayTradeRefundRequest(); $suc['trade_no'] = $payOrder['trade_no']; $suc['refund_amount'] = $payOrder['trade_amount']; $suc['out_request_no'] = $payOrder['out_refund_no']; $request->setBizContent(json_encode($suc)); if ($payOrder['m_pay_type'] == 1) { $result = $aop->execute($request); } else { $result = $aop->execute($request, "", $config["alipay_token"]); } // print_r($result); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; /*print_r($payOrder); print_r($result);*/ if (!empty($resultCode) && $resultCode == 10000) { $res = $this->searchRefund_v1($payOrder, $config); } else { $res['status'] = 0; $res['data1'] = $result; $res['msg'] = '退款失败,请商家检查支付宝可用余额1'; } return $res; } /** * 查询退款状态 * @param $payOrder * @param $config * @return mixed * @throws \Exception */ public function searchRefund_v1($payOrder, $config) { vendor('AopSDK.AopClient'); $aop = new \AopClient (); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; if ($payOrder['m_pay_type'] == 1) { $aop->appId = $config['alipay_appid']; $aop->rsaPrivateKey = $config['alipay_private_key']; $aop->alipayrsaPublicKey = $config['alipay_public_key']; } else { $aop->appId = $config['systemConfig']['alipay_appid']; $aop->rsaPrivateKey = $config['systemConfig']['alipay_private_key']; $aop->alipayrsaPublicKey = $config['systemConfig']['alipay_public_key']; } $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; vendor('AopSDK.request.AlipayTradeFastpayRefundQueryRequest'); $request = new \AlipayTradeFastpayRefundQueryRequest(); $suc['trade_no'] = $payOrder['trade_no']; $suc['out_request_no'] = $payOrder['out_refund_no']; $request->setBizContent(json_encode($suc)); if ($payOrder['m_pay_type'] == 1) { $result = $aop->execute($request); } else { $result = $aop->execute($request, "", $config["alipay_token"]); } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; if (!empty($resultCode) && $resultCode == 10000) { $returnData = json_decode(json_encode($result), true)['alipay_trade_fastpay_refund_query_response']; if ($returnData['msg'] == 'Success') { $res['status'] = 1; $res['data'] = $returnData; $res['msg'] = '退款成功'; } else { $res['status'] = 0; $res['data'] = $returnData; $res['msg'] = '退款失败,请重试'; } } else { $res['status'] = 0; $res['data'] = $result; $res['msg'] = '退款失败'; } return $res; } /*---------------------------2019-5-17 组合支付结束--------------------------*/ /*---------------------------2019-6-22 新版支付开始--------------------------*/ /** * 支付宝当面付准备下单 * @param $payOrder * @param $config * @return mixed * @throws \Exception */ public function toAliPay_v2($payOrder, $config) { $aliCodePay = $this->alCodePay_v2($payOrder, $config); if ($aliCodePay['status'] == 1) { //收款成功 $PayOrderModel = D('RetailPayOrder'); $PaySetModel = D('RetailPaySet'); if ($payOrder[$PayOrderModel::M_PAY_TYPE] == 1) { $mch_id = $config[$PaySetModel::AL_APPID]; } else { if (!empty($config[$PaySetModel::AL_SELLER_ID])) { $mch_id = $config[$PaySetModel::AL_SELLER_ID]; } else { $mch_id = $config[$PaySetModel::AL_TOKEN]; } } $save = array( $PayOrderModel::TRADE_NO => $aliCodePay['trade_no'], $PayOrderModel::ORDER_STATE => 'SUCCESS', $PayOrderModel::TRADE_DATE => time(), $PayOrderModel::EXTEND_CONTENT => json_encode($aliCodePay), $PayOrderModel::MER_ID => $mch_id ); $update = $PayOrderModel->updateOrder(array($PayOrderModel::ID => $payOrder[$PayOrderModel::ID]), $save); if ($payOrder[$PayOrderModel::M_PAY_TYPE] == 2) { $this->doAddRate($aliCodePay['trade_no'], $payOrder, 1);//分润 } if ($update) { $result['status'] = 1; $result['msg'] = '收款成功'; } else { $result['status'] = 0; $result['msg'] = '数据库发生错误,收款成功,订单更新失败,请联系服务商'; } } else { //收款失败 $result['status'] = 0; $result['msg'] = $aliCodePay['msg']; } return $result; } /** * 当面付去下单 * @param $payOrder * @param $config * @return bool|mixed|\SimpleXMLElement * @throws \Exception */ protected function alCodePay_v2($payOrder, $config) { $PayOrderModel = D('RetailPayOrder'); $PaySetModel = D('RetailPaySet'); $item ['out_trade_no'] = $payOrder[$PayOrderModel::OUT_TRADE_NO]; $item ['subject'] = $config['body'] ? $config['body'] : '买单'; $item ['total_amount'] = $payOrder[$PayOrderModel::TRADE_AMOUNT]; if (!empty($payOrder['face_token'])) { $item ['scene'] = 'security_code'; $item ['auth_code'] = $payOrder[$PayOrderModel::FACE_TOKEN]; } else { $item ['auth_code'] = $payOrder[$PayOrderModel::CODE]; $item ['scene'] = 'bar_code'; } vendor('AopSDK.AopClient'); $aop = new \AopClient(); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; if ($payOrder[$PayOrderModel::M_PAY_TYPE] == 1) { if (empty($config[$PaySetModel::AL_APPID]) || empty($config[$PaySetModel::AL_PRI_KEY]) || empty($config[$PaySetModel::AL_PUB_KEY])) { $res['status'] = 0; $res['msg'] = '商家支付宝参数未配置'; return $res; } $aop->appId = $config[$PaySetModel::AL_APPID]; $aop->rsaPrivateKey = $config[$PaySetModel::AL_PRI_KEY]; $aop->alipayrsaPublicKey = $config[$PaySetModel::AL_PUB_KEY]; } else { $AliPay = $config['systemConfig']; $SystemConfigModel = D('SystemConfig'); if (!empty($AliPay[$SystemConfigModel::AL_PID])) { $item ['extend_params'] = array('sys_service_provider_id' => $AliPay[$SystemConfigModel::AL_PID]); } if (empty($AliPay[$SystemConfigModel::AL_APPID]) || empty($AliPay[$SystemConfigModel::AL_PRI_KEY]) || empty($AliPay[$SystemConfigModel::AL_PUB_KEY])) { $res['status'] = 0; $res['msg'] = '商家支付宝参数未配置'; return $res; } $aop->appId = $AliPay[$SystemConfigModel::AL_APPID]; $aop->rsaPrivateKey = $AliPay[$SystemConfigModel::AL_PRI_KEY]; $aop->alipayrsaPublicKey = $AliPay[$SystemConfigModel::AL_PUB_KEY]; } $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; vendor('AopSDK.request.AlipayTradePayRequest'); $request = new \AlipayTradePayRequest(); $request->setBizContent(json_encode($item)); if ($payOrder[$PayOrderModel::M_PAY_TYPE] == 1) { $result = $aop->execute($request); } else { $result = $aop->execute($request, "", $config[$PaySetModel::AL_TOKEN]); } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; $result = json_decode(json_encode($result), true)[$responseNode]; if (!empty($resultCode) && $resultCode == 10000) { //交易成功 $result['status'] = 1; return $result; } else if (!empty($resultCode) && $resultCode == 10003) { //需要用户输入支付密码 $result = $this->searchAndUpdateOrder_v2($payOrder, $config); return $result; } else { //交易失败 $result1['status'] = 0; $result1['msg'] = $result['sub_msg'] ? $result['sub_msg'] : '交易失败'; $result1['error_msg'] = json_encode($result); return $result1; } } /** * 支付金额过大用户输入支付密码,反复请求支付结果,超时就撤销订单 * @param $payOrder * @param $config * @return mixed * @throws \Exception */ protected function searchAndUpdateOrder_v2($payOrder, $config) { $canReturn = false; while (!$canReturn) { $res = $this->inquireOrder_v2($payOrder, $config); if ($res['trade_status'] == 'WAIT_BUYER_PAY') { //用户支付中 $time = get_runtime(); if ($time > 60) { $res = $this->revocation_v2($payOrder, $config); $result = $res; $canReturn = true; } } else if ($res['trade_status'] == 'TRADE_SUCCESS') { //支付成功 $canReturn = true; $result = $res; $result['status'] = 1; } else { $result['status'] = 0; $result['msg'] = '用户取消支付'; $canReturn = true; } } return $result; } /** * 订单查询接口 * @param $payOrder * @param $config * @return mixed * @throws \Exception */ protected function inquireOrder_v2($payOrder, $config) { $PayOrderModel = D('RetailPayOrder'); $PaySetModel = D('RetailPaySet'); vendor('AopSDK.AopClient'); $aop = new \AopClient (); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; if ($payOrder[$PayOrderModel::M_PAY_TYPE] == 1) { $aop->appId = $config[$PaySetModel::AL_APPID]; $aop->rsaPrivateKey = $config[$PaySetModel::AL_PRI_KEY]; $aop->alipayrsaPublicKey = $config[$PaySetModel::AL_PUB_KEY]; } else { $AliPay = $config['systemConfig']; $SystemConfigModel = D('SystemConfig'); $aop->appId = $AliPay[$SystemConfigModel::AL_APPID]; $aop->rsaPrivateKey = $AliPay[$SystemConfigModel::AL_PRI_KEY]; $aop->alipayrsaPublicKey = $AliPay[$SystemConfigModel::AL_PUB_KEY]; } $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; vendor('AopSDK.request.AlipayTradeQueryRequest'); $request = new \AlipayTradeQueryRequest (); $item['out_trade_no'] = $payOrder[$PayOrderModel::OUT_TRADE_NO]; $request->setBizContent(json_encode($item)); if ($payOrder[$PayOrderModel::M_PAY_TYPE] == 1) { $result = $aop->execute($request); } else { $result = $aop->execute($request, "", $config[$PaySetModel::AL_TOKEN]); } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; $result = json_decode(json_encode($result), true)[$responseNode]; if (!empty($resultCode) && $resultCode == 10000) { return $result; } else { $data['status'] = 0; $data['msg'] = '订单查询失败'; $this->ajaxReturn($data); } } /* * 支付撤销接口 * * */ public function revocation_v2($payOrder, $config) { $PayOrderModel = D('RetailPayOrder'); $PaySetModel = D('RetailPaySet'); vendor('AopSDK.AopClient'); $aop = new \AopClient (); vendor('AopSDK.request.AlipayTradeCancelRequest'); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; if ($payOrder[$PayOrderModel::M_PAY_TYPE] == 1) { $aop->appId = $config[$PaySetModel::AL_APPID]; $aop->rsaPrivateKey = $config[$PaySetModel::AL_PRI_KEY]; $aop->alipayrsaPublicKey = $config[$PaySetModel::AL_PUB_KEY]; } else { $AliPay = $config['systemConfig']; $SystemConfigModel = D('SystemConfig'); $aop->appId = $AliPay[$SystemConfigModel::AL_APPID]; $aop->rsaPrivateKey = $AliPay[$SystemConfigModel::AL_PRI_KEY]; $aop->alipayrsaPublicKey = $AliPay[$SystemConfigModel::AL_PUB_KEY]; } $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; $request = new \AlipayTradeCancelRequest (); if (!empty($payOrder[$PayOrderModel::TRADE_NO])) { $item['trade_no'] = $payOrder[$PayOrderModel::TRADE_NO]; } else { $item['out_trade_no'] = $payOrder[$PayOrderModel::OUT_TRADE_NO]; } $request->setBizContent(json_encode($item)); if ($payOrder[$PayOrderModel::M_PAY_TYPE] == 1) { $result = $aop->execute($request); } else { $result = $aop->execute($request, "", $config[$PaySetModel::AL_TOKEN]); } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; $result = json_decode(json_encode($result), true)[$responseNode]; file_put_contents('./cancelWxOrder.txt', date('Y-m-d H:i:s') . json_encode($result) . "\r\n", FILE_APPEND); if (!empty($resultCode) && $resultCode == 10000) { $save[$PayOrderModel::ORDER_STATE] = 'REVOKED'; $save[$PayOrderModel::REVOKE_DATE] = time(); $save[$PayOrderModel::EXTEND_CONTENT] = json_encode($result); $up = $PayOrderModel->updateOrder(array($PayOrderModel::ID => $payOrder[$PayOrderModel::ID]), $save); if ($up) { $res['status'] = 0; $res['msg'] = '订单超时并已撤销'; } else { $res['status'] = 0; $res['msg'] = '订单超时并已撤销,但信息更新失败,请联系管理员'; } } else { $res['status'] = 0; $res['msg'] = '超时但撤销订单失败,请用户别支付'; } return $res; } /** * 退款接口 * @param $payOrder * @param $config * @return mixed * @throws \Exception */ public function toAliRefund_v2($payOrder, $config) { $PayOrderModel = D('RetailPayOrder'); $PaySetModel = D('RetailPaySet'); vendor('AopSDK.AopClient'); $aop = new \AopClient (); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; if ($payOrder[$PayOrderModel::M_PAY_TYPE] == 1) { $aop->appId = $config[$PaySetModel::AL_APPID]; $aop->rsaPrivateKey = $config[$PaySetModel::AL_PRI_KEY]; $aop->alipayrsaPublicKey = $config[$PaySetModel::AL_PUB_KEY]; } else { $AliPay = $config['systemConfig']; $SystemConfigModel = D('SystemConfig'); $aop->appId = $AliPay[$SystemConfigModel::AL_APPID]; $aop->rsaPrivateKey = $AliPay[$SystemConfigModel::AL_PRI_KEY]; $aop->alipayrsaPublicKey = $AliPay[$SystemConfigModel::AL_PUB_KEY]; } $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; vendor('AopSDK.request.AlipayTradeRefundRequest'); $request = new \AlipayTradeRefundRequest(); $suc['trade_no'] = $payOrder[$PayOrderModel::TRADE_NO]; $suc['refund_amount'] = $payOrder[$PayOrderModel::TRADE_AMOUNT]; $suc['out_request_no'] = $payOrder[$PayOrderModel::OUT_TRADE_NO]; $request->setBizContent(json_encode($suc)); if ($payOrder[$PayOrderModel::M_PAY_TYPE] == 1) { $result = $aop->execute($request); } else { $result = $aop->execute($request, "", $config[$PaySetModel::AL_TOKEN]); } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; $result = json_decode(json_encode($result), true)[$responseNode]; if (!empty($resultCode) && $resultCode == 10000) { $res = $this->searchRefund_v2($payOrder, $config); } else { $res['status'] = 0; $res['data'] = $result; $res['msg'] = $result['sub_msg'] ? $result['sub_msg'] : '退款失败,请商家检查支付宝可用余额'; } return $res; } /** * 查询退款状态 * @param $payOrder * @param $config * @return mixed * @throws \Exception */ public function searchRefund_v2($payOrder, $config) { $PayOrderModel = D('RetailPayOrder'); $PaySetModel = D('RetailPaySet'); vendor('AopSDK.AopClient'); $aop = new \AopClient (); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; if ($payOrder[$PayOrderModel::M_PAY_TYPE] == 1) { $aop->appId = $config[$PaySetModel::AL_APPID]; $aop->rsaPrivateKey = $config[$PaySetModel::AL_PRI_KEY]; $aop->alipayrsaPublicKey = $config[$PaySetModel::AL_PUB_KEY]; } else { $AliPay = $config['systemConfig']; $SystemConfigModel = D('SystemConfig'); $aop->appId = $AliPay[$SystemConfigModel::AL_APPID]; $aop->rsaPrivateKey = $AliPay[$SystemConfigModel::AL_PRI_KEY]; $aop->alipayrsaPublicKey = $AliPay[$SystemConfigModel::AL_PUB_KEY]; } $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; vendor('AopSDK.request.AlipayTradeFastpayRefundQueryRequest'); $request = new \AlipayTradeFastpayRefundQueryRequest(); $suc['trade_no'] = $payOrder[$PayOrderModel::TRADE_NO]; $suc['out_request_no'] = $payOrder[$PayOrderModel::OUT_TRADE_NO]; $request->setBizContent(json_encode($suc)); if ($payOrder[$PayOrderModel::M_PAY_TYPE] == 1) { $result = $aop->execute($request); } else { $result = $aop->execute($request, "", $config[$PaySetModel::AL_TOKEN]); } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; $result = json_decode(json_encode($result), true)[$responseNode]; if (!empty($resultCode) && $resultCode == 10000) { if ($result['msg'] == 'Success') { //分润退款 $this->doRefundRate($payOrder[$PayOrderModel::TRADE_NO], $payOrder[$PayOrderModel::TRADE_AMOUNT], 1);//分润 $res['status'] = 1; $res['data'] = $result; $res['msg'] = '退款成功'; } else { $res['status'] = 0; $res['data'] = $result; $res['msg'] = $result['sub_msg'] ? $result['sub_msg'] : '退款失败,请重试'; } } else { $res['status'] = 0; $res['data'] = $result; $res['msg'] = $result['sub_msg'] ? $result['sub_msg'] : '退款失败'; } return $res; } /*---------------------------2019-6-22 新版支付结束--------------------------*/ /** * 查询订单状态并更新订单信息 * @param $order 订单信息 * @return mixed * @throws \Exception */ public function queryOrderAndUpdate($order) { $PaySetModel = new RetailPaySetModel(); $paySet = $PaySetModel->getFindMerchantConfig($order['merchant_id'], 2); $config = $paySet['config']; $aliCodePay = $this->searchAndUpdateOrder_v2($order, $config); if ($aliCodePay['status'] != 1) { return $aliCodePay; } if ($order['m_pay_type'] == 1) { $mch_id = $config[$PaySetModel::AL_APPID]; } else { if (!empty($config[$PaySetModel::AL_SELLER_ID])) { $mch_id = $config[$PaySetModel::AL_SELLER_ID]; } else { $mch_id = $config[$PaySetModel::AL_TOKEN]; } } $save = array( 'trade_no' => $aliCodePay['trade_no'], 'order_state' => 'SUCCESS', 'trade_date' => time(), 'extend_content' => json_encode($aliCodePay), 'mer_id' => $mch_id ); $PayOrderModel = new RetailPayOrderModel(); $update = $PayOrderModel->updateOrder(array('id' => $order['id']), $save); if ($order['m_pay_type'] == 2) { $this->doAddRate($aliCodePay['trade_no'], $order, 1);//分润 } if ($update) { $result['status'] = 1; $result['msg'] = '收款成功'; } else { $result['status'] = 0; $result['msg'] = '数据库发生错误,收款成功,订单更新失败,请联系服务商'; } return $result; } /** * 当面付去下单 2020-10-17 * @param $payOrder * @param $config * @return bool|mixed|\SimpleXMLElement * @throws \Exception */ public function alCodePay_v3($payOrder, $config) { $item ['out_trade_no'] = $payOrder['out_trade_no']; $item ['subject'] = $config['body'] ? $config['body'] : '买单'; $item ['total_amount'] = $payOrder['trade_amount']; if (!empty($payOrder['face_token'])) { $item ['scene'] = 'security_code'; $item ['auth_code'] = $payOrder['face_token']; } else { $item ['auth_code'] = $payOrder['code']; $item ['scene'] = 'bar_code'; } vendor('AopSDK.AopClient'); $aop = new \AopClient(); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; if ($payOrder['m_pay_type'] == 1) { if (empty($config['alipay_appid']) || empty($config['alipay_private_key']) || empty($config['alipay_public_key'])) { $res['status'] = 0; $res['msg'] = '商家支付宝参数未配置'; return $res; } $aop->appId = $config['alipay_appid']; $aop->rsaPrivateKey = $config['alipay_private_key']; $aop->alipayrsaPublicKey = $config['alipay_public_key']; } else { $AliPay = $config['systemConfig']; if (!empty($AliPay['alipay_pid'])) { $item ['extend_params'] = array('sys_service_provider_id' => $AliPay['alipay_pid']); } if (empty($AliPay['alipay_appid']) || empty($AliPay['alipay_private_key']) || empty($AliPay['alipay_public_key'])) { $res['status'] = 0; $res['msg'] = '商家支付宝参数未配置'; return $res; } $aop->appId = $AliPay['alipay_appid']; $aop->rsaPrivateKey = $AliPay['alipay_private_key']; $aop->alipayrsaPublicKey = $AliPay['alipay_public_key']; } $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; vendor('AopSDK.request.AlipayTradePayRequest'); $request = new \AlipayTradePayRequest(); $request->setBizContent(json_encode($item)); if ($payOrder['m_pay_type'] == 1) { $result = $aop->execute($request); } else { $result = $aop->execute($request, "", $config['alipay_token']); } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; $result = json_decode(json_encode($result), true)[$responseNode]; if (!empty($resultCode) && $resultCode == 10000) { //交易成功 $result['status'] = 1; $result['message'] = '发起支付成功'; return $result; } else if (!empty($resultCode) && $resultCode == 10003) { //需要用户输入支付密码 $result['status'] = 1; $result['message'] = '发起支付成功'; return $result; } else { //交易失败 $result1['status'] = 0; $result1['message'] = $result['sub_msg'] ? $result['sub_msg'] : '交易失败'; $result1['error_msg'] = json_encode($result); return $result1; } } /** * 订单查询接口 2020-10-17 * @param $payOrder * @param $config * @return mixed * @throws \Exception */ public function inquireOrder_v3($payOrder, $config) { vendor('AopSDK.AopClient'); $aop = new \AopClient (); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; if ($payOrder['m_pay_type'] == 1) { $aop->appId = $config['alipay_appid']; $aop->rsaPrivateKey = $config['alipay_private_key']; $aop->alipayrsaPublicKey = $config['alipay_public_key']; } else { $AliPay = $config['systemConfig']; $aop->appId = $AliPay['alipay_appid']; $aop->rsaPrivateKey = $AliPay['alipay_private_key']; $aop->alipayrsaPublicKey = $AliPay['alipay_public_key']; } $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; vendor('AopSDK.request.AlipayTradeQueryRequest'); $request = new \AlipayTradeQueryRequest (); $item['out_trade_no'] = $payOrder['out_trade_no']; $request->setBizContent(json_encode($item)); if ($payOrder['m_pay_type'] == 1) { $result = $aop->execute($request); } else { $result = $aop->execute($request, "", $config['alipay_token']); } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; $result = json_decode(json_encode($result), true)[$responseNode]; if (!empty($resultCode) && $resultCode == 10000) { $result['status'] = 1; $data['msg'] = '订单查询成功'; return $result; } else { $data['status'] = 0; $data['msg'] = '订单查询失败'; return $data; } } /** * 支付撤销接口 * @param $payOrder * @param $config * @return mixed * @throws \Exception */ public function revocation_v3($payOrder, $config) { vendor('AopSDK.AopClient'); $aop = new \AopClient (); vendor('AopSDK.request.AlipayTradeCancelRequest'); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; if ($payOrder['m_pay_type'] == 1) { $aop->appId = $config['alipay_appid']; $aop->rsaPrivateKey = $config['alipay_private_key']; $aop->alipayrsaPublicKey = $config['alipay_public_key']; } else { $AliPay = $config['systemConfig']; $aop->appId = $AliPay['alipay_appid']; $aop->rsaPrivateKey = $AliPay['alipay_private_key']; $aop->alipayrsaPublicKey = $AliPay['alipay_public_key']; } $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset = 'utf-8'; $aop->format = 'json'; $request = new \AlipayTradeCancelRequest (); if (!empty($payOrder['trade_no'])) { $item['trade_no'] = $payOrder['trade_no']; } else { $item['out_trade_no'] = $payOrder['out_trade_no']; } $request->setBizContent(json_encode($item)); if ($payOrder['m_pay_type'] == 1) { $result = $aop->execute($request); } else { $result = $aop->execute($request, "", $config['alipay_token']); } $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $resultCode = $result->$responseNode->code; $result = json_decode(json_encode($result), true)[$responseNode]; file_put_contents('./cancelWxOrder.txt', date('Y-m-d H:i:s') . json_encode($result) . "\r\n", FILE_APPEND); $res = [ 'resultCode' => $resultCode, 'result' => $result ]; return $res; } }