支付宝封装sdk

<?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;
        }
    }
}

?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS微信支付封装是指在iOS平台上对微信支付支付支付功能进行封装和集成,方便开发者在自己的应用中集成使用这两种支付方式。 首先,对于微信支付封装,开发者可以使用微信官方提供的iOS SDK进行集成。该SDK包含了微信支付的各种功能和接口,如创建支付订单、发起支付请求、支付结果回调等。开发者只需按照微信提供的文档进行相应的调用和配置,即可实现在自己的应用中使用微信支付功能。 其次,对于支付支付封装,同样可以使用支付官方提供的iOS SDK进行集成。该SDK包含了支付支付的相关功能和接口,如创建支付订单、发起支付请求、支付结果回调等。开发者只需按照支付提供的文档进行相应的调用和配置,即可实现在自己的应用中使用支付支付功能。 通过对iOS微信支付封装,开发者可以在自己的应用中方便地集成和使用微信支付支付支付功能,提供给用户更多的支付方式选择,方便用户进行支付操作。同时,封装的过程也可以提高开发效率和代码的复用性,减少开发者的开发工作量。 需要注意的是,为了保证支付过程的安全性和可靠性,开发者在集成和使用支付功能时应按照相关的规范和指引进行操作,确保支付过程的顺利完成和支付信息的安全保密。同时,开发者还需要了解和掌握各种支付方式的使用规则和注意事项,方便根据实际需求进行相应的配置和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值