123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace app\utils;
- class PayUtil
- {
- private $message = '';
- private $data = [];
- private $config = [
- 'sub_appid' => 'wx29d6281c5252f504',
- 'huifu_id' => '6666000145960408',
- 'sys_id' => '6666000145960408',
- 'product_id' => 'YMFZS',
- 'rsaPrivateKey' => 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCWjQ+o6u8TgOQETBJs/OebvIZMLZ7J0kyAfyKXECl44R2EvfOWTNhtnDc6ZuaXegBoNo8u+skVxzy8k+unoBNghpct/BCaXc2op0F/3/3kc+Y5I74PX2lF//4MvCNP1ia+ykfHJkdnlfz2W8c2Tc1L311PPQ4dsB0c6mup/NjV1DX0WkAXWsMCHSss1iRLLb5mqVJtCkSbPmuSZV+bm+JASsD5goo32245HxqNbjKm/fi+OfZ9ONKl0wT+Ch5SQEbQAr61yhIyHdlqc7o7xupbYFKjT4ez8Y3VU4U1uKu57x2cKI0ETwsASALDqgvYS6mFjJFC3LcEjskyAAi1a+HNAgMBAAECggEABvS1iIRyT2BhKKGrKEX3HYb0XYiuEX0Z88Xr/zbL873S81MjcgpS5Z2WH6ipds44PphmvTDxIYR3Fe9vr+sBejA9w+4lhjxXSDeSGypqPKfuNy2jlmo+HvHY9xWqPAIVADOwZU8rdWnEqk1LQwMNPTgww3x6rnx4m+Fo4A3Cpv54vrK7f4fTUaaD8gHRFkSkkCvNJYEZdd5OLqCKwuAwL6vxwhX/tBuNa+zltw+9fV+FCnaBlkVncU8j8os7FBoT7MTbMoijDG78b1feUofuWUmn4A+lb/hH9R2zTNaTlGzTdyj/hybkPmz3TMPsymzIxpr239z8HZvVfnBiScFUAQKBgQDhj/mGKM7tkYuR+gRgBwlvtsPyru3DmaqabkweBz6nL9c7F5bVrLuoSZRkZTEfwU/lHDpRPqP6elLdv7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N9tuOR8ajW4ypv34vjn2fTjSpdME/goeUkBG0AK+tcoSMh3ZanO6O8bqW2BSo0+Hs/GN1VOFNbirue8dnCiNBE8LAEgCw6oL2EuphYyRQty3BI7JMgAItWvhzQIDAQAB',
- 'huiRsaPublicKey' => "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkA7CU0t1vIlnRC8vT2pD2CiSUb5KpVI2lOzHaNlxAQyFRfTfe4xAFw2XMxYvv4f2jgWlQwYTC13+m9aboQglzJtw7VXtw4G8I5DEDnYOiU2Z2vC/TaRFw3OLICk7H8WuWKN6Rn2iiVsauh+Jy31G9MOlUbFbIQjfw1J0NIbscbUsvCwUrc1DG3onM1aFTyFjTTmLYw7SjCkX7T4SKaBfP6Nqn87d8fkrcP6ruXHt4Zmf6xAFcTBuwpw9FRjzHn4bpxDatFleO9KFqHoBTDe0EGWHWhmShG7Mn1ubAGWJ/OUe9CLEOKVSnNYtR7MFlhoO+DmYlz5iSy7lYWmS6sTpGwIDAQAB",
- ];
- public function __construct()
- {
- }
- public function jsPay(string $openid, string $order_no, string $amount, string $desc = '', string $notify_url = '', string $type = 'T_MINIAPP')
- {
- $url = "https://api.huifu.com/v2/trade/payment/jspay";
- $post_data = [
- 'req_date' => date('Ymd'),
- 'req_seq_id' => $order_no,//请求流水号 示例值:rQ2021121311173944134649875651
- 'huifu_id' => $this->config['huifu_id'],//渠道与一级代理商的直属商户ID;示例值:6666000123123123
- 'goods_desc' => $desc,//商品描述
- //交易类型T_JSAPI: 微信公众号;T_MINIAPP: 微信小程序;A_JSAPI: 支付宝JS;A_NATIVE: 支付宝正扫;U_NATIVE: 银联正扫
- // U_JSAPI: 银联JS;D_NATIVE: 数字人民币正扫;T_H5:微信直连H5支付;T_APP:微信APP支付(只支持直连);T_NATIVE:微信正扫(只支持直连);示例值:A_NATIVE
- 'trade_type' => $type,
- 'trans_amt' => $amount,// 交易金额 单位元 最低传入0.01
- 'wx_data' => [
- 'sub_appid' => $this->config['sub_appid'],
- 'sub_openid' => $openid,
- ],
- 'notify_url' => $notify_url
- ];
- // ksort($post_data);
- $data = [
- 'sys_id' => $this->config['sys_id'],
- 'product_id' => $this->config['product_id'],
- 'sign' => $this->getSign($post_data),
- 'data' => $post_data
- ];
- $res = CurlUtil::postJson($url, $data);
- if (empty($res['data']['resp_code']) || $res['data']['resp_code'] != '00000100') {
- return $this->error($res['data']['resp_desc'] ?? '下单失败', $res);
- }
- return $this->success($res['data']['resp_desc'] ?? '下单成功!', $res);
- }
- private function getSign($post_data)
- {
- ksort($post_data);
- $post_data = json_encode($post_data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
- return $this->sha_with_rsa_sign($post_data, $this->config['rsaPrivateKey']);
- }
- public function checkSign($signature, $data)
- {
- return $this->verifySign($signature,$data,$this->config['huiRsaPublicKey']);
- }
- /**
- * 私钥加签(对数据源排序),可用于 V2 版本接口数据加签
- *
- * @param string $data 原数据( 排序后的json字符串; 数组参数排序后转json字符串(数据的中文和斜杠均不转码):ksort($post_data); json_encode($post_data, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE); )
- * @param string $rsaPrivateKey 私钥
- * @param int $alg 默认 OPENSSL_ALGO_SHA256
- * @return false|string 签名串
- */
- private function sha_with_rsa_sign(string $data, string $rsaPrivateKey, int $alg = OPENSSL_ALGO_SHA256)
- {
- $key = "-----BEGIN PRIVATE KEY-----\n" . wordwrap($rsaPrivateKey, 64, "\n", true) . "\n-----END PRIVATE KEY-----";
- $signature = '';
- try {
- openssl_sign($data, $signature, $key, $alg);
- return base64_encode($signature);
- } catch (\Exception $e) {
- $this->message = $e->getMessage();
- return false;
- }
- }
- /**
- * 使用公钥验签,可用于异步应答验签
- *
- * @param string $signature 签文
- * @param string $data 原数据(string)
- * @param string $rsaPublicKey 公钥
- * @param int $alg 默认 OPENSSL_ALGO_SHA256
- *
- * @return false|int 验证结果:成功/失败
- */
- public static function verifySign($signature, $data, $rsaPublicKey, $alg=OPENSSL_ALGO_SHA256){
- $key = "-----BEGIN PUBLIC KEY-----\n".wordwrap($rsaPublicKey, 64, "\n", true)."\n-----END PUBLIC KEY-----";
- return openssl_verify($data, base64_decode($signature), $key, $alg);
- }
- private function success($message = '', $data = [])
- {
- $this->message = $message;
- $this->data = $data;
- return true;
- }
- private function error($message = '', $data = [])
- {
- $this->message = $message;
- $this->data = $data;
- return false;
- }
- public function getMessage()
- {
- return $this->message;
- }
- public function getData()
- {
- return $this->data;
- }
- }
|