123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993 |
- <?php
- namespace App\Http\Controllers;
- use App\Imports\ExpressImport;
- use App\Models\Order;
- use App\Models\Region;
- use App\Models\Store;
- use App\Models\User;
- use App\Models\Bank;
- use App\Models\Bank123;
- use Excel;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Cache;
- class MiniSubController extends Controller
- {
- //申请小微商户
- public function apply_wx()
- {
- $url = "https://api.mch.weixin.qq.com/v3/ecommerce/applyments/";//这个是微信的申请地址
- $param = array(
- "out_request_no" => "20200708093101909876",//申请编号,用户自定义就可以
- "organization_type" => "2401",//申请类型 2401代表申请的是小微商户(就是个人,没有营业执照的人),其它类型可参考微信v3文档
- "need_account_info" => false,//是否填写结算银行账户,提现会提到这个账户上,设置true需要添加银行卡信息等。这里我就设置false了。因为绑定银行卡可以通过微信中的小程序“商家助手”自己去绑定。也可以通过微信的绑卡接口在写一个绑定银行卡的功能。
- "account_info" => [
- "bank_account_type" => "74",
- "account_name" => "",//开户名称
- "account_bank" => "工商银行",//开户银行
- "bank_address_code" => "110000",//开户银行省市编码
- "bank_branch_id" => "402713354941",
- "bank_name" => "",//开户银行全称(含支行)
- "account_number" => "",//银行账户
- ],
- "contact_info" => array(//填写一个店铺的管理员信息
- "contact_type" => "65",//管理员类型 65是经营者/法人 其它类型看微信文档
- "contact_name" => $this->getEncrypts("张奇"),//管理员姓名getEncrypts是加密字符串的方法,见下文。
- "mobile_phone" => $this->getEncrypts("15236877164"),//管理员电话getEncrypts是加密字符串的方法
- "contact_id_card_number" => $this->getEncrypts("412326199612220311"),//管理员身份证号码
- // "contact_email" =>$this->getEncrypts("邮箱@.com"),//管理员的邮箱地址
- ),
- "sales_scene_info" => array(//店铺信息
- "store_name" => "大卫博士微商店",//店铺全称
- ),
- "merchant_shortname" => "大卫博士",//商户简称
- "id_card_info" => array(//经营者/法人的身份证信息
- 'id_card_copy' => '9DNPQr2pl-BoHpWimJBY76nt_fEehBjGAT6PTijhvHRMfWN8COZtSb_LTgU8159D04a-4QzYCGPl0urT8jl3-YfZ-W_3BFOVBEKXe1TCb6Y', // 身份证人像面照片
- 'id_card_national' => '9DNPQr2pl-BoHpWimJBY7xVvut4fNtB2n4GVjEgEvr_DsJL43easTmbP6Ofp4Wu0O2fzm1qWstPBIZ57QDwa69oHp_Gnc2uivCZegMtFC5U', // 身份证国徽面照片
- "id_card_name" => $this->getEncrypts('张奇'),//身份证上的姓名 getEncrypts()字符串加密方法 见下文
- "id_card_number" => $this->getEncrypts("412326199612220311"),//身份证号,此身份证号可以和管理员是一个人
- "id_card_valid_time" => "2027-09-07" //身份证国徽面的到期日期
- )
- );
- $merchant_id = "1604585534"; //服务商的商户号
- $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//商户证书的序列号,可在微信商户平台API安全 中获取
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//获取随机字符串
- $sign = $this->sign($url, 'POST', $timestamp, $nonce, json_encode($param), $mch_private_key, $merchant_id, $serial_no);//进行签名操作
- $header = [//设置发送的头信息
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,//签名
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,//服务商的商户号
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书的序列号,注意:是平台证书,不是上文的商户证书序列号
- ];
- $result = $this->curl($url, json_encode($param), $header);//curl方法见下文
- $result = json_decode($result, true);
- return $result;
- var_dump($result);
- // $result['applyment_id'] 返回了 说明申请成功了
- }
- //查询进件状态
- public function query()
- {
- $out_request_no = '20200708093101909876';//申请编号
- $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/applyments/out-request-no/' . $out_request_no;//查询地址
- $merchant_id = "1604585534";//商户号
- $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//随机字符串
- $body = "";
- $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
- $header = [
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result = $this->curl($url, '', $header, 'GET');
- $result = json_decode($result, true);
- var_dump($result);
- $result['applyment_state'] == "FINISH"; //完成签约 其它状态:CHECKING:资料校验中 ACCOUNT_NEED_VERIFY:待账户验 AUDITING:审核中 REJECTED:已驳回 NEED_SIGN:待签约 FINISH:完成 FROZEN:已冻结
- }
- //加密敏感字符串
- public function getEncrypts($str = "张三")
- {
- // $public_key_path = 'D:\phpstudy_pro\WWW\weidian-api/cert/cert.pem';//获取的文件临时保存到服务器
- $public_key_path='/www/wwwroot/weidian.jiuweiyun.cn/weidian-online-api/cert/cert.pem';//这是获取平台证书临时存起来的平台公钥,通过getzhengshu()方法获取,见下文。。。注意是平台证书,不是商户证书
- $public_key = file_get_contents($public_key_path);
- $encrypted = '';
- if (openssl_public_encrypt($str, $encrypted, $public_key, OPENSSL_PKCS1_OAEP_PADDING)) {
- //base64编码
- $sign = base64_encode($encrypted);
- } else {
- throw new Exception('encrypt failed');
- }
- return $sign;
- // if(openssl_public_encrypt($str,$encrypted,$public_key,OPENSSL_PKCS1_OAEP_PADDING)){
- // $sign=base64_encode($encrypted);
- // }else{
- // echo "error";exit;
- // }
- // return $sign;//返回加密的敏感字符串
- }
- //上传证件图片
- public function uploadmedia($path = "uploads/header/z.jpg")
- {
- $url = "https://api.mch.weixin.qq.com/v3/merchant/media/upload";//微信的上传地址
- $merchant_id = "1604585534";//商户号
- $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//商户证书序列号,注意是商户证书,不是平台证书序列号
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//随机字符串
- $fi = new \finfo(FILEINFO_MIME_TYPE);
- $mime_type = $fi->file($path);
- $meta = [
- 'filename' => pathinfo($path, 2),
- 'sha256' => hash_file('sha256', $path)
- ];
- $sign = $this->sign($url, 'POST', $timestamp, $nonce, json_encode($meta), $mch_private_key, $merchant_id, $serial_no);
- $boundary = uniqid();
- $header = [
- 'Content-Type:multipart/form-data;boundary=' . $boundary,
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id
- ];
- $body = '--' . $boundary . "\r\n";
- $body .= 'Content-Disposition:form-data; name="meta"' . "\r\n";
- $body .= 'Content-Type:application/json' . "\r\n\r\n";
- $body .= json_encode($meta) . "\r\n";
- $body .= '--' . $boundary . "\r\n";
- $body .= 'Content-Disposition:form-data;name="file";filename="' . $meta['filename'] . '"' . "\r\n";
- $body .= 'Content-Type:' . $mime_type . "\r\n\r\n";
- $body .= file_get_contents($path) . "\r\n";
- $body .= '--' . $boundary . '--' . "\r\n";
- $result = $this->curl($url, $body, $header);
- $result = json_decode($result, true);
- return $result['media_id'];//返回微信返回来的图片标识
- }
- //读取商户api证书公钥
- public function getPublicKey()
- {
- return openssl_get_privatekey(file_get_contents(config('wechat.payment.default.key_path')));//商户平台API安全证书中下载,保存到服务器
- }
- //生成随机字符串
- public function nonce_str($length = 32)
- {
- $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
- $str = "";
- for ($i = 0; $i < $length; $i++) {
- $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
- }
- return $str;
- }
- //签名
- public function sign($url, $http_method, $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no)
- {
- $url_parts = parse_url($url);
- $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
- $message =
- $http_method . "\n" .
- $canonical_url . "\n" .
- $timestamp . "\n" .
- $nonce . "\n" .
- $body . "\n";
- openssl_sign($message, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');
- $sign = base64_encode($raw_sign);
- $schema = 'WECHATPAY2-SHA256-RSA2048';
- $token = sprintf(
- 'mchid="%s",nonce_str="%s",signature="%s",timestamp="%d",serial_no="%s"',
- $merchant_id,
- $nonce,
- $sign,
- $timestamp,
- $serial_no
- );
- return $token;
- }
- //curl提交
- public function curl($url, $data = [], $header, $method = 'POST')
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- curl_setopt($curl, CURLOPT_HEADER, false);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- if ($method == "POST") {
- curl_setopt($curl, CURLOPT_POST, TRUE);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- }
- $result = curl_exec($curl);
- curl_close($curl);
- return $result;
- }
- //获取平台证书
- public function getzhengshu()
- {
- $url = "https://api.mch.weixin.qq.com/v3/certificates";//获取地址
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//获取一个随机字符串
- $body = "";
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥
- $merchant_id = config('wechat.payment.default.mch_id');//商户号
- $serial_no = '696B623841134292AAB48D4EC5907FB376504E98';//商户证书序列号
- $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);
- $header = [
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id
- ];
- $result = $this->curl($url, '', $header, 'GET');
- $result = json_decode($result, true);
- // Log::error($result);
- $serial_no = $result['data'][0]['serial_no'];//获取的平台证书序列号
- $encrypt_certificate = $result['data'][0]['encrypt_certificate'];
- $sign_key = config('wechat.payment.default.api_v3'); //APIv3密钥,商户平台API安全中获取
- $result = $this->decryptToString($encrypt_certificate['associated_data'], $encrypt_certificate['nonce'], $encrypt_certificate['ciphertext'], $sign_key);
- file_put_contents('/www/wwwroot/weidian.jiuweiyun.cn/weidian-online-api/cert/cert.pem', $result);//获取的文件临时保存到服务器
- return $serial_no;//返回平台证书序列号
- }
- //解密返回的信息
- public function decryptToString($associatedData, $nonceStr, $ciphertext, $aesKey)
- {
- if (strlen($aesKey) != 32) {
- throw new InvalidArgumentException('无效的ApiV3Key,长度应为32个字节');
- }
- // $this->aesKey = $aesKey;
- $ciphertext = \base64_decode($ciphertext);
- if (strlen($ciphertext) <= 16) {
- return false;
- }
- if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') &&
- \sodium_crypto_aead_aes256gcm_is_available()) {
- return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
- return '1111';
- }
- // ext-libsodium (need install libsodium-php 1.x via pecl)
- if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') &&
- \Sodium\crypto_aead_aes256gcm_is_available()) {
- return '222';
- return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
- }
- // if(function_exists('\sodium_crypto_aead_aes256gcm_is_available')&& \sodium_crypto_aead_aes256gcm_is_available()){
- // return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext,$associatedData,$nonceStr,$aesKey);
- // }
- if (PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())) {
- $ctext = substr($ciphertext, 0, -16);
- $authTag = substr($ciphertext, -16);
- return \openssl_decrypt(
- $ctext,
- 'aes-256-gcm',
- $aesKey,
- \OPENSSL_RAW_DATA,
- $nonceStr,
- $authTag,
- $associatedData
- );
- }
- throw new \RuntimeException('php7.1');
- }
- //完结分账信息
- public function finish(Request $request)
- {
- $order_no = $request->input('order_no');
- $order = Order::where('order_no', $order_no)->first();
- $user = User::find(Auth::user()->id);
- // $sub_mchid=$request->input('sub_mchid');
- $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/profitsharing/finish-order';//地址
- $merchant_id = config('wechat.payment.default.mch_id');//商户号
- $serial_no = config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//随机字符串
- $data = [
- 'sub_mchid' => Store::where('id', $user->store_id)->value('sub_mchid'),
- 'transaction_id' => $order->wechat_pay_no,//微信支付单号
- 'out_order_no' => 'finish' . '_' . $order->order_no,//订单号
- 'description' => '分账完结',
- ];
- $sign = $this->sign($url, 'POST', $timestamp, $nonce, json_encode($data), $mch_private_key, $merchant_id, $serial_no);//签名
- $header = [
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result = $this->curl($url, json_encode($data), $header);
- $result = json_decode($result, true);
- return $this->success($result);
- }
- //查询分账
- public function fenzhang()
- {
- $sub_mchid = '1604640016';
- $transaction_id = '4200000902202104094861688627';
- $out_order_no = 'WJ21040906580535143167060';
- $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/profitsharing/orders?sub_mchid=' . $sub_mchid . '&transaction_id=' . $transaction_id . '&out_order_no=' . $out_order_no;//查询地址
- $merchant_id = "1604585534";//商户号
- $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//随机字符串
- $body = "";
- $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
- $header = [
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result = $this->curl($url, '', $header, 'GET');
- $result = json_decode($result, true);
- return $this->success($result);
- // var_dump($result);exit;
- }
- public function fenzhangyue()
- {
- // $sub_mchid='1604640016';
- $transaction_id = '4200000838202012077922886767';//'4200000753202012077136468639';
- // $out_order_no='finish_DD20120716581049363773415';
- $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/profitsharing/orders/' . $transaction_id . '/amounts';//查询地址
- $merchant_id = "1604585534";//商户号
- $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//随机字符串
- $body = "";
- $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
- $header = [
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result = $this->curl($url, '', $header, 'GET');
- $result = json_decode($result, true);
- return $this->success($result);
- // var_dump($result);exit;
- }
- //查询实时余额
- public function shishiyue(Request $request)
- {
- $sub_mchid = $request->input('sub_mchid');//'1604640016';
- // $transaction_id='4200000838202012077922886767';//'4200000753202012077136468639';
- // $out_order_no='finish_DD20120716581049363773415';
- $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/fund/balance/' . $sub_mchid;//查询地址
- $merchant_id = "1604585534";//商户号
- $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//随机字符串
- $body = "";
- $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
- $header = [
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result = $this->curl($url, '', $header, 'GET');
- $result = json_decode($result, true);
- return $this->success($result);
- // var_dump($result);exit;https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw
- }
- //二级商户提现余额
- public function tixianyue(Request $request)
- {
- $sub_mchid = $request->input('sub_mchid');
- $amount = $request->input('amount');
- $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw';//查询地址
- $merchant_id = "1604585534";//商户号
- $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//随机字符串
- $data = [
- 'sub_mchid' => $sub_mchid,
- 'out_request_no' => 'TX' . date('ymdHis') . substr(microtime(), 2, 6) . mt_rand(10000, 99999),
- 'amount' => (float)$amount,
- 'remark' => '二级商户提现',
- 'bank_memo' => '微信二级商户提现'
- ];
- $sign = $this->sign($url, 'POST', $timestamp, $nonce, json_encode($data), $mch_private_key, $merchant_id, $serial_no);//签名
- $header = [
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result = $this->curl($url, json_encode($data), $header, 'POST');
- $result = json_decode($result, true);
- return $this->success($result);
- }
- //提现状态查询
- public function tixianchaxun()
- {
- $withdraw_id = '209000130152900202106022213780029';
- $sub_mchid = '1609479387';
- // $transaction_id='4200000838202012077922886767';//'4200000753202012077136468639';
- $out_order_no='TX21060220551298361934197';
- $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/out-request-no/' . $out_order_no.'?sub_mchid='.$sub_mchid;//查询地址
- // $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/' . $withdraw_id.'?sub_mchid='.$sub_mchid;//查询地址
- $merchant_id = "1603992271";//商户号
- $serial_no = "696B623841134292AAB48D4EC5907FB376504E98";//不是平台证书序列号
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//随机字符串
- $body = "";
- // $data = [
- // 'sub_mchid' => $sub_mchid,
- // ];
- $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
- $header = [
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result = $this->curl($url, '', $header, 'GET');
- // dd($result);
- $result = json_decode($result, true);
- return $this->success($result);
- // var_dump($result);exit;https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw
- }
- //退款接口
- public function tuikuan()
- {
- $sub_mchid = '1604640016';
- $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/refunds/apply';//查询地址
- $merchant_id = "1604585534";//商户号
- $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//随机字符串
- $data = [
- 'sub_mchid' => $sub_mchid,
- 'sp_appid' => config('wechat.payment.default.app_id'),
- 'out_trade_no' => 'DD20120716580942358726226',//'DD20120119281312383728170',
- 'out_refund_no' => 'TK' . date('ymdHis') . substr(microtime(), 2, 6) . mt_rand(10000, 99999),
- 'amount' => ["refund" => 1, "total" => 1, "currency" => "CNY"],
- 'notify_url' => 'http://weidian.jiuweiyun.cn/api/mini/refund_notify'
- ];
- // return $data;
- $sign = $this->sign($url, 'POST', $timestamp, $nonce, json_encode($data), $mch_private_key, $merchant_id, $serial_no);//签名
- $header = [
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result = $this->curl($url, json_encode($data), $header, 'POST');
- $result = json_decode($result, true);
- return $this->success($result);
- }
- //退款通知
- public function refundNotify()
- {
- Log::info('退款通知123');
- defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..'));
- //接收微信返回的数据数据,返回的xml格式
- $xmlData = file_get_contents('php://input');
- //将xml格式转换为数组
- Log::info($xmlData);
- $xmlData = json_decode($xmlData, true);
- $data = $xmlData['resource'];
- Log::info($data['associated_data']);
- $sign_key = config('wechat.payment.default.api_v3'); //APIv3密钥,商户平台API安全中获取
- $result = $this->decryptToString($data['associated_data'], $data['nonce'], $data['ciphertext'], $sign_key);
- $result = json_decode($result, true);
- log::info($result['refund_status']);
- if ($result['refund_status'] == 'SUCCESS') {
- return true;
- }
- Log::info($result);
- return $this->success([], 'SUCCESS');
- //为了防止假数据,验证签名是否和返回的一样。
- //记录一下,返回回来的签名,生成签名的时候,必须剔除sign字段。
- // $sign = $data['sign'];
- // unset($data['sign']);
- // if($sign == self::getSign($data,config('wechat.payment.default.app_v3'))){
- // //签名验证成功后,判断返回微信返回的
- // if ($data['result_code'] == 'SUCCESS') {
- // //根据返回的订单号做业务逻辑
- // Log::info(json_encode($data));
- // }else{//支付失败,输出错误信息
- // fwrite($file,date("Y-m-d H:i:s")."错误信息:支付失败,".json_encode($data).PHP_EOL);
- // }
- // }else{
- // fwrite($file,date("Y-m-d H:i:s")."错误信息:签名验证失败,".json_encode($data).PHP_EOL);
- // }
- }
- public static function FromXml($xml)
- {
- if (!$xml) {
- echo "xml数据异常!";
- }
- //将XML转为array
- //禁止引用外部xml实体
- libxml_disable_entity_loader(true);
- $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
- return $data;
- }
- //生成签名
- public static function getSign($params, $api_key)
- {
- ksort($params); //将参数数组按照参数名ASCII码从小到大排序
- foreach ($params as $key => $item) {
- if (!empty($item)) { //剔除参数值为空的参数
- $newArr[] = $key . '=' . $item; // 整合新的参数数组
- }
- }
- $stringA = implode("&", $newArr); //使用 & 符号连接参数
- $stringSignTemp = $stringA . "&key=" . $api_key; //拼接key
- // key是在商户平台API安全里自己设置的
- $stringSignTemp = MD5($stringSignTemp); //将字符串进行MD5加密
- $sign = strtoupper($stringSignTemp); //将所有字符转换为大写
- return $sign;
- }
- //提现状态查询
- public function tuikuanchaxun()
- {
- $refund_id ='50301107102021012605823556180';// '50300706442020120904591342059';
- $sub_mchid ='1604839213';// '1604640016';
- $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/refunds/id/' . $refund_id . '?sub_mchid=' . $sub_mchid;//查询地址
- $merchant_id = "1604585534";//商户号
- $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//随机字符串
- $body = "";
- $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
- $header = [
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result = $this->curl($url, '', $header, 'GET');
- $result = json_decode($result, true);
- return $this->success($result);
- }
- //企业付款到用户零钱
- public function companyPayCash(){
- // $sub_mchid = '1604640016';
- $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';//查询地址
- $merchant_id = "1604585534";//商户号
- $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//随机字符串
- $data = [
- 'mchid' => $merchant_id,
- 'mch_appid' => config('wechat.payment.default.app_id'),
- 'partner_trade_no' => 'CP' . date('ymdHis') . substr(microtime(), 2, 6) . mt_rand(10000, 99999),
- 'openid' => 'oJn4Fv3GoK8rMqftML5kSU8FoDUc',
- 'check_name' => 'NO_CHECK',
- 'amount' => 1,
- 'desc' => '退款',
- 'nonce_str' =>$nonce,
- ];
- $sign = $this->sign($url, 'POST', $timestamp, $nonce, json_encode($data), $mch_private_key, $merchant_id, $serial_no);//签名
- return $sign['signature'];
- $data['sign']=$sign->signature;
- $data=$this->arraytoxml($data);
- return $data;
- $header = [
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result = $this->curl($url, json_encode($data), $header, 'POST');
- return $result;
- Log::info($result);
- $result = json_decode($result, true);
- return $this->success($result);
- }
- function arraytoxml($data){
- $str='<xml>';
- foreach($data as $k=>$v) {
- $str.='<'.$k.'>'.$v.'</'.$k.'>';
- }
- $str.='</xml>';
- return $str;
- }
- //获取资金账单下载url
- public function zijinzhangdan()
- {
- $bill_date = '2020-12-09';
- $account_type = 'BASIC';
- $tar_type = 'GZIP';
- $sub_mchid = '1604640016';
- $url = 'https://api.mch.weixin.qq.com/v3/bill/fundflowbill?bill_date=' . $bill_date . '&account_type=' . $account_type . '&tar_type=' . $tar_type;//查询地址
- $merchant_id = "1604585534";//商户号
- $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//随机字符串
- $body = "";
- $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
- $header = [
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result = $this->curl($url, '', $header, 'GET');
- $result = json_decode($result, true);
- return $this->success($result);
- }
- //获取交易账单下载url
- public function jiaoyizhangdan()
- {
- $bill_date = '2020-12-09';
- $account_type = 'ALL';
- $tar_type = 'GZIP';
- // $sub_mchid='1604640016';
- $sub_mchid = '';//'&sub_mchid='.$sub_mchid.
- $url = 'https://api.mch.weixin.qq.com/v3/bill/tradebill?bill_date=' . $bill_date . '&account_type=' . $account_type . '&tar_type=' . $tar_type;//查询地址
- $merchant_id = "1604585534";//商户号
- $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//随机字符串
- $body = "";
- $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
- $header = [
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result = $this->curl($url, '', $header, 'GET');
- $result = json_decode($result, true);
- return $this->success($result);
- }
- //下载账单
- public function downzhangdan()
- {
- $url = 'https://api.mch.weixin.qq.com/v3/billdownload/file?token=rpsaygCj-6NgWW-evyqxZ-yj-AE6cfd9KKctbebu80VBuBQL-I1x0ioq6lQ4pwSC&tartype=gzip';//查询地址
- $merchant_id = "1604585534";//商户号
- $serial_no = "73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//不是平台证书序列号
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//随机字符串
- $body = "";
- $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
- $header = [
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result = $this->curl($url, '', $header, 'GET');
- return $result;
- $result = json_decode($result, true);
- return $this->success($result);
- }
- //导入
- public function getImport(Request $request)
- {
- ini_set('memory_limit', '1024M');
- Cache::put('page_city', '123',-1);
- $page = Cache::get('page_city') ?? 0;
- $size = 300;
- $num = $page * $size;
- $bank123 = Bank123::orderBy('sub_branch_id')->skip($num)->take($size)->get();
- $region=Region::where('level',2)->get();
- foreach ($bank123 as $key => $val) {
- foreach($region as $k=>$v){
- if(strpos($val->sub_branch_name,$v->sname)){
- Log::info( $page.'//'.$val->sub_branch_name . '//' . $v->sname);
- $name=explode(',',$v->mername);
- Bank123::where('sub_branch_id',$val->sub_branch_id)->update(['city'=>$v->name,'province'=>$name[1]]);
- }
- }
- }
- $page++;
- Cache::put('page_city', $page);
- return '123';
- // Cache::put('page', '123',-1);
- // return $page = Cache::get('page');
- $page = Cache::get('page') ?? 0;
- $size = 300;
- $num = $page * $size;
- // $banks = Bank::where('bank_name', '工商银行')->get();
- $bank123 = Bank123::orderBy('sub_branch_id')->skip($num)->take($size)->get();
- foreach ($bank123 as $key => $val) {
- Bank123::where('sub_branch_id',$val->sub_branch_id)->update(['bank'=>mb_substr($val->sub_branch_name,0,6)]);
- }
- $page++;
- Cache::put('page', $page);
- return $page . '//' . $size;
- return 1;
- $name = '上海市';
- $city_id = '3101';
- $province_id = '31';
- // $row=Bank::where('province',$name)->update(['city_id'=>$city_id,'province_id'=>$province_id,'city'=>$name]);
- $row = Bank::where('city', $name)->update(['city_id' => $city_id, 'province_id' => $province_id]);
- return $row;
- $json = file_get_contents('C:\Users\001\Desktop\123123.json');
- // return $json[0];
- return json_decode($json, true);
- return 1;
- $page = Cache::get('page') ?? 17;
- $page++;
- Cache::put('page', $page);
- $size = 200;
- $num = $page * $size;
- $banks = Bank::where('bank_name', '工商银行')->get();
- $bank123 = Bank123::skip($num)->take($size)->get();
- foreach ($banks as $key => $val) {
- foreach ($bank123 as $k => $v) {
- if ($val->sub_branch_name == $v->sub_branch_name) {
- Bank::where('bank_name', '工商银行')->where('sub_branch_name', $v->sub_branch_name)->update(['sub_branch_id' => $v->sub_branch_id]);
- }
- }
- }
- return $page . '//' . $size;
- $file = $request->file('file');
- if (!$file->isValid()) { //判断文件是否有效
- return $this->error('文件上传失败,请重新上传');
- }
- $array = Excel::toArray(new ExpressImport, request()->file('file'));
- $arr = $array[0];// array_slice($array[0],1);
- // return $arr;
- $data = [];
- $aa = [];
- foreach ($arr as $key => $val) {
- $data[$key]['sub_branch_name'] = $val[1];
- $data[$key]['sub_branch_id'] = $val[0];
- $aa[] = $val[0];
- }
- // return $aa;
- Bank::whereIn('sub_branch_id', $aa)->delete();
- Bank::insert($data);
- // Log::info($a);
- }
- //$apicode = "30422c9cbb1f4fed9f1c64b31197d7d1";
- //
- //$params = array(
- //"bankType" => "102",
- //"queryName" => "北京",
- //// "cbdocvo" => "",
- //
- //);
- public function getwww()
- {
- header('Content-type:text/html;charset=utf-8');
- //配置您申请的appkey
- $apicode = "30422c9cbb1f4fed9f1c64b31197d7d1";
- $url = "https://api.yonyoucloud.com/apis/dst/BankDotQuery/bankTypeQuery";
- $method = "GET";
- // $params = array();
- $params = array(
- "bankType" => "102",
- "queryName" => "北京",
- );
- $header = array();
- $header[] = "apicode:" . $apicode;
- $header[] = "content-type:application/json";
- $content = $this->linkcurl($url, $method, $params, $header);
- return $content;
- // return $this->success($content);
- $result = json_decode($content, true);
- // return json_decode($result['result'],true);
- // return json_decode($result,true);
- if ($result) {
- if ($result['error_code'] == '0') {
- print_r($result);
- } else {
- echo $result['error_code'] . ":" . $result['reason'];
- }
- } else {
- echo "请求失败";
- }
- }
- /**
- * 请求接口返回内容
- * @param string $url [请求的URL地址]
- * @param string $params [请求的参数]
- * @param int $ipost [是否采用POST形式]
- * @return string
- */
- function linkcurl($url, $method, $params = false, $header = false)
- {
- $httpInfo = array();
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_FAILONERROR, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- if (1 == strpos("$" . $url, "https://")) {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
- curl_setopt($ch, CURLOPT_TIMEOUT, 60);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- if ($method == "POST") {
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
- } else if ($params) {
- curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($params));
- }
- $response = curl_exec($ch);
- if ($response === FALSE) {
- //echo "cURL Error: " . curl_error($ch);
- return false;
- }
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
- curl_close($ch);
- return $response;
- }
- public function getBank()
- {
- header('Content-type:text/html;charset=utf-8');
- //配置您申请的appkey
- $apicode = "30422c9cbb1f4fed9f1c64b31197d7d1";
- // $url = "https://api.yonyoucloud.com/apis/u8c/uapbd/custdoc_insert";
- $url = "https://api.yonyoucloud.com/apis/dst/BankDotQuery/bankDotQuery";
- $method = "GET";
- $params = array(
- "bankType" => "102",
- "queryName" => "北京",
- );
- // $params = array(
- // "cbdocvo" => "",
- //
- // );
- $header = array();
- $header[] = "apicode:" . $apicode;
- $header[] = "content-type:application/json";
- $header[] = "trantype:code";
- $content = $this->wlinkcurl($url, $method, $params, $header);
- $result = json_decode($content, true);
- return $result;
- if ($result) {
- if ($result['error_code'] == '0') {
- print_r($result);
- } else {
- echo $result['error_code'] . ":" . $result['reason'];
- }
- } else {
- echo "请求失败";
- }
- }
- /**
- * 请求接口返回内容
- * @param string $url [请求的URL地址]
- * @param string $params [请求的参数]
- * @param int $ipost [是否采用POST形式]
- * @return string
- */
- public function wlinkcurl($url, $method, $params = false, $header = false)
- {
- $httpInfo = array();
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_FAILONERROR, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- if (1 == strpos("$" . $url, "https://")) {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
- curl_setopt($ch, CURLOPT_TIMEOUT, 60);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- if ($method == "POST") {
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
- } else if ($params) {
- curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($params));
- }
- $response = curl_exec($ch);
- if ($response === FALSE) {
- //echo "cURL Error: " . curl_error($ch);
- return false;
- }
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
- curl_close($ch);
- return $response;
- }
- }
|