PHP實現支付寶小程序發送模板消息的工具類

背景

最近公司項目一直在圍繞着支付寶作應用開發,爲了能保證消息可以及時的給用戶傳遞,所以須要開發模板消息的功能,而小程序的模板消息也是最快捷的通知方式php

事先準備

一、請仔細閱讀支付寶模板消息發送指引:模板消息指引
二、仔細閱讀用戶的受權文檔,用戶受權的詳細的實現步驟能夠見我寫的另一篇文章:《PHP實現支付寶小程序用戶受權的工具類》
三、在小程序中加入模板消息的權限,以下圖html

clipboard.png
四、仔細閱讀支付寶發送模板消息接口文檔:alipay.open.app.mini.templatemessage.send
五、事先經過小程序的form組件收集好對一個的formid,formid組件文檔
六、將小程序綁定到生活號上
七、配置一個模板消息編號,詳細步驟:小程序--->模板消息,最終配置號的模板消息以下json

clipboard.png

實現流程

一、經過客戶端的form組件,收集好formid,並單獨開一個後端接口將formid經過http請求保存到後臺,最好是儘量多的收集formid,好比按鈕的點擊事件、tab的切換上均可以增長formid組件
二、經過調用alipay.open.app.mini.templatemessage.send接口,給客戶端發送模板消息 ,注意支付寶全部的模板消息都是基於生活號進行分發的,因此事先必定要綁定好對應的生活號小程序

實現方法

相關常量

//模板消息的接口方法名稱
const API_METHOD_SEND_TPL_MSG = 'alipay.open.app.mini.templatemessage.send';
//模板消息的結果返回節點名稱
const RESPONSE_OUTER_NODE_SEND_TPL_MSG = 'alipay_open_app_mini_templatemessage_send_response';

入口方法

/**
     * 發送小程序模板消息
     * @param $formId
     * @param $to 發送給用戶的編號
     * @param $tplId 模板編號
     * @param $tplContent 模板內容
     * @param $page 要跳轉的頁面
     * @return array
     */
    public static function sendAmpTplMsg($formId,$to,$tplId,$tplContent,$page = ''){
        $param = self::getTplMsgBaseParam($formId,$to,$tplId,$tplContent,$page = '');
        $url = self::buildRequestUrl($param);
        $response = self::getResponse($url,self::RESPONSE_OUTER_NODE_SEND_TPL_MSG);
        return $response;
    }

獲取基礎參數方法

/**
     * 獲取發送模板消息的接口參數
     */
    protected static function getTplMsgBaseParam($formId,$to,$tplId,$tplContent,$page = ''){
        $baseParam = [
            'to_user_id' => $to,
            'form_id' => $formId,
            'user_template_id' => $tplId,
            'page' => $page,
            'data' => $tplContent,
        ];
        $bizContent = json_encode($baseParam,JSON_UNESCAPED_UNICODE);
        $busiParam = [
            'biz_content' => $bizContent
        ];
        $param = self::buildApiBuisinessParam($busiParam,self::API_METHOD_SEND_TPL_MSG);
        return $param;
    }
    
    /**
     * 構建業務參數
     */
    protected static function buildApiBuisinessParam($businessParam,$apiMethod){
        $pubParam = self::getApiPubParam($apiMethod);
        $businessParam = array_merge($pubParam,$businessParam);
        $signContent = self::getSignContent($businessParam);
        error_log('sign_content ===========>'.$signContent);
        $rsaHelper = new RsaHelper();
        $sign = $rsaHelper->createSign($signContent);
        error_log('sign ===========>'.$sign);
        $businessParam['sign'] = $sign;
        return $businessParam;
    }
    
    /**
     * 公共參數
     *
     */
    protected static function getApiPubParam($apiMethod){
        $ampBaseInfo = BusinessHelper::getAmpBaseInfo();
        $param = [
            'timestamp' => date('Y-m-d H:i:s') ,
            'method' => $apiMethod,
            'app_id' => formatArrValue($ampBaseInfo,'appid',config('param.amp.appid')),
            'sign_type' =>self::SIGN_TYPE_RSA2,
            'charset' =>self::FILE_CHARSET_UTF8,
            'version' =>self::VERSION,
        ];
        return $param;
    }
    
    /**
     * 獲取簽名的內容
     */
    protected static function getSignContent($params) {
        ksort($params);
        $stringToBeSigned = "";
        $i = 0;
        foreach ($params as $k => $v) {
            if (!empty($v) && "@" != substr($v, 0, 1)) {
                if ($i == 0) {
                    $stringToBeSigned .= "$k" . "=" . "$v";
                } else {
                    $stringToBeSigned .= "&" . "$k" . "=" . "$v";
                }
                $i++;
            }
        }
        unset ($k, $v);
        return $stringToBeSigned;
    }
    
    /**
     * 構建請求連接
     */
    protected static function buildRequestUrl($param){
        $paramStr = http_build_query($param);
        return self::API_DOMAIN . $paramStr;
    }

