<?php define("HTTP_TYPE", "http://"); $_index = daddslashes(isset($_GET['_index']) && (!empty($_GET['_index'])) ? trim($_GET['_index']) : ''); include_once RPC_DIR . '/module/common/common.php'; //引入公共文件 include_once RPC_DIR . '/conf/template_msg_alipay.php'; //引入模板消息 include_once RPC_DIR . '/module/mobile/mvc/alipay/sdk3.3/AopClient.php'; //引入支付宝客户端 include_once RPC_DIR.'/module/mobile/mvc/alipay/sdk3.3/request/AlipayUserInfoShareRequest.php'; //引入支付宝需要模板 include_once RPC_DIR . '/module/common/api_comm.php'; use common\api_comm; class alipay extends api_comm { /** * 获取支付宝userid之类的基础信息 * @param string $result * @param array $merchant * @return array * @throws */ function AliUserAuthLogin($result = '', $merchant = array()) { if (empty($result->alipay_system_oauth_token_response->user_id)) { return array("code" => 1, "msg" => "用户id获取失败!"); } $al_id = $result->alipay_system_oauth_token_response->user_id; //头像 self::Get_access_token($result, $merchant); $is_exist = $this->DB('slave1')->count(TABLE_USER_ALIPAY, "*", ["al_id" => $al_id]); if (!$is_exist) { //判断是否带参数 $res=$this->DB('master')->insert(TABLE_USER_ALIPAY, ["al_id" => $al_id, "addtime" => date("Y-m-d H:i:s")]); if ($res->rowCount()) { $_SESSION['al_id'] = $al_id; } else { return array("code" => 1, "msg" => "基础授权插入执行异常"); } } else { $this->DB('master')->update(TABLE_USER_ALIPAY, ["updatetime" => date("Y-m-d H:i:s")], ["al_id" => $al_id]); $_SESSION['al_id'] = $al_id; } return array("code" => 0, "msg" => "支付宝授权成功"); } /** * 获取access_token * @param string $result * @param array $_merchant * @return array * @throws Exception */ protected function Get_access_token($result = '', $_merchant = array()) { if (empty($_merchant)) { $_merchant = $this->GetMerchant(); } if (empty($_merchant)) { return array("code" => 1, "msg" => '错误的商户信息'); } //检查一下数据库中最access_token是否过期 $expires = $_merchant['expires']; $current = time(); if ($expires > $current) { //有效的 return array("code" => 0, "access_token" => $_merchant['access_token']); } else { //授权直接返回了access_token if (!empty($result->alipay_system_oauth_token_response->access_token)) { $expires_in = intval($result->alipay_system_oauth_token_response->expires_in); $re_expires_in = intval($result->alipay_system_oauth_token_response->re_expires_in); $this->DB('master')->update(TABLE_MERCHANT_ALIPAY, [ 'access_token' => $result->alipay_system_oauth_token_response->access_token, 'expires' => ($current + $expires_in - 3600), 'expires_in' => $expires_in, 'refresh_token' => $result->alipay_system_oauth_token_response->refresh_token, 're_expires' => ($current + $re_expires_in), 're_expires_in' => $re_expires_in ], [ 'id' => 1 ]); return array("code" => 0, "access_token" => $result->alipay_system_oauth_token_response->access_token); } elseif ($_merchant['re_expires'] > $current) { //还可以刷新 $_client = self::Alipay_client($_merchant); $_request = self::Alipay_request('AlipaySystemOauthTokenRequest'); $_request->setGrantType("refresh_token"); $_request->setRefreshToken($_merchant['refresh_token']); $result = $_client->execute($_request); if (!empty($result->alipay_system_oauth_token_response->access_token)) { $expires_in = intval($result->alipay_system_oauth_token_response->access_token); $re_expires_in = intval($result->alipay_system_oauth_token_response->re_expires_in); $this->DB('master')->update(TABLE_MERCHANT_ALIPAY, [ 'access_token' => $result->alipay_system_oauth_token_response->access_token, 'expires' => ($current + $expires_in - 3600), 'expire_in' => $expires_in, 'refresh_token' => $result->alipay_system_oauth_token_response->refresh_token, 're_expires' => ($current + $re_expires_in), 're_expires_in' => $re_expires_in ], [ 'id' => 1 ]); return array("code" => 0, "access_token" => $result->alipay_system_oauth_token_response->access_token); } else { return array("code" => 1, "msg" => 'access_token刷新请求错误'); } } return array("code" => 1, "msg" => '授权超时,请重新获取access_token'); } } /** * 重置授权码 * @return array * @throws Exception */ protected function Reload_Access_Token() { $this->DB('master')->update(TABLE_MERCHANT_ALIPAY, [ 'addtimestamp' => 0 ], [ 'id' => 1 ]); return $this->Get_access_token(); } /** * 发送模板消息 * @param $todata * @return array * @throws Exception */ function SentNotifyFromTemplate($todata) { $TokenData = $this->Get_access_token(); if ($TokenData['code'] == 1) { return array("code" => 1, "msg" => $TokenData['msg']); } $return = $this->SentTemplateMsg($todata, $TokenData['access_token']); if (isset($return['errcode']) && $return['errcode'] == 40001) { $this->Reload_Access_Token(); return array("code" => 1, "msg" => $TokenData['msg']); } return $return; } /** * 支付宝生活号默认配置 * @return array|bool|mixed */ public function GetMerchant($id=ALIPAY_APP_ID) { return $this->DB('slave1')->get(TABLE_MERCHANT_ALIPAY, '*', ['id' => $id]); } /** * 支付宝小程序默认配置 * @return array|bool|mixed */ public function GetMerchantALXCX() { return $this->DB('slave1')->get(TABLE_MERCHANT_ALXCX, '*', ['id' => 1]); } /** * 获取支付宝个人信息 * @return array|bool|mixed */ public function GetMyUserInfo() { return $this->DB('slave1')->get(TABLE_USER_ALIPAY, '*', ['al_id' => $_SESSION['al_id']]); } /** * 支付宝积分 * @param int $type_id * @param int $point * @param int $userid * @param int $orderid * @param int $al_id */ protected function Aliapy_AddPointLog($type_id = 0, $point = 0, $userid = 0, $orderid = 0, $al_id = 0) { if ($al_id) { $this->DB('master')->update(TABLE_USER_ALIPAY, [ 'order_count[+]' => 1, 'last_order_date' => date("Y-m-d H:i:s") ], [ 'al_id' => $al_id ]); } if (!empty($userid)) { $this->AddPointLog($type_id, $point, $userid, $orderid); } } /** * 支付宝客户端调用类 * @param array $merchant * @return AopClient|array */ function Alipay_client($merchant = array()) { if (empty($merchant)) { return array(); } $request = new AopClient; $request->gatewayUrl = "https://openapi.alipay.com/gateway.do"; $request->appId = $merchant['app_id']; $request->rsaPrivateKey = $merchant['merchant_private_key']; $request->format = "json"; $request->charset = "GBK"; $request->signType = "RSA2"; $request->alipayrsaPublicKey = $merchant['public_key']; return $request; } /** * 支付宝SDK的request * @param string $alipay_request * @return null */ function Alipay_request($alipay_request = '') { if (is_file(RPC_DIR . '/tool/Aliyun/ali_sdk/aop/request/' . $alipay_request . '.php')) { include RPC_DIR . '/tool/Aliyun/ali_sdk/aop/request/' . $alipay_request . '.php'; $request = new $alipay_request(); return $request; } else { return NULL; } } } ?>
支付宝封装sdk
最新推荐文章于 2022-12-02 15:38:36 发布