| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <?php
- /**
- * 支付宝
- */
- namespace utils;
- use alipay\AopCertClient;
- use alipay\AopClient;
- use Alipay\EasySDK\Kernel\Factory;
- use Alipay\EasySDK\Kernel\Util\ResponseChecker;
- use Alipay\EasySDK\Kernel\Config;
- use alipay\request\AlipayFundTransToaccountTransferRequest;
- use alipay\request\AlipayTradeAppPayRequest;
- use think\Exception;
- use think\facade\Db;
- class AliPay
- {
- protected $conf;
- public $pay_v = 1; // 1 通用 2 Easy
- private static $appCertPath = ''; //应用公钥证书
- private static $alipayCertPath = '';//支付宝公钥证书
- private static $rootCertPath = '';//支付宝根证书
- private static $appId = 0; //APPID
- private static $rsaPrivateKey = ''; //支付宝应用私钥
- private static $gatewayUrl = 'https://openapi.alipay.com/gateway.do';//网关地址
- private static $apiVersion = '1.0';
- private static $signType = 'RSA2';
- private static $postCharset = 'utf-8';
- private static $format = 'json';
- // private static $notifyUrl = 'http://zy.suncorex.com:2080/api/aliResult';
- private static $notifyUrl = 'https://api.meikangjw.com/api/aliResult';
- private $class;
- public function __construct ($type = 'pay')
- {
- if ($type == 'pay') {
- self::payConfig();
- if ($this->pay_v == 1) {
- // self::payConfig();
- } else {
- Factory::setOptions(self::getOptions());
- }
- }
- }
- /**
- * 支付宝统一下单(通用版本sdk)
- * @param array $param
- * @return array
- */
- public function unifiedOrder (array $param)
- {
- try {
- $this->class = new AopCertClient();
- $this->class->gatewayUrl = self::$gatewayUrl;
- $this->class->appId = self::$appId;
- $this->class->rsaPrivateKey = self::$rsaPrivateKey;
- $this->class->format = self::$format;
- $this->class->postCharset = self::$postCharset;
- $this->class->signType = self::$signType;
- //调用getPublicKey从支付宝公钥证书中提取公钥
- $this->class->alipayrsaPublicKey = $this->class->getPublicKey(self::$alipayCertPath);
- //是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
- $this->class->isCheckAlipayPublicCert = true;
- //调用getCertSN获取证书序列号
- $this->class->appCertSN = $this->class->getCertSN(self::$appCertPath);
- //调用getRootCertSN获取支付宝根证书序列号
- $this->class->alipayRootCertSN = $this->class->getRootCertSN(self::$rootCertPath);
- //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify
- $request = new AlipayTradeAppPayRequest();
- //SDK已经封装掉了公共参数,这里只需要传入业务参数
- //此次只是参数展示,未进行字符串转义,实际情况下请转义
- $arrData = [
- 'total_amount' => $param['total_amount'],
- 'subject' => $param['body'],
- 'out_trade_no' => $param['out_trade_no'],
- ];
- // $request->setNotifyUrl(self::$notifyUrl);
- $request->setNotifyUrl(getWebUrl().'/api/aliResult');
- $request->setBizContent(json_encode($arrData, JSON_UNESCAPED_UNICODE));
- $result = $this->class->sdkExecute($request);
- if (!empty($result))
- return ['flag' => true, 'msg' => '调用成功', 'data' => ['result' => $result]];
- else
- return ['flag' => false, 'msg' => "统一下单失败"];
- } catch (\Exception $e) {
- // return ['flag' => false, 'msg' => "统一下单失败"];
- return ['flag' => false, 'msg' => $e->getMessage()];
- }
- }
- /**
- * 支付宝统一下单(Easy版本sdk)
- * @param array $param
- * @return array
- */
- public function easyUnifiedOrder (array $param)
- {
- try {
- $result = Factory::payment()->common()->create($param['body'], $param['out_trade_no'], $param['total_amount'], null);
- $responseChecker = new ResponseChecker();
- //3. 处理响应或异常
- if ($responseChecker->success($result)) {
- return ['flag' => true, 'msg' => '调用成功', 'data' => $result];
- } else {
- return ['flag' => false, 'msg' => "调用失败,原因:" . $result->msg . "," . $result->subMsg . PHP_EOL];
- }
- } catch (\Exception $e) {
- return ['flag' => false, 'msg' => "调用失败," . $e->getMessage() . PHP_EOL];
- }
- }
- /**
- * 提现
- * @param array $param
- * @return array
- * @throws \think\Exception
- */
- public function withdrawal (array $param)
- {
- $aop = new AopClient();
- $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
- $conf = Db::name('pay_config')->where('status', 1)->where('channel', 1)->field('public_key,app_id,private_key,app_cert_path,pay_root_cert,pay_cert_path,mchid')->find();
- sr_log(json_encode($conf));
- $aop->appId = $conf['app_id'];
- $aop->rsaPrivateKey = $conf['private_key']; // 应用私钥
- $aop->alipayrsaPublicKey = $conf['public_key']; // 支付宝公钥
- $aop->apiVersion = '1.0';
- $aop->signType = 'RSA2';
- $aop->postCharset = 'utf-8';
- $aop->format = 'json';
- $bizContent = [
- 'out_biz_no' => time() . rand(10000, 99999) . $param['uid'],
- 'trans_amount' => $param['practical_money'],
- 'product_code' => 'TRANS_ACCOUNT_NO_PWD',
- 'biz_scene' => 'DIRECT_TRANSFER',
- 'order_title' => '用户提现',
- 'payee_info' => [
- 'identity_type' => 'ALIPAY_LOGON_ID',
- 'identity' => $param['zfb_number'],
- 'name' => $param['zfb_name'],
- ],
- 'remark' => '用户提现'
- ];
- $request = new AlipayFundTransToaccountTransferRequest();
- $request->setBizContent(json_encode($bizContent, JSON_UNESCAPED_UNICODE));
- $result = $aop->execute($request);
- // sr_log('code'. json_encode($result));
- if (!$result) {
- return ['flag' => false, 'msg' => '系统出错'];
- }
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- $resultCode = $result->{$responseNode}->code;
- if (!empty($resultCode) && $resultCode == 10000) {
- return ['flag' => true, 'data' => ['out_biz_no' => $result->{$responseNode}->out_biz_no]];
- } else {
- // sr_log($request->{$responseNode}->sub_msg);
- return ['flag' => false, 'msg' => $request->{$responseNode}->sub_msg];
- }
- }
- /**
- * 验签
- * @param array $param
- * @return bool
- */
- public function checkSign (array $param)
- {
- try {
- if ($this->pay_v == 1) {
- $obj = new AopCertClient();
- $param['fund_bill_list'] = html_entity_decode($param['fund_bill_list']); // 处理数据中存在"
- return $obj->rsaCheckV1($param, self::$alipayCertPath, 'RSA2');
- } else {
- return Factory::payment()->common()->verifyNotify($param);
- }
- } catch (\Exception $e) {
- return false;
- }
- }
- /**
- * 公共参数
- * @return Config
- */
- static protected function getOptions ()
- {
- $options = new Config();
- $options->protocol = 'https';
- $options->gatewayHost = self::$gatewayUrl;
- $options->signType = self::$signType;
- $options->appId = self::$appId;
- $options->merchantPrivateKey = self::$rsaPrivateKey; // 应用私钥
- $options->alipayCertPath = self::$alipayCertPath; // 支付宝公钥证书文件路径
- $options->alipayRootCertPath = self::$rootCertPath; // 支付宝根证书文件路径
- $options->merchantCertPath = self::$appCertPath; // 应用公钥证书文件路径
- return $options;
- }
- /**
- * 支付宝配置
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- static protected function payConfig ()
- {
- $conf = Db::name('pay_config')->where('status', 1)->where('channel', 1)->field('app_id,private_key,app_cert_path,pay_root_cert,pay_cert_path,mchid')->find();
- if (empty($conf))
- throw new Exception('支付宝配置错误');
- self::$appCertPath = $conf['app_cert_path']; //应用公钥证书
- self::$alipayCertPath = $conf['pay_cert_path'];//支付宝公钥证书
- self::$rootCertPath = $conf['pay_root_cert'];//支付宝根证书
- self::$appId = $conf['app_id']; //APPID
- self::$rsaPrivateKey = $conf['private_key']; //支付宝应用私钥
- }
- }
|