獲取返回的結果值

/**
     * 獲取返回的數據,對返回的結果作進一步的封裝和解析
     */
    protected static function getResponse($url,$responseNode){
        $json = curlRequest($url);
        error_log("result is =========>".$json);
        $response = json_decode($json,true);
        $responseContent = formatArrValue($response,$responseNode,[]);
        $errResponse = formatArrValue($response,self::RESPONSE_OUTER_NODE_ERROR_RESPONSE,[]);
        if($errResponse){
            return $errResponse;
        }
        return $responseContent;
    }

返回結果

若是返回的節點code爲10000,則表示消息發送segmentfault

{
    "alipay_open_app_mini_templatemessage_send_response":{
        "code":"10000",
        "msg":"Success"
      }
    ,"sign":"ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
}

調用

$formId = 'MjA4ODMwMjI2MjE4Mzc4MF8xNTQ2ODQ0MTUyNzU0XzA1NQ==';
$openId = '2088302262183780';
$tplId = 'Mzc4OTk2ODU1YzM4NTI3NmY5ZjI2OTdhNGNkZDE2NGQ=';
$content = [
    'keyword1' => [
        'value' => '您的朋友【】偷去了你的能量啦~',
    ],
    'keyword2' => [
        'value' => '朋友偷能量提醒',
    ],
    'keyword3' => [
        'value' => '點我查看詳情',
    ],
];
$result = AmpHelper::sendAmpTplMsg($formId,$openId,$tplId,$content,$page= 'pages/index/index');

效果圖

clipboard.png

附錄:完整的工具類

<?php
/**
 * Created by PhpStorm.
 * User: My
 * Date: 2018/8/16
 * Time: 17:45
 */

namespace App\Http\Helper;

use App\Http\Helper\Sys\BusinessHelper;
use Illuminate\Support\Facades\Log;

class AmpHelper
{

    const API_DOMAIN = "https://openapi.alipay.com/gateway.do?";
    const API_METHOD_GENERATE_QR = 'alipay.open.app.qrcode.create';
    const API_METHOD_AUTH_TOKEN = 'alipay.system.oauth.token';
    const API_METHOD_GET_USER_INFO = 'alipay.user.info.share';
    const API_METHOD_SEND_TPL_MSG = 'alipay.open.app.mini.templatemessage.send';
    const API_METHOD_TRADE_PAY = 'alipay.trade.create';

    const SIGN_TYPE_RSA2 = 'RSA2';
    const VERSION = '1.0';
    const FILE_CHARSET_UTF8 = "UTF-8";
    const FILE_CHARSET_GBK = "GBK";
    const RESPONSE_OUTER_NODE_QR = 'alipay_open_app_qrcode_create_response';
    const RESPONSE_OUTER_NODE_AUTH_TOKEN = 'alipay_system_oauth_token_response';
    const RESPONSE_OUTER_NODE_USER_INFO = 'alipay_user_info_share_response';
    const RESPONSE_OUTER_NODE_SEND_TPL_MSG = 'alipay_open_app_mini_templatemessage_send_response';
    const RESPONSE_OUTER_NODE_TRADE_PAY = 'alipay_trade_create_response';
    const RESPONSE_OUTER_NODE_ERROR_RESPONSE = 'error_response';

    const STATUS_CODE_SUCCESS = 10000;
    const STATUS_CODE_EXCEPT = 20000;


    /**
     * 獲取用戶信息接口,根據token
     * @param $code 受權碼
     * 經過受權碼獲取用戶的信息
     */
    public static function getAmpUserInfoByAuthCode($code){
        $aliUserInfo = [];
        $tokenData = AmpHelper::getAmpToken($code);
        //若是token不存在,這種主要是爲了處理支付寶的異常記錄
        if(isset($tokenData['code'])){
            return $tokenData;
        }
        $token = formatArrValue($tokenData,'access_token');
        if($token){
            $userBusiParam = self::getAmpUserBaseParam($token);
            $url = self::buildRequestUrl($userBusiParam);
            $resonse = self::getResponse($url,self::RESPONSE_OUTER_NODE_USER_INFO);
            if($resonse['code'] == self::STATUS_CODE_SUCCESS){
                //有效的字段列
                $userInfoColumn = ['user_id','avatar','province','city','nick_name','is_student_certified','user_type','user_status','is_certified','gender'];
                foreach ($userInfoColumn as $column){
                    $aliUserInfo[$column] = formatArrValue($resonse,$column,'');
                }

            }else{
                $exceptColumns = ['code','msg','sub_code','sub_msg'];
                foreach ($exceptColumns as $column){
                    $aliUserInfo[$column] = formatArrValue($resonse,$column,'');
                }
            }
        }
        return $aliUserInfo;
    }


