123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- declare (strict_types = 1);
- namespace library\utils;
- use WeChatPay\Builder;
- use WeChatPay\Formatter;
- use WeChatPay\Crypto\Rsa;
- use WeChatPay\Crypto\AesGcm;
- use WeChatPay\Util\PemUtil;
- use WeChatPay\Request\WeChatPayTradeOutTradeNoQueryRequest;
- class weixinPay{
- private $config;
- private $client;
- public $errorMsg="系统错误";
- private $merchantPrivateKeyInstance="";
-
- public function __construct($config = [])
- {
- if(empty($config)) $config = config('wxpay');
- $this->config = $config;
-
- $merchantPrivateKeyFilePath = $this->config["PrivateKey"];
- $merchantPrivateKeyInstance = Rsa::from($merchantPrivateKeyFilePath, Rsa::KEY_TYPE_PRIVATE);
- $merchantCertificateSerial = $this->config["merchantSerialNumber"];
-
- $platformCertificateFilePath = $this->config["Certificate"];
- $platformPublicKeyInstance = Rsa::from($platformCertificateFilePath, Rsa::KEY_TYPE_PUBLIC);
- $platformCertificateSerial = PemUtil::parseCertificateSerialNo($platformCertificateFilePath);
-
- $instance = Builder::factory([
- 'mchid' => $this->config["MCHID"],
- 'serial' => $merchantCertificateSerial,
- 'privateKey' => $merchantPrivateKeyInstance,
- 'certs' => [
- $platformCertificateSerial => $platformPublicKeyInstance,
- ],
- ]);
- $this->merchantPrivateKeyInstance = $merchantPrivateKeyInstance;
- $this->client = $instance;
- }
-
- public function wxmpPay($post=[]){
- $apiUrl = "v3/pay/transactions/jsapi";
- $jsonData = [];
-
- $jsonData["appid"] = $this->config["APPID"];
- $jsonData["mchid"] = $this->config["MCHID"];
- $jsonData["notify_url"] = $this->config["NOTIFY_URL"];
-
- $jsonData["description"] = $post["description"];
- $jsonData["out_trade_no"] = $post["out_trade_no"];
- $jsonData["amount"] = [
- "total"=> (int)(floatval($post["total"])*100),
- "currency" => 'CNY'
- ];
- $jsonData["scene_info"] = [
- "payer_client_ip"=>empty($post["payer_client_ip"])?"127.0.0.1":$post["payer_client_ip"],
- ];
- $jsonData["time_expire"] = date("Y-m-d\TH:i:s+08:00",time()+30*60);
- $jsonData["payer"]=[
- "openid"=>$post["openid"]
- ];
- $result = $this->clientHttp("POST", $apiUrl, $jsonData);
- if(empty($result)){
- if(empty($this->errorMsg)){
- $this->errorMsg = "支付错误001";
- }
- return false;
- }
- $resuleAr = json_decode($result,true);
- if(empty($resuleAr)){
- if(empty($this->errorMsg)){
- $this->errorMsg = "支付错误002";
- }
- return false;
- }
- if(empty($resuleAr["prepay_id"])){
- if(empty($this->errorMsg)){
- $this->errorMsg = "支付错误003";
- }
- return false;
- }
-
- $payInfo=array();
- $data=$this->makeSign(["appId"=>$this->config["APPID"],"prepay_id"=>$resuleAr["prepay_id"]]);
- $data["payData"] = $jsonData;
- return $data;
- }
-
- public function searchOrder($out_trade_no){
- $apiUrl = "v3/pay/transactions/out-trade-no/{out_trade_no}";
- $result = $this->clientHttp("GET", $apiUrl,[
- "out_trade_no"=>$out_trade_no,
- "query"=>["mchid"=>$this->config["MCHID"]],
- ]);
- return $result;
- }
-
- public function closeOrder($out_trade_no){
- $apiUrl = "v3/pay/transactions/out-trade-no/{out_trade_no}/close";
- $result = $this->clientHttp("GET", $apiUrl,[
- "out_trade_no"=>$out_trade_no,
- "query"=>["mchid"=>$this->config["MCHID"]],
- ]);
- return $result;
- }
-
- private function makeSign($info){
- $params = [
- 'appId' => $info["appId"],
- 'timeStamp' => (string)Formatter::timestamp(),
- 'nonceStr' => Formatter::nonce(),
- 'package' => 'prepay_id='.$info["prepay_id"],
- ];
- $params["paySign"] = Rsa::sign(Formatter::joinedByLineFeed(...array_values($params)),$this->merchantPrivateKeyInstance);
- $params["signType"] = 'RSA';
- return $params;
- }
-
- public function aesGcmDecrypt($data){
-
- $inBodyResource = AesGcm::decrypt($data["ciphertext"], $this->config["apiv3Key"], $data["nonce"], $data["associated_data"]);
-
- $inBodyResourceArray = (array)json_decode($inBodyResource, true);
- return $inBodyResourceArray;
- }
-
- private function clientHttp($type='POST',$url='',$json=[]){
- try {
- $resp=null;
- if($type=="POST"){
- $resp = $this->client->chain($url)->post(['json' => $json]);
- if(empty($json)){
- $resp = $this->client->chain($url)->post();
- }else{
- $resp = $this->client->chain($url)->post(['json' => $json]);
- }
- }
- if($type=="GET"){
- if(empty($json)){
- $resp = $this->client->chain($url)->get();
- }else{
- $resp = $this->client->chain($url)->get($json);
- }
- }
- if(empty($resp)){
- $this->errorMsg="提交方式错误";
- return false;
- }
- $statusCode = $resp->getStatusCode();
- if ($statusCode == 200) {
- return $resp->getBody()->getContents();
- } else if ($statusCode == 204) {
- $this->errorMsg = "处理成功,无返回Body";
- return false;
- }else{
- $this->errorMsg = "未知错误";
- return false;
- }
- } catch (\Exception $e) {
-
- $this->errorMsg = $e->getMessage();
- if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
- $r = $e->getResponse();
- $this->errorMsg=$r->getStatusCode()."".$r->getReasonPhrase().$r->getBody();
- }
- return false;
- }
- }
-
- }
|