支付宝生活号开发(php)
鉴于支付宝生活号开发的Demo看起来比较乱,自己最近做了这个东西,所以就整理了一下,希望对你有点帮助!
-
激活开发者模式
class AlipayController extends Controller { private $aop; protected function _initialize() { require './Application/Pay/Conf/aliConfig.php'; vendor("Alipay.aop.AopClient"); //引入sdk $aop = new \AopClient(); $aop->gatewayUrl = $config['gatewayUrl']; $aop->appId = $config['app_id']; $aop->rsaPrivateKey = $config['merchant_private_key']; $aop->alipayrsaPublicKey = $config['alipay_public_key']; $aop->apiVersion = '1.0'; $aop->postCharset=$config['charset']; $aop->format='json'; $aop->signType=$config['sign_type']; $this->aop = $aop; } //支付宝生活号激活开发者网关 public function gateway() { $data = $_POST; $sign_verify = $this->aop->rsaCheckV2($data, '', $data['sign_type']); if (!$sign_verify) { // 如果验证网关时,请求参数签名失败,则按照标准格式返回,方便在服务窗后台查看。 if ($data["service"] == "alipay.service.check") { $this->verifygw(false,$data["biz_content"]); } exit (); } // 验证网关请求 if ($data["service"] == "alipay.service.check") { $this->verifygw(true, $data["biz_content"]); } else if ($data["service"] == "alipay.mobile.public.message.notify") { // 处理收到的消息 $this->Message($data['biz_content']); } } }
verifygw
public function verifygw($is_sign_success,$biz_content=null) { $disableLibxmlEntityLoader = libxml_disable_entity_loader(true); $xml = simplexml_load_string ( $biz_content ); libxml_disable_entity_loader($disableLibxmlEntityLoader); // print_r($xml); $EventType = ( string ) $xml->EventType; // echo $EventType; if ($EventType == "verifygw") { if ($is_sign_success) { $response_xml = "<success>true</success><biz_content>" . $this->aop->rsaPrivateKey . "</biz_content>"; } else { // echo $response_xml; $response_xml = "<success>false</success><error_code>VERIFY_FAILED</error_code><biz_content>" . $this->aop->merchant_private_key . "</biz_content>"; } $mysign=$this->aop->alonersaSign($response_xml,$this->aop->rsaPrivateKey,$this->aop->signType); $return_xml = "<?xml version=\"1.0\" encoding=\"".$this->aop->postCharset."\"?><alipay><response>".$response_xml."</response><sign>".$mysign."</sign><sign_type>".$this->aop->signType."</sign_type></alipay>"; echo $return_xml; exit (); } }
aliConfig.php的内容
$config = array ( //应用ID,您的APPID。 'app_id' => "应用的appid", //商户私钥,您的原始格式RSA私钥 'merchant_private_key' => "应用的私钥", //编码格式 'charset' => "UTF-8", //签名方式 'sign_type'=>"RSA2", //支付宝网关 'gatewayUrl' => "https://openapi.alipay.com/gateway.do", //支付宝公钥 'alipay_public_key' => "应用的支付宝公钥" );
应用网关的地址需要写成 域名/模块/Alipay/gateway
-
开发者模式激活后就可以开始开发了
重点在这个Message里面//生活号事件类型判断 public function Message($biz_content) { $date = date("Y-m-d"); Log::write($biz_content, 'info', '', 'Logs/Pay/Alipay/ShengHuoHao/' . $date . '.txt'); header("Content-Type: text/xml;charset=utf-8"); $UserInfo = $this->getNode($biz_content, "UserInfo"); $FromUserId = $this->getNode($biz_content, "FromAlipayUserId"); $AppId = $this->getNode($biz_content, "AppId"); $CreateTime = $this->getNode($biz_content, "CreateTime"); $MsgType = $this->getNode($biz_content, "MsgType"); $EventType = $this->getNode($biz_content, "EventType"); $AgreementId = $this->getNode($biz_content, "AgreementId"); $ActionParam = $this->getNode($biz_content, "ActionParam"); $AccountNo = $this->getNode($biz_content, "AccountNo"); if ($EventType == 'follow') { //关注事件 }else if(($EventType == 'unfollow')){ //取消关注事件 } echo self::mkAckMsg ( $FromUserId ); exit (); }
其他事件判断请参考支付宝官方文档 https://docs.alipay.com/fw/api/105937
getNode
/** * 直接获取xml中某个结点的内容 * */ public function getNode($biz_content, $node) { $xml = "<?xml version=\"1.0\" encoding=\"GBK\"?>" . $biz_content; $dom = new \DOMDocument ( "1.0", "GBK" ); $dom->loadXML ( $xml ); $event_type = $dom->getElementsByTagName ( $node ); return $event_type->item ( 0 )->nodeValue; }
mkAckMsg
public function mkAckMsg($toUserId) { $response_xml = "<XML><ToUserId><![CDATA[" . $toUserId . "]]></ToUserId><AppId><![CDATA[" . $this->aop->appId . "]]></AppId><CreateTime>" . time () . "</CreateTime><MsgType><![CDATA[ack]]></MsgType></XML>"; $mysign=$this->aop->alonersaSign($response_xml,$this->aop->merchant_private_key,$this->aop->signType); $return_xml = "<?xml version=\"1.0\" encoding=\"".$this->aop->postCharset."\"?><alipay><response>".$response_xml."</response><sign>".$mysign."</sign><sign_type>".$this->aop->signType."</sign_type></alipay>"; return $return_xml; }
就写到这里吧!愿君能有几多愁,恰是一群太监上青楼!