    /**
     * 獲取小程序token接口
     */
    public static function getAmpToken($code){
        $param = self::getAuthBaseParam($code);
        $url = self::buildRequestUrl($param);
        $response = self::getResponse($url,self::RESPONSE_OUTER_NODE_AUTH_TOKEN);
        $tokenResult = [];
        if(isset($response['code']) && $response['code'] != self::STATUS_CODE_SUCCESS){
            $exceptColumns = ['code','msg','sub_code','sub_msg'];
            foreach ($exceptColumns as $column){
                $tokenResult[$column] = formatArrValue($response,$column,'');
            }
        }else{
            $tokenResult = $response;
        }
        return $tokenResult;
    }

    /**
     * 獲取二維碼連接接口
     * 433ac5ea4c044378826afe1532bcVX78
     * https://openapi.alipay.com/gateway.do?timestamp=2013-01-01 08:08:08&method=alipay.open.app.qrcode.create&app_id=2893&sign_type=RSA2&sign=ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE&version=1.0&biz_content=
    {"url_param":"/index.html?name=ali&loc=hz", "query_param":"name=1&age=2", "describe":"二維碼描述"}
    */
    public static function generateQrCode($mpPage = 'pages/index',$queryParam = [],$describe){
        $param = self::getQrcodeBaseParam($mpPage,$queryParam,$describe );
        $url = self::buildRequestUrl($param);
        $response = self::getResponse($url,self::RESPONSE_OUTER_NODE_QR);
        return $response;
    }


    /**
     * 發送小程序模板消息
     * @param $formId
     * @param $to 發送給用戶的編號
     * @param $tplId 模板編號
     * @param $tplContent 模板內容
     * @param $page 要跳轉的頁面
     * @return array
     */
    public static function sendAmpTplMsg($formId,$to,$tplId,$tplContent,$page = ''){
        $param = self::getTplMsgBaseParam($formId,$to,$tplId,$tplContent,$page = '');
        $url = self::buildRequestUrl($param);
        $response = self::getResponse($url,self::RESPONSE_OUTER_NODE_SEND_TPL_MSG);
        return $response;
    }

    /**
     * 獲取支付的方法
     */
    public static function tradePay($outTradeNo,$totalAmount,$subject,$body = ''){
        $param = self::getTradePayBaseParam($outTradeNo,$totalAmount,$subject,$body);
        $result = self::doBusiness($param,self::RESPONSE_OUTER_NODE_TRADE_PAY);
        return $result;
    }


    /**
     * 獲取返回的數據,對返回的結果作進一步的封裝和解析
     */
    protected static function getResponse($url,$responseNode){
        $json = curlRequest($url);
        error_log("result is =========>".$json);
        $response = json_decode($json,true);
        $responseContent = formatArrValue($response,$responseNode,[]);
        $errResponse = formatArrValue($response,self::RESPONSE_OUTER_NODE_ERROR_RESPONSE,[]);
        if($errResponse){
            return $errResponse;
        }
        return $responseContent;
    }


    /**
     * 開始全部的業務並獲取相應的結果
     */
    protected static function doBusiness($busiParam,$reponseNode){
        $url = self::buildRequestUrl($busiParam);
        $response = self::getResponse($url,$reponseNode);
        return $response;
    }

    /**
     * 獲取請求的連接
     */
    protected static function buildQrRequestUrl($mpPage = 'pages/index',$queryParam = []){
        $paramStr = http_build_query(self::getQrBaseParam($mpPage,$queryParam));
        return self::API_DOMAIN . $paramStr;
    }



    /**
     * 構建請求連接
     */
    protected static function buildRequestUrl($param){
        $paramStr = http_build_query($param);
        return self::API_DOMAIN . $paramStr;
    }


    /**
     * 獲取用戶的基礎信息接口
     */
    protected static function getAmpUserBaseParam($token){
        $busiParam = [
            'auth_token' => $token,
        ];
        $param = self::buildApiBuisinessParam($busiParam,self::API_METHOD_GET_USER_INFO);
        return $param;

    }

    /**
     *獲取二維碼的基礎參數
     */
    protected static function getQrcodeBaseParam($page= 'pages/index/index',$queryParam = [],$describe = ''){
        $busiParam = [
            'biz_content' => self::getQrBizContent($page,$queryParam,$describe)
        ];
        $param = self::buildApiBuisinessParam($busiParam,self::API_METHOD_GENERATE_QR);
        return $param;

    }

    /**
     *獲取受權的基礎參數
     */
    protected static function getAuthBaseParam($code,$refreshToken = ''){
        $busiParam = [
            'grant_type' => 'authorization_code',
            'code' => $code,
            'refresh_token' => $refreshToken,
        ];
        $param = self::buildApiBuisinessParam($busiParam,self::API_METHOD_AUTH_TOKEN);
        return $param;
    }

