摘要:背景最近公司項目一直在圍繞著支付寶做應用開發,為了能保證消息能夠及時的給用戶傳遞,因此需要開發模板消息的功能,而小程序的模板消息也是最快捷的通知方式事先準備請仔細閱讀支付寶模板消息發送指引模板消息指引仔細閱讀用戶的授權文檔,用戶授權的詳細的
背景
最近公司項目一直在圍繞著支付寶做應用開發,為了能保證消息能夠及時的給用戶傳遞,因此需要開發模板消息的功能,而小程序的模板消息也是最快捷的通知方式
事先準備1、請仔細閱讀支付寶模板消息發送指引:模板消息指引
2、仔細閱讀用戶的授權文檔,用戶授權的詳細的實現步驟可以見我寫的另外一篇文章:《PHP實現支付寶小程序用戶授權的工具類》
3、在小程序中加入模板消息的權限,如下圖
4、仔細閱讀支付寶發送模板消息接口文檔:alipay.open.app.mini.templatemessage.send
5、事先通過小程序的form組件收集好對一個的formid,formid組件文檔
6、將小程序綁定到生活號上
7、配置一個模板消息編號,詳細步驟:小程序--->模板消息,最終配置號的模板消息如下
1、通過客戶端的form組件,收集好formid,并多帶帶開一個后端接口將formid通過http請求保存到后臺,最好是盡可能多的收集formid,比如按鈕的點擊事件、tab的切換上都可以增加formid組件
2、通過調用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,則表示消息發送
{ "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");效果圖 附錄:完整的工具類
".$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; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/29959.html
摘要:背景最近項目需要上線支付寶小程序,同時需要走用戶的授權流程完成用戶信息的存儲,以前做過微信小程序的開發,本以為實現授權的過程是很簡單的事情,但是再實現的過程中還是遇到了不少的坑,因此記錄一下實現的過程學到的知識支付寶開放接口的調用模式以及實 背景 最近項目需要上線支付寶小程序,同時需要走用戶的授權流程完成用戶信息的存儲,以前做過微信小程序的開發,本以為實現授權的過程是很簡單的事情,但是...
摘要:在用戶喜愛的眾多功能中,使用率最高的是模版消息推送。模版消息推送數的量級也由早期每天幾百條,變為后來的每天數百萬條。平臺支持少知曉云已經支持包括微信小程序和支付寶小程序在內的各大小程序平臺的消息推送,對平臺的支持也將在近期上線。 兩年多前,為了讓更多的人找到好玩、好用的小程序,我們成立了「知曉程序」。 再后來,我們推出了后端云服務平臺——知曉云,幫助大家降低創業成本,提升開發效率。 「...
摘要:目前支持哪些平臺的搬家目前對外開放版本釋放了微信小程序轉支付寶小程序的功能,這也是我們在調研中發現需求最多的。從筆者的了解來看,微信小程序框架原理更接近于,而支付寶小程序更接近于。 原文地址: https://ant-move.github.io/we... 螞蟻搬家工具(Antmove)是一個小程序開發輔助工具,致力于解決小程序跨平臺開發的難題,借助于 Antmove,你只需要編寫...
閱讀 3319·2021-11-23 09:51
閱讀 2436·2021-11-09 09:46
閱讀 1476·2019-08-30 15:54
閱讀 3121·2019-08-30 14:22
閱讀 2909·2019-08-29 12:40
閱讀 1629·2019-08-26 10:33
閱讀 1774·2019-08-23 17:09
閱讀 1553·2019-08-23 16:11