├── .github └── FUNDING.yml ├── .gitignore ├── README.md ├── composer.json ├── demo.php └── src └── Payjs.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | custom: https://payjs.cn/sponsor/dajjxz 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | .idea 3 | composer.lock -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 |

PAYJS Wechat Payment Composer Package

5 |

6 | 7 | 8 | Latest Stable Version 9 | 10 | 11 | 12 | Total Downloads 13 | 14 | 15 | 16 | License 17 | 18 |

19 | 20 | ## 简介 21 | 本项目是基于 PAYJS 的 API 开发的 Composer Package,可直接用于生产环境 22 | 23 | PAYJS 针对个人主体提供微信支付接入能力,是经过检验的正规、安全、可靠的微信支付个人开发接口 24 | 25 | 其它版本: [PAYJS Laravel 开发包](https://github.com/xhat/payjs-laravel) 26 | 27 | ## 安装 28 | 29 | 通过 Composer 安装 30 | 31 | ```bash 32 | $ composer require xhat/payjs 33 | ``` 34 | 35 | ## 使用方法 36 | 37 | 首先在业务中引入 38 | 39 | ```php 40 | '12323412323', // 配置商户号 47 | 'key' => 'sadfsaddsaf', // 配置通信密钥 48 | ]; 49 | 50 | // 初始化 51 | $payjs = new Payjs($config); 52 | ``` 53 | 54 | 其次开始使用 55 | 56 | - 扫码支付 57 | 58 | ```php 59 | // 构造订单基础信息 60 | $data = [ 61 | 'body' => '订单测试', // 订单标题 62 | 'total_fee' => 2, // 订单金额 63 | 'out_trade_no' => time(), // 订单号 64 | 'attach' => 'test_order_attach', // 订单附加信息(可选参数) 65 | 'notify_url' => 'https://www.baidu.com', // 异步通知地址(可选参数) 66 | ]; 67 | 68 | $result = $payjs->native($data); 69 | print_r($result); 70 | ``` 71 | 72 | - 收银台模式支付(直接在微信浏览器打开) 73 | 74 | ```php 75 | // 构造订单基础信息 76 | $data = [ 77 | 'body' => '订单测试', // 订单标题 78 | 'total_fee' => 2, // 订单金额 79 | 'out_trade_no' => time(), // 订单号 80 | 'attach' => 'test_order_attach', // 订单附加信息(可选参数) 81 | 'notify_url' => 'https://www.baidu.com', // 异步通知地址(可选参数) 82 | 'callback_url' => 'https://www.baidu.com', // 支付后前端跳转地址(可选参数) 83 | ]; 84 | $url = $payjs->cashier($data); 85 | header('Location:'.$url); 86 | exit; 87 | ``` 88 | 89 | - JSAPI模式支付 90 | 91 | ```php 92 | // 构造订单基础信息 93 | $data = [ 94 | 'body' => '订单测试', // 订单标题 95 | 'total_fee' => 2, // 订单金额 96 | 'out_trade_no' => time(), // 订单号 97 | 'attach' => 'test_order_attach', // 订单附加信息(可选参数) 98 | 'notify_url' => 'https://www.baidu.com', // 异步通知地址(可选参数) 99 | 'openid' => 'xxxxxxxxxxxxx', // OPENID (必选参数) 100 | ]; 101 | 102 | $result = $payjs->jsapi($data); 103 | print_r($result); 104 | ``` 105 | - H5 模式支付 106 | 107 | ```php 108 | // 构造订单基础信息 109 | $data = [ 110 | 'body' => '订单测试', // 订单标题 111 | 'total_fee' => 2, // 订单金额 112 | 'out_trade_no' => time(), // 订单号 113 | 'attach' => 'test_order_attach', // 订单附加信息(可选参数) 114 | 'notify_url' => 'https://www.baidu.com', // 异步通知地址(可选参数) 115 | 'callback_url' => 'https://www.qq.com', // 前端跳转url(可选参数) 116 | ]; 117 | 118 | $result = $payjs->mweb($data); 119 | print_r($result); 120 | ``` 121 | 122 | - 查询订单 123 | 124 | ```php 125 | // 根据订单号查询订单状态 126 | $payjs_order_id = '********************'; 127 | $result = $payjs->check($payjs_order_id); 128 | print_r($result); 129 | ``` 130 | 131 | - 关闭订单 132 | 133 | ```php 134 | // 根据订单号关闭订单 135 | $payjs_order_id = '********************'; 136 | $result = $payjs->close($payjs_order_id); 137 | print_r($result); 138 | ``` 139 | 140 | - 退款 141 | 142 | ```php 143 | // 根据订单号退款 144 | $payjs_order_id = '*********************'; 145 | $result = $payjs->refund($payjs_order_id); 146 | print_r($result); 147 | ``` 148 | 149 | - 投诉订单获取 150 | 151 | ```php 152 | // 构造订单基础信息 153 | $data = [ 154 | 'mchid' => '123123', // 商户号 155 | ]; 156 | 157 | $result = $payjs->complaint($data); 158 | print_r($result); 159 | ``` 160 | 161 | - 获取商户资料 162 | 163 | 164 | ```php 165 | // 返回商户基础信息 166 | $result = $payjs->info(); 167 | print_r($result); 168 | ``` 169 | 170 | - 获取用户资料 171 | 172 | ```php 173 | // 根据订单信息中的 OPENID 查询用户资料 174 | $openid = '*******************'; 175 | $result = $payjs->user($openid); 176 | print_r($result); 177 | ``` 178 | 179 | - 查询银行名称 180 | 181 | ```php 182 | // 根据订单信息中的银行编码查询银行中文名称 183 | $bank = '*******************'; 184 | $result = $payjs->bank($bank); 185 | print_r($result); 186 | ``` 187 | 188 | - 接收异步通知 189 | 190 | ```php 191 | // 接收异步通知,无需关注验签动作,已自动处理 192 | $notify_info = $payjs->notify(); 193 | // 接收信息后自行处理 194 | ``` 195 | 196 | ## 更新日志 197 | Version 1.5.0 198 | 增加投诉API 199 | 增加H5支付API 200 | 201 | Version 1.4.3 202 | 主要去除了对 GuzzleHttp 包的依赖,同时去除了 Curl 60 的错误提示 203 | 修复jsapi的一处bug 204 | 205 | ## 安全相关 206 | 如果您在使用过程中发现各种 bug,请积极反馈,我会尽早修复 207 | 208 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "xhat/payjs", 3 | "description": "本项目是基于 PAYJS 的 API 开发的 Composer Package,可直接用于生产环境 https://payjs.cn", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "andy", 9 | "email": "andy@popfeng.com" 10 | } 11 | ], 12 | "autoload": { 13 | "psr-4": { 14 | "Xhat\\Payjs\\": "src/" 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /demo.php: -------------------------------------------------------------------------------- 1 | '', // 配置商户号 8 | 'key' => '', // 配置通信密钥 9 | ]; 10 | 11 | // 初始化 12 | $payjs = new Payjs($config); 13 | 14 | // 构造订单参数 15 | $data = [ 16 | 'total_fee' => 1, // 金额,单位 分 17 | 'body' => 'payjs_sdk_test_order', // 订单标题 18 | 'out_trade_no' => time(), // 商户订单号, 在用户侧请唯一 19 | ]; 20 | 21 | // 扫码支付 22 | $rst = $payjs->native($data); 23 | 24 | print_r($rst); 25 | -------------------------------------------------------------------------------- /src/Payjs.php: -------------------------------------------------------------------------------- 1 | mchid = $config['mchid']; 25 | $this->key = $config['key']; 26 | $api_url = isset($config['api_url']) ? $config['api_url'] : 'https://payjs.cn/api/'; 27 | 28 | $this->api_url_native = $api_url . 'native'; 29 | $this->api_url_cashier = $api_url . 'cashier'; 30 | $this->api_url_refund = $api_url . 'refund'; 31 | $this->api_url_close = $api_url . 'close'; 32 | $this->api_url_check = $api_url . 'check'; 33 | $this->api_url_user = $api_url . 'user'; 34 | $this->api_url_info = $api_url . 'info'; 35 | $this->api_url_bank = $api_url . 'bank'; 36 | $this->api_url_jsapi = $api_url . 'jsapi'; 37 | $this->api_url_complaint = $api_url . 'complaint'; 38 | $this->api_url_mweb = $api_url . 'mweb'; 39 | } 40 | 41 | // 扫码支付 42 | public function native(array $data) 43 | { 44 | $this->url = $this->api_url_native; 45 | return $this->post($data); 46 | } 47 | 48 | // JSAPI 模式 49 | public function jsapi(array $data) 50 | { 51 | $this->url = $this->api_url_jsapi; 52 | return $this->post($data); 53 | } 54 | 55 | // 收银台模式 56 | public function cashier(array $data) 57 | { 58 | $this->url = $this->api_url_cashier; 59 | $data = $this->sign($data); 60 | $url = $this->url . '?' . http_build_query($data); 61 | return $url; 62 | } 63 | 64 | // 投诉订单 65 | public function complaint(array $data) 66 | { 67 | $this->url = $this->api_url_complaint; 68 | return $this->post($data); 69 | } 70 | 71 | // MWEB(H5) 模式 72 | public function mweb(array $data) 73 | { 74 | $this->url = $this->api_url_mweb; 75 | return $this->post($data); 76 | } 77 | 78 | // 退款 79 | public function refund($payjs_order_id) 80 | { 81 | $this->url = $this->api_url_refund; 82 | $data = ['payjs_order_id' => $payjs_order_id]; 83 | return $this->post($data); 84 | } 85 | 86 | // 关闭订单 87 | public function close($payjs_order_id) 88 | { 89 | $this->url = $this->api_url_close; 90 | $data = ['payjs_order_id' => $payjs_order_id]; 91 | return $this->post($data); 92 | } 93 | 94 | // 检查订单 95 | public function check($payjs_order_id) 96 | { 97 | $this->url = $this->api_url_check; 98 | $data = ['payjs_order_id' => $payjs_order_id]; 99 | return $this->post($data); 100 | } 101 | 102 | // 用户资料 103 | public function user($openid) 104 | { 105 | $this->url = $this->api_url_user; 106 | $data = ['openid' => $openid]; 107 | return $this->post($data); 108 | } 109 | 110 | // 商户资料 111 | public function info() 112 | { 113 | $this->url = $this->api_url_info; 114 | $data = []; 115 | return $this->post($data); 116 | } 117 | 118 | // 银行资料 119 | public function bank($name) 120 | { 121 | $this->url = $this->api_url_bank; 122 | $data = ['bank' => $name]; 123 | return $this->post($data); 124 | } 125 | 126 | // 异步通知接收 127 | public function notify() 128 | { 129 | $data = $_POST; 130 | if ($this->checkSign($data) === true) { 131 | return $data; 132 | } else { 133 | return '验签失败'; 134 | } 135 | } 136 | 137 | // 数据签名 138 | public function sign(array $data) 139 | { 140 | $data['mchid'] = $this->mchid; 141 | $data = array_filter($data); 142 | ksort($data); 143 | $data['sign'] = strtoupper(md5(urldecode(http_build_query($data) . '&key=' . $this->key))); 144 | return $data; 145 | } 146 | 147 | // 校验数据签名 148 | public function checkSign($data) 149 | { 150 | $in_sign = $data['sign']; 151 | unset($data['sign']); 152 | $data = array_filter($data); 153 | ksort($data); 154 | $sign = strtoupper(md5(urldecode(http_build_query($data) . '&key=' . $this->key))); 155 | return $in_sign == $sign ? true : false; 156 | } 157 | 158 | // 数据发送 159 | public function post($data) 160 | { 161 | $data = $this->sign($data); 162 | 163 | $ch = curl_init(); 164 | curl_setopt($ch, CURLOPT_TIMEOUT, 10); 165 | curl_setopt($ch, CURLOPT_URL, $this->url); 166 | curl_setopt($ch, CURLOPT_USERAGENT, 'HTTP CLIENT'); 167 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 168 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 169 | curl_setopt($ch, CURLOPT_HEADER, FALSE); 170 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 171 | curl_setopt($ch, CURLOPT_POST, TRUE); 172 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 173 | 174 | $data = curl_exec($ch); 175 | curl_close($ch); 176 | return json_decode($data, true); 177 | } 178 | 179 | } 180 | --------------------------------------------------------------------------------