    /**
     * 獲取發送模板消息的接口參數
     */
    protected static function getTplMsgBaseParam($formId,$to,$tplId,$tplContent,$page = ''){
        $baseParam = [
            'to_user_id' => $to,
            'form_id' => $formId,
            'user_template_id' => $tplId,
            'page' => $page,
            'data' => $tplContent,
        ];
        $bizContent = json_encode($baseParam,JSON_UNESCAPED_UNICODE);
        $busiParam = [
            'biz_content' => $bizContent
        ];
        $param = self::buildApiBuisinessParam($busiParam,self::API_METHOD_SEND_TPL_MSG);
        return $param;
    }

    /**
     * 獲取支付的基礎參數接口
     * @param $outTradeNo 第三方交易訂單號
     * @param $totalAmount 待支付的金額
     * @param $subject 主題,能夠是商品名稱
     * @param $body 描述,能夠是商品簡介
     * @return array
     */
    protected static function getTradePayBaseParam($outTradeNo,$totalAmount,$subject,$body = ''){
        $baseParam = [
            'out_trade_no' =>$outTradeNo,
            'total_amount' =>$totalAmount,
            'subject' =>$subject,
            'body' =>$body,
            'notify_url' => getDomain().'/alipay/notify'
        ];
        return self::buildBusiBaseParam($baseParam, self::API_METHOD_TRADE_PAY);
    }


    /**
     * 構建基礎的業務參數
     */
    protected static function buildBusiBaseParam($param, $apiMethod){
        $bizContent = json_encode($param,JSON_UNESCAPED_UNICODE);
        $busiParam = [
            'biz_content' => $bizContent
        ];
        $param = self::buildApiBuisinessParam($busiParam,$apiMethod);
        return $param;
    }

    /**
     * 構建業務參數
     */
    protected static function buildApiBuisinessParam($businessParam,$apiMethod){
        $pubParam = self::getApiPubParam($apiMethod);
        $businessParam = array_merge($pubParam,$businessParam);
        $signContent = self::getSignContent($businessParam);
        error_log('sign_content ===========>'.$signContent);
        $rsaHelper = new RsaHelper();
        $sign = $rsaHelper->createSign($signContent);
        error_log('sign ===========>'.$sign);
        $businessParam['sign'] = $sign;
        return $businessParam;
    }


    /**
     * 公共參數
     *
     */
    protected static function getApiPubParam($apiMethod){
        $ampBaseInfo = BusinessHelper::getAmpBaseInfo();
        $param = [
            'timestamp' => date('Y-m-d H:i:s') ,
            'method' => $apiMethod,
            'app_id' => formatArrValue($ampBaseInfo,'appid',config('param.amp.appid')),
            'sign_type' =>self::SIGN_TYPE_RSA2,
            'charset' =>self::FILE_CHARSET_UTF8,
            'version' =>self::VERSION,
        ];
        return $param;
    }


    /**
     * 獲取簽名的內容
     */
    protected static function getSignContent($params) {
        ksort($params);
        $stringToBeSigned = "";
        $i = 0;
        foreach ($params as $k => $v) {
            if (!empty($v) && "@" != substr($v, 0, 1)) {
                if ($i == 0) {
                    $stringToBeSigned .= "$k" . "=" . "$v";
                } else {
                    $stringToBeSigned .= "&" . "$k" . "=" . "$v";
                }
                $i++;
            }
        }
        unset ($k, $v);
        return $stringToBeSigned;
    }


    protected static function convertArrToQueryParam($param){
        $queryParam = [];
        foreach ($param as $key => $val){
            $obj = $key.'='.$val;
            array_push($queryParam,$obj);
        }
        $queryStr = implode('&',$queryParam);
        return $queryStr;
    }

    /**
     * 轉換字符集編碼
     * @param $data
     * @param $targetCharset
     * @return string
     */
    protected static function characet($data, $targetCharset) {
        if (!empty($data)) {
            $fileType = self::FILE_CHARSET_UTF8;
            if (strcasecmp($fileType, $targetCharset) != 0) {
                $data = mb_convert_encoding($data, $targetCharset, $fileType);
            }
        }
        return $data;
    }

    /**
     * 獲取業務參數內容
     */
    protected static function getQrBizContent($page, $queryParam = [],$describe = ''){
        if(is_array($queryParam)){
            $queryParam = http_build_query($queryParam);
        }
        $obj = [
            'url_param' => $page,
            'query_param' => $queryParam,
            'describe' => $describe
        ];
        $bizContent = json_encode($obj,JSON_UNESCAPED_UNICODE);
        return $bizContent;
    }

}
相關文章
相關標籤/搜索