/*** 扫码枪支付* @author wzs* @param string $order_num 订单号* @param int $total_amount 订单金额* @param string $auth_code 接收的付款码* @param string $shop_alipay_app_auth_token (服务商下子商户的支付宝授权码,如果是单商户可不传)* @param string $shop_wxpay_sub_mch_id(服务商下子商户的子商户号,如果是单商户可不传)* @return array|bool*/public function sweepCodePay(string $order_num,int $total_amount,string $auth_code,string $shop_alipay_app_auth_token,string $shop_wxpay_sub_mch_id){$start_num = substr($auth_code,0,2);$ali_num_array = [25,26,27,28,29,30];if(in_array($start_num,$ali_num_array)){$content = ['out_trade_no'=>$order_num,'scene'=>'bar_code','auth_code'=>$auth_code,'product_code'=>'FACE_TO_FACE_PAYMENT','subject'=>'支付宝扫码支付','total_amount'=>$total_amount];$content = json_encode($content,JSON_UNESCAPED_UNICODE);$pay_result = SmilePay::barCodePay($content,$shop_alipay_app_auth_token);}else{$content = ['auth_code'=>$auth_code,'order_num'=>$order_num,'subject'=>'微信扫码支付','total_amount'=>$total_amount];$pay_result = WxPay::barcodePay($content,$shop_wxpay_sub_mch_id);}if(empty($pay_result)){return false;}return $pay_result;}/*** 支付宝初始化*/
private static function getInstance()
{
vendor(‘alipay.AopSdk’,’.php’);
if(!self::KaTeX parse error: Expected '}', got 'EOF' at end of input: … self::instance = new \AopClient();
self::instance−>gatewayUrl=config(′alipaygatewayUrl′);//配置信息self::instance->gatewayUrl = config('alipay_gatewayUrl');//配置信息 self::instance−>gatewayUrl=config(′alipaygatewayUrl′);//配置信息self::instance->appId = config(‘alipay_app_id’);
self::instance−>rsaPrivateKey=config(′alipayrsaPrivateKey′);self::instance->rsaPrivateKey = config('alipay_rsaPrivateKey'); self::instance−>rsaPrivateKey=config(′alipayrsaPrivateKey′);self::instance->alipayrsaPublicKey = config(‘alipay_alipayrsaPublicKey’);
self::instance−>apiVersion=′1.0′;self::instance->apiVersion = '1.0'; self::instance−>apiVersion=′1.0′;self::instance->signType = ‘RSA2’;
self::instance−>postCharset=′UTF−8′;self::instance->postCharset='UTF-8'; self::instance−>postCharset=′UTF−8′;self::instance->format=‘json’;
}
}
/*** 支付宝扫码支付* @param $content* @param $shop_alipay_app_auth_token* @return array*/public static function barCodePay($content,$shop_alipay_app_auth_token){self::getInstance();$request = new \AlipayTradePayRequest();$request->setBizContent($content);$result = self::$instance->execute ( $request,null,$shop_alipay_app_auth_token);$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";$resultCode = $result->$responseNode->code;Log::write(json_encode($result->$responseNode,JSON_UNESCAPED_UNICODE),'log');if(!empty($resultCode) && $resultCode == 10000){return ['order_trade_no'=>$result->$responseNode->trade_no,'total_amount'=>$result->$responseNode->total_amount,'order_pay_time'=>strtotime($result->$responseNode->gmt_payment),'order_channel_id'=>1001];} else {return [];}}
/**
* 微信扫码支付
* @author wzs
* @param $content 订单信息
* @param submchid服务商下子商户号(一般单商户不用传)∗@returnarray∗/publicstaticfunctionbarcodePay(sub_mch_id 服务商下子商户号(一般单商户不用传) * @return array */ public static function barcodePay(submchid服务商下子商户号(一般单商户不用传)∗@returnarray∗/publicstaticfunctionbarcodePay(content,$sub_mch_id)
{
$config = array(
‘appid’ => config(‘wxpay_app_id’),
‘mch_id’ => config(‘wxpay_mch_id’),
‘key’ => config(‘wxpay_api_secret’),
);
$unified = array(
‘appid’ => config[′appid′],′submchid′=>config['appid'], 'sub_mch_id'=>config[′appid′],′submchid′=>sub_mch_id,
‘body’ => $content[‘subject’],
‘mch_id’ => $config[‘mch_id’],
‘nonce_str’ => self::nonce_str(),//随机字符串
‘out_trade_no’ => $content[‘order_num’],
‘total_fee’ => config(‘sx_site’) == 1 ? 1 : content[′totalamount′]∗100,′authcode′=>content['total_amount'] * 100, 'auth_code'=>content[′totalamount′]∗100,′authcode′=>content[‘auth_code’],
‘spbill_create_ip’ => Request::instance()->ip()?:‘127.0.0.1’,//终端的ip
);
$unified['sign'] = self::getSign($unified, $config['key']);//签名$url = 'https://api.mch./pay/micropay';$responseXml = Http::postRequest($url, Http::arrayToXml($unified));$unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);Log::write(json_encode($unifiedOrder,JSON_UNESCAPED_UNICODE),'log');//支付成功if ($unifiedOrder->return_code == 'SUCCESS' && $unifiedOrder->result_code == 'SUCCESS') {return ['order_trade_no'=>$unifiedOrder->transaction_id,'total_amount'=>$unifiedOrder->cash_fee,'order_pay_time'=>strtotime($unifiedOrder->time_end),'order_channel_id'=>1002];}else if ($unifiedOrder->err_code == 'USERPAYING') {$get_res = self::getWxOrderPay($sub_mch_id,$content['order_num'],0);Log::write(json_encode($get_res,JSON_UNESCAPED_UNICODE),'log');if($get_res->trade_state == 'SUCCESS'){return ['order_trade_no'=>$unifiedOrder->transaction_id,'total_amount'=>$unifiedOrder->cash_fee,'order_pay_time'=>strtotime($unifiedOrder->time_end),'order_channel_id'=>1002];}}return [];}//随机32位字符串public static function nonce_str(){$result = '';$str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';for ($i=0;$i<32;$i++){$result .= $str[rand(0,48)];}return $result;}/*** 获取签名*/public static function getSign($params, $key){ksort($params, SORT_STRING);$unSignParaString = self::formatQueryParaMap($params, false);$signStr = strtoupper(md5($unSignParaString . "&key=" . $key));return $signStr;}protected static function formatQueryParaMap($paraMap, $urlEncode = false){$buff = "";ksort($paraMap);foreach ($paraMap as $k => $v) {if (null != $v && "null" != $v) {if ($urlEncode) {$v = urlencode($v);}$buff .= $k . "=" . $v . "&";}}$reqPar = '';if (strlen($buff) > 0) {$reqPar = substr($buff, 0, strlen($buff) - 1);}return $reqPar;}/*** 订单查询* @author wzs* @param $sub_mch_id* @param $order_num* @param $second* @return \SimpleXMLElement*/protected static function getWxOrderPay($sub_mch_id,$order_num,$second){$config = array('appid' => config('wxpay_app_id'),'mch_id' => config('wxpay_mch_id'),'key' => config('wxpay_api_secret'),);$unified = array('appid' => $config['appid'],'sub_mch_id'=>$sub_mch_id,'mch_id' => $config['mch_id'],'nonce_str' => self::nonce_str(),//随机字符串'out_trade_no' => $order_num,);$unified['sign'] = self::getSign($unified, $config['key']);//签名$url = 'https://api.mch./pay/orderquery';$responseXml = Http::postRequest($url, Http::arrayToXml($unified));$unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);if($unifiedOrder->trade_state == 'USERPAYING' && $second <= 30){sleep(3);$second += 3;$unifiedOrder = self::getWxOrderPay($sub_mch_id,$order_num,$second);}return $unifiedOrder;}
}
如果觉得《支付宝/微信扫码支付》对你有帮助,请点赞、收藏,并留下你的观点哦!