PHP を使用して WeChat シェイク周辺赤い封筒と周辺赤い封筒を実装する_PHP チュートリアル

WBOY
リリース: 2016-07-12 09:01:36
オリジナル
911 人が閲覧しました

PHP を使用して WeChat のシェイクレッドエンベロープ、周辺のレッドエンベロープを実装します

最近、あるプロジェクトを引き受けましたが、シェイクレッドエンベロープ機能を実装する必要があり、長い間オンラインで検索しましたが、できませんでした。ソースコードを見つける方法はありませんでした。私が自動的に作成したものです。この記事はあなたの参考のために私の努力の成果を共有します。意見を共有し、一緒に学び、進歩することができます。

WeChatの公式説明は以下の通りです

赤い封筒の説明書を振ってください

機能説明

周囲の赤い封筒を振るインターフェイスは、オフライン加盟店向けに提供される赤い封筒発行機能です。ユーザーは加盟店などのオフラインの場所で周囲を振ることで加盟店が発行する赤い封筒を受け取ることができます。オンライン転送や共有は無効です。

開発者はインターフェイスを通じてシェイクレッドエンベロープ機能を開発できます:
1. テンプレート読み込みページまたはカスタム HTML5 ページを使用して、WeChat ネイティブの赤い封筒ページを呼び出すことを選択できます (詳細については、赤い封筒作成アクティビティの use_template フィールドを参照してください。1 ではテンプレートを使用し、2 ではテンプレートを使用します)カスタム HTML5 ページ)
2. ネイティブの赤い封筒ページで赤い封筒を開くには、公開アカウントのメッセージを通じて送信する必要はありません
3. 公開アカウントをフォローする機能を提供し、ユーザーはアカウントをフォローするかどうかを選択できます (核分裂の赤い封筒を共有する場合は無効です)
4. 完成したページには、マーチャントの他のカスタマイズされた HTML5 ページにジャンプできるジャンプ リンクを設定できます
5. 同じユーザーは、1 つの赤い封筒イベントで 1 つの赤い封筒しか受け取ることができません

ユーザー側の対話プロセス

赤いエンベロープコンポーネントインターフェース呼び出しプロセス

1. 赤い封筒インターフェイスの許可を申請する: https://zb.weixin.qq.com でシェイク アンド シェイク マーチャントのバックエンドにログインし、開発者サポートに入り、シェイク アンド シェイク レッド エンベロープ コンポーネントを開くために申請します。インターフェース
2. 赤い封筒の事前注文: WeChat 決済 API を呼び出して赤い封筒の事前注文を行い、配布する赤い封筒の量と人数を通知し、赤い封筒のチケットを生成します。 3. イベントを作成し、赤い封筒の情報を入力します。Shake ペリフェラル プラットフォームの API を呼び出して赤い封筒のアクティビティを作成し、情報を入力し、注文時に生成された赤い封筒のチケットを渡します。 4. jsapi を呼び出して赤い封筒を描画します。シェイクアウト ページで、jsapi を呼び出して赤い封筒を描画します。赤い封筒を獲得したユーザーは、赤い封筒を開くことができます。 5. 上記のインターフェイスを呼び出すときは、赤い封筒プロバイダーと赤い封筒発行業者のパブリック アカウント要件が一致している必要があります。


説明:

赤い封筒プロバイダー: 赤い封筒の事前注文インターフェイスを通じて渡されるパラメーター wxappid で表される販売者 赤い封筒を発行する販売者: 赤い封筒のインターフェイスを呼び出して赤い封筒のアクティビティを作成し、赤い封筒の情報を入力し、赤い封筒の販売者公開アカウントを発行します。そのため、手順は、① 赤い封筒のアクティビティを作成する、② 事前に注文する、③ 赤い封筒を入力する、ということになります。以前に開催されたクラスを見つけて書き留めます 1. イベントを作成します


インターフェースの説明

赤い封筒アクティビティを作成し、赤い封筒アクティビティの有効期間、赤い封筒アクティビティのスイッチ、その他の基本情報を設定し、アクティビティIDを返します


インターフェース呼び出し手順

サーバー側の呼び出し

httpリクエストメソッド:POST URL: https://api.weixin.qq.com/shakearound/lottery/addlotteryinfo?access_token=ACCESSTOKEN&use_template=1&logo_url=LOGO_URL


リクエストパラメータの説明

リクエスト例

リーリー 戻りデータの説明

リーリー 送信されるデータはxmlではなくjsonであることに注意してください

フロントエンドページを作るだけ



phpコード

リーリー 2. 事前注文

インターフェースの説明

赤い封筒1枚の金額や種類などを設定し、赤い封筒情報を生成します。予約注文後、赤い封筒の描画操作を完了するには、72 時間以内に jsapi を呼び出す必要があります。 (赤い封筒の有効期限が切れると、資金は販売者の Tenpay アカウントに返金されます。)

インターフェース呼び出し手順

サーバーサイドコール

httpリクエストメソッド:POST

https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder

POSTデータ形式:XML

販売者証明書が必要です

リクエストパラメータの説明

请求示例

<xml>   
<sign><![CDATA[E1EE61A91C8E90F299DE6AE075D60A2D]]></sign>   
<mch_billno><![CDATA[0010010404201411170000046545]]></mch_billno>   
<mch_id><![CDATA[10000097]]></mch_id>   
<wxappid><![CDATA[wxcbda96de0b165486]]></wxappid>   
<send_name><![CDATA[send_name]]></send_name>   
<hb_type><![CDATA[NORMAL]]></hb_type>   
<auth_mchid><![CDATA[10000098]]></auth_mchid>   
<auth_appid><![CDATA[wx7777777]]></auth_appid>   
<total_amount><![CDATA[200]]></total_amount>   
<amt_type><![CDATA[ALL_RAND]]></amt_type>   
<total_num><![CDATA[3]]></total_num>   
<wishing><![CDATA[恭喜发财 ]]></wishing>   
<act_name><![CDATA[ 新年红包 ]]></act_name>   
<remark><![CDATA[新年红包 ]]></remark>   
<risk_cntl><![CDATA[NORMAL]]></risk_cntl>   
<nonce_str><![CDATA[50780e0cca98c8c8e814883e5caa672e]]></nonce_str>
</xml>
ログイン後にコピー

返回数据说明

以下字段在return_code 和result_code都为SUCCESS的时候有返回

成功示例

 <xml> 
<return_code><![CDATA[SUCCESS]]></return_code> 
<return_msg><![CDATA[发放成功.]]></return_msg> 
<result_code><![CDATA[SUCCESS]]></result_code> 
<err_code><![CDATA[0]]></err_code> 
<err_code_des><![CDATA[发放成功.]]></err_code_des> 
<mch_billno><![CDATA[0010010404201411170000046545]]></mch_billno> 
<mch_id>10010404</mch_id> 
<wxappid><![CDATA[wx6fa7e3bab7e15415]]></wxappid> 
<sp_ticket><![CDATA[0cca98c8c8e814883]]></sp_ticket> 
<total_amount>3</total_amount> 
<detail_id><![CDATA[001001040420141117000004888]]></detail_id> 
<send_time><![CDATA[20150101080000]]></send_time> 
</xml> 
ログイン後にコピー

失败示例

 <xml>   
<return_code><![CDATA[FAIL]]></return_code> 
<return_msg><![CDATA[系统繁忙,请稍后再试.]]></return_msg> 
<result_code><![CDATA[FAIL]]></result_code> 
<err_code><![CDATA[268458547]]></err_code> 
<err_code_des><![CDATA[系统繁忙,请稍后再试.]]></err_code_des> 
<mch_billno><![CDATA[0010010404201411170000046542]]></mch_billno>     
<mch_id>10010404</mch_id> 
<wxappid><![CDATA[wx6fa7e3bab7e15415]]></wxappid>  
<total_amount>3</total_amount> 
</xml> 
/**
 * 摇一摇红包预下单 
 * @author jiosen
 */
class Yhb_pub extends Wxpay_client_pub
{
  var $code;//code码,用以获取openid
  var $openid;//用户的openid
  function __construct()
  {
    //设置接口链接
    $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder";
    //设置curl超时时间
    $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
  }
  /**
   * 生成接口参数xml
   */
  function createXml()
  {
    try
    {
      //检测必填参数
      if($this->parameters["mch_billno"] == null)
      {
        throw new SDKRuntimeException("缺少发红包接口必填参数mch_billno!"."<br>");
      }elseif ($this->parameters["send_name"] == null ) {
        throw new SDKRuntimeException("缺少发红包接口必填参数send_name!"."<br>");
      }elseif ($this->parameters["total_amount"] == null) {
        throw new SDKRuntimeException("缺少发红包接口必填参数total_amount!"."<br>");
      }elseif ($this->parameters["total_num"] == null) {
        throw new SDKRuntimeException("缺少发红包接口必填参数total_num!"."<br>");
      }elseif ($this->parameters["wishing"] == null) {
        throw new SDKRuntimeException("缺少发红包接口必填参数wishing!"."<br>");
      }elseif ($this->parameters["act_name"] == null) {
        throw new SDKRuntimeException("缺少发红包接口必填参数act_name!"."<br>");
      }elseif ($this->parameters["remark"] == null) {
        throw new SDKRuntimeException("缺少发红包接口必填参数remark!"."<br>");
      }
      $this->parameters["wxappid"] = WxPayConf_pub::APPID;//公众账号ID
      $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
      $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
      //$this->parameters["re_openid"] = $this->openid;//用户openid
      $this->parameters["hb_type"] = 'NORMAL';//红包类型 NORMAL-普通红包;GROUP-裂变红包(可分享红包给好友,无关注公众号能力)。 
      $this->parameters["auth_mchid"] = '1000052601';//摇周边商户号
      $this->parameters["auth_appid"] = 'wxbf42bd79c4391863';//摇周边 appid
      $this->parameters["risk_cntl"] = 'NORMAL';//风控设置
      $this->parameters["sign"] = $this->getSign($this->parameters);//签名
      return $this->arrayToXml($this->parameters);
    }catch (SDKRuntimeException $e)
    {
      die($e->errorMessage());
    }
  }
  function hbpreorder()
  {
    $this->postXmlSSL();
    $this->result = $this->xmlToArray($this->response);
    return $this->result;
  }
  /**
   * 作用:生成可以获得code的url
   */
  function createOauthUrlForCode($redirectUrl)
  {
    $urlObj["appid"] = WxPayConf_pub::APPID;
    $urlObj["redirect_uri"] = "$redirectUrl";
    $urlObj["response_type"] = "code";
    $urlObj["scope"] = "snsapi_base";
    $urlObj["state"] = "STATE"."#wechat_redirect";
    $bizString = $this->formatBizQueryParaMap($urlObj, false);
    return "https://open.weixin.qq.com/connect/oauth2/authorize&#63;".$bizString;
  }
  /**
   * 作用:生成可以获得openid的url
   */
  function createOauthUrlForOpenid()
  {
    $urlObj["appid"] = WxPayConf_pub::APPID;
    $urlObj["secret"] = WxPayConf_pub::APPSECRET;
    $urlObj["code"] = $this->code;
    $urlObj["grant_type"] = "authorization_code";
    $bizString = $this->formatBizQueryParaMap($urlObj, false);
    return "https://api.weixin.qq.com/sns/oauth2/access_token&#63;".$bizString;
  }
  /**
   * 作用:通过curl向微信提交code,以获取openid
   */
  function getOpenid()
  {
    $url = $this->createOauthUrlForOpenid();
    //初始化curl
    $ch = curl_init();
    //设置超时
    curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    //运行curl,结果以jason形式返回
    $res = curl_exec($ch);
    curl_close($ch);
    //取出openid
    $data = json_decode($res,true);
    $this->openid = $data['openid'];
    return $this->openid;
  }
  /**
   * 作用:设置code
   */
  function setCode($code_)
  {
    $this->code = $code_;
  }
}
ログイン後にコピー

这里需要注意的是 auth_mchid 和 auth_appid 要填摇周边平台给出的appid 和商户号
调用 (这里不贴前端页面了)

$Redpack = new \Yhb_pub(); 
 $Redpack->setParameter('mch_billno', WxPayConf_pub::MCHID.date('YmdHis').rand(1000, 9999));
 //商户名称
 $Redpack->setParameter('send_name', "商户名称");
 //付款金额
 $Redpack->setParameter('total_amount', 100); //单位分
 //红包发放总人数
 $Redpack->setParameter('amt_type', "ALL_RAND");
 $Redpack->setParameter('total_num', 1);
 //红包祝福语
 $Redpack->setParameter('wishing', "摇一摇送红包");
 //活动名称
 $Redpack->setParameter('act_name', "摇一摇送红包");
 //备注
 $Redpack->setParameter('remark', "摇一摇送红包 备注");
 $result = $Redpack->hbpreorder();
 if($result[''])
ログイン後にコピー

3.录入红包

接口说明

在调用"创建红包活动"接口之后,调用此接口录入红包信息。注意,此接口每次调用,都会向某个活动新增一批红包信息,如果红包数少于100 个,请通过一次调用添加所有红包信息。如果红包数大于100,可以多次调用接口添加。请注意确保多次录入的红包ticket总的数目不大于创建该红包活动 时设置的total值。

接口调用说明

服务器端调用

http请求方式: POST
URL:https://api.weixin.qq.com/shakearound/lottery/setprizebucket?access_token=ACCESSTOKEN

请求参数说明


POST BODY:JSON格式的结构体

请求示例

 Content-Type: application/json Post Body:
{   
"lottery_id": "xxxxxxllllll",   
"mchid": "10000098",   
"sponsor_appid": "wx8888888888888888",  
"prize_info_list": [     
   {      
  "ticket": "v1|ZiPs2l0hpMBp3uwGI1rwp45vOdz/V/zQ/00jP9MeWT+e47/q1FJjwCIP34frSjzOxAEzJ7k2CtAg1pmcShvkChBWqbThxPm6MBuzceoHtj79iHuHaEn0WAO+j4sXnXnbGswFOlDYWg1ngvrRYnCY3g=="
   },
   {
  "ticket": "v1|fOhNUTap1oepSm5ap0hx1gmATM\/QX\/xn3sZWL7K+5Z10sbV5\/mZ4SwxwxbK2SPV32eLRvjd4ww1G3H5a+ypqRrySi+4oo97y63KoEQbRCPjbkyQBY8AYVyvD40V2b9slTQCm2igGY98mPe+VxZiayQ=="
   }
  ]
}


ログイン後にコピー

返回数据说明

示例

 {   
"errcode":0,   
"errmsg":"",   
"repeat_ticket_list":[     
   {      
"ticket": "v1|ZiPs2l0hpMBp3uwGI1rwp45vOdz/V/zQ/00jP9MeWT+e47/q1FJjwCIP34frSjzOxAEzJ7k2CtAg1pmcShvkChBWqbThxPm6MBuzceoHtj79iHuHaEn0WAO+j4sXnXnbGswFOlDYWg1ngvrRYnCY3g=="            
   },
   {
"ticket":"v1|ZiPs2l0zzXCsdfwe45dxCdHiukOdz/V/zQ/89xcnC5XnT+e47/q1FJjwCO4frSjzOxAEzJ7k2CtAg1pmcShvkChBWzc45dDGC32Dcxx4DGxczjDCGsdjowe9iHuaEn0WAO+GswFOlDYWg1ngvrRYnCY3g=="     }   
   } 
 ], 
"success_num":100 
}

/**
 * 摇一摇红包 录入红包
 * @author jiosen
 */
class lottery_pub extends Wxpay_client_pub
{
  var $code;//code码,用以获取openid
  var $openid;//用户的openid
  function __construct($access_token)
  {
    //设置接口链接
    $this->url = "https://api.weixin.qq.com/shakearound/lottery/setprizebucket&#63;access_token=".$access_token;
    //设置curl超时时间
    $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
  }
  /**
   * 生成接口参数 json
   */
  function createJson()
  {
    try
    {
      //检测必填参数
      if($this->parameters["lottery_id"] == null)
      {
        throw new SDKRuntimeException("缺少抽奖活动id lottery_id !"."<br>");
      }else if(empty($this->parameters["prize_info_list"])){
        throw new SDKRuntimeException("缺少抽奖活动红包 prize_info_list !"."<br>");
      }
      $this->parameters["mchid"] = WxPayConf_pub::MCHID;//授权商户号
      $this->parameters["sponsor_appid"] = WxPayConf_pub::APPID;//授权上号appid
      return json_encode($this->parameters);
      //echo json_encode($this->parameters);die;
    }catch (SDKRuntimeException $e)
    {
      die($e->errorMessage());
    }
  }
  function setJsonArray($parameter, $parameterValue){
    $this->parameters[$this->trimString($parameter)] = $parameterValue;
  }
  function hbpreorder()
  {
    $data = $this->createJson();
    $result = $this->curl_post($this->url,$data);
    $result = json_decode($result);
    return $result;
  }
  function curl_post($url,$data)
  {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_POST, 1);//发送一个常规的Post请求
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);//Post提交的数据包
    $rv = curl_exec($curl);//输出内容
    curl_close($curl);
    return $rv;
  }
  /**
   * 作用:生成可以获得code的url
   */
  function createOauthUrlForCode($redirectUrl)
  {
    $urlObj["appid"] = WxPayConf_pub::APPID;
    $urlObj["redirect_uri"] = "$redirectUrl";
    $urlObj["response_type"] = "code";
    $urlObj["scope"] = "snsapi_base";
    $urlObj["state"] = "STATE"."#wechat_redirect";
    $bizString = $this->formatBizQueryParaMap($urlObj, false);
    return "https://open.weixin.qq.com/connect/oauth2/authorize&#63;".$bizString;
  }
  /**
   * 作用:生成可以获得openid的url
   */
  function createOauthUrlForOpenid()
  {
    $urlObj["appid"] = WxPayConf_pub::APPID;
    $urlObj["secret"] = WxPayConf_pub::APPSECRET;
    $urlObj["code"] = $this->code;
    $urlObj["grant_type"] = "authorization_code";
    $bizString = $this->formatBizQueryParaMap($urlObj, false);
    return "https://api.weixin.qq.com/sns/oauth2/access_token&#63;".$bizString;
  }
  /**
   * 作用:通过curl向微信提交code,以获取openid
   */
  function getOpenid()
  {
    $url = $this->createOauthUrlForOpenid();
    //初始化curl
    $ch = curl_init();
    //设置超时
    curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    //运行curl,结果以jason形式返回
    $res = curl_exec($ch);
    curl_close($ch);
    //取出openid
    $data = json_decode($res,true);
    $this->openid = $data['openid'];
    return $this->openid;
  }
  /**
   * 作用:设置code
   */
  function setCode($code_)
  {
    $this->code = $code_;
  }
}
ログイン後にコピー

调用

<br>    $token = getAccessToken();<br>    $Redpack = new \lottery_pub($token);<br>    $lottery_id = ''; //这里读取数据库取出创建活动时返回的 lottery_id
 $Redpack->setParameter('lottery_id', $lottery_id);
  //活动id
  $prize_info_list =array(array('ticket'=>'这里取出预下单返回的sp_ticket'));
  $Redpack->setJsonArray('prize_info_list', $prize_info_list);
  //提交
  $Redpack->hbpreorder();
ログイン後にコピー

抢红包页面 php

function getshakeinfo($access_token,$ticket){
    $getshakeinfourl='https://api.weixin.qq.com/shakearound/user/getshakeinfo&#63;access_token='.$access_token;
     $jo=0;
     if($access_token){
      $data=array('ticket' =>$ticket);
      $rd=$this->curl_post($getshakeinfourl,json_encode($data));
      $jo=json_decode($rd);
     }else{
      echo 'access_token null';
     }
     return $jo;
  }
    $ticket=$_GET['ticket'];//获叏设备信息,包括 U UID 、 major 、 minor ,以及距离、 openID 等信息
    $token = getAccessToken();
    $shake=getshakeinfo($token,$ticket);
    $openid=$shake->data->openid;
    $jsapi = new Common_util_pub(); 
    $noncestr = $jsapi->createNoncestr();
    $parameters = array(
        'lottery_id' =>'创建活动时候返回的活动ID',
        'noncestr'=>$noncestr,
        'openid'=>$openid,
      );
    $signStr = $jsapi->formatBizQueryParaMap($parameters,false);
    $key = '创建活动时候的key';
    $signStr=$signStr."&key=".$key;
    $sign = strtoupper(md5($signStr));
ログイン後にコピー

上一步返回的参数填在抢红包html页面

<script type="text/javascript" src="http://zb.weixin.qq.com/app/shakehb/BeaconShakehbJsBridge.js">
</script>
<script type="text/javascript">
  BeaconShakehbJsBridge.ready(function(){
    //alert();
    BeaconShakehbJsBridge.invoke('jumpHongbao',{lottery_id:"{$lottery_id}",noncestr:"{$noncestr}",openid:"{$openid}",sign:"{$sign}"}); 
    });
</script> 
ログイン後にコピー

红包绑定用户事件通知     

接口说明

用户进入红包页面时,后台会将一个红包ticket和用户openid绑定,微信会把这个事件推送到开发者填写的URL(登录公众平台进入开发者中心设置)。推送内容包含用户openid,红包活动id,红包ticket、金额以及红包绑定时间。
注:红包绑定用户不等同于用户领取红包。用户进入红包页面后,有可能不拆红包,但该红包ticket已被绑定,不能再被其他用户绑定,过期后会退回商户财付通账户。

推送XML数据包示例

<xml> 
<ToUserName><![CDATA[toUser]]></ToUserName> 
<FromUserName><![CDATA[fromUser]]></FromUserName> 
<CreateTime>1442824314</CreateTime> 
<MsgType><![CDATA[event]]></MsgType> 
<Event><![CDATA[ShakearoundLotteryBind]]></Event> 
<LotteryId><![CDATA[lotteryid]]></LotteryId> 
<Ticket><![CDATA[ticket]]></Ticket> 
<Money>88</Money> 
<BindTime>1442824313</BindTime> 
</xml> 
ログイン後にコピー

添加事件处理即可

/**
   * 事件处理
   * @param unknown $object
   * @return string
   */
  public function handleEvent($object) {
    // Event是事件类型(subscribe,LOCATION)
    $oneEvent = $object->Event;
    // EventKey是菜单事件的key值
    $key = $object->EventKey;
    // 关注事件
    if ($oneEvent == "subscribe" || $oneEvent == "SCAN") {
      if(!empty($object->Ticket)) {
        //扫码事件
        ....
      } else {
        //关注事件
        ....
      }
    }else if($oneEvent=="ShakearoundLotteryBind"){
      //添加到数据库
    }else if.......其他的事件......
  }
ログイン後にコピー

 完毕了.时间比较匆忙 也没时间做优化 大神经过顺便指导12  我好搓的英文基础

下面贴上完整WxPayPubHelper 集成了所有支付类 配置可用

<?php
/*** WeChat 支払いヘルプ ライブラリ
 * ================================================ ===
 ※インターフェースには3種類あります。
 * [リクエストインターフェイス]--Wxpay_client_
 * 統合支払インターフェイス クラス -- UnifiedOrder
 * 注文クエリ インターフェイス -- OrderQuery
 * 返金申請インターフェース -- 返金
 * 返金クエリ インターフェース - RefundQuery
 * 請求書インターフェース - DownloadBill
 * ショートリンク変換インターフェース -- ShortUrl
 * [応答性の高いインターフェイス]--Wxpay_server_
 * 一般的な通知インターフェイス -- 通知
 * ネイティブ支払い - 加盟店に製品情報インターフェイスの取得を要求 - NativeCall
 * 【他の】
 * 静的リンク QR コード - NativeLink
 * JSAPI決済--JsApi
 * ================================================ ====
 ※【CommonUtil】よく使うツール:
 ※trimString()、パラメータ設定時に必要な文字処理関数
 * createNoncestr()、32 ビット以下のランダムな文字列を生成します
 * formatBizQueryParaMap()、フォーマットパラメータ、署名プロセスに必要
 * getSign()、署名を生成
 * arrayToXml()、配列を XML に変換
 * xmlToArray()、xml から配列へ
 * postXmlCurl()、投稿モードで対応するインターフェイス URL に XML を送信します
 * postXmlSSLCurl()、証明書を使用して、ポスト モードで対応するインターフェイス URL に XML を送信します*/
  include_once("SDKRuntimeException.php");
  include_once("WxPay.pub.config.php");
/*** すべてのインターフェースの基本クラス*/
クラス Common_util_pub
{
  関数 __construct() {
  }
  関数トリム文字列($value)
  {
    $ret = null;
    if (null != $value) 
    {
      $ret = $value;
      if (strlen($ret) == 0) 
      {
        $ret = null;
      }
    }
    $ret を返します。
  }
  /*** 機能: 32 ビット以下のランダムな文字列を生成します。*/
  パブリック関数 createNoncestr( $length = 32 ) 
  {
    $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; 
    $str ="";
    for ( $i = 0; $i < $length; $i++ ) { 
      $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1); 
    } 
    $str を返します。
  }
  /*** 機能: 署名プロセスに必要なフォーマットパラメータ*/
  関数 formatBizQueryParaMap($paraMap, $urlencode)
  {
    $buff = "";
    ksort($paraMap);
    foreach ($paraMap as $k => $v)
    {
      if($urlencode)
      {
        $v = urlencode($v);
      }
      //$buff .= strto lower($k) . 「=」。 $v 。 "&";
      $buff .= $k . 「=」。 $v 。 "&";
    }
    $reqPar;
    if (strlen($buff) > 0) 
    {
      $reqPar = substr($buff, 0, strlen($buff)-1);
    }
    $reqPar を返します。
  }
  /*** 機能: 署名を生成*/
  パブリック関数 getSign($Obj)
  {
    foreach ($Obj as $k => $v)
    {
      $パラメータ[$k] = $v;
    }
    //签名步骤一:按字典序排序パラメータ
    ksort($Parameters);
    $String = $this->formatBizQueryParaMap($Parameters, false);
    //echo '【string1】'.$String.'</br>';
    //签名步骤二:文字列の後にKEYを追加
    $String = $String."&key=".WxPayConf_pub::KEY;
    //echo "【string2】".$String."</br>";
    //签名步骤三:MD5加密
    $String = md5($String);
    //echo "【string3】 ".$String."</br>";
    //签名步骤四:全字符转は大写
    $result_ = strtoupper($String);
    //echo "【結果】 ".$result_."</br>";
    $result_ を返します。
  }
  /*** 関数: 配列を XML に変換*/
  関数 arrayToXml($arr)
  {
    $xml = "<xml>";
    foreach ($arr as $key=>$val)
    {
       if (is_numeric($val))
       {
        $xml.="<".$key.">".$val."</".$key.">"; 
       }
       それ以外
        $xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; 
    }
    $xml.="</xml>";
    $xml を返します。 
  }
  /*** 関数: XML を配列に変換します。*/
  パブリック関数 xmlToArray($xml)
  {    
    // XML を配列にします    
    $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);   
    $array_data を返します。
  }
  /*** 機能: 投稿モードで対応するインターフェイス URL に XML を送信します。*/
  パブリック関数 postXmlCurl($xml,$url,$秒=30)
  {    
    //初期化カール    
    $ch =curl_init();
    //設置超時間
    カール_setopt($ch, CURLOP_TIMEOUT, $秒);
    // ここに設置代理人がいる場合、その旨
    //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
    //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
    //設置ヘッダー
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    // 要求結果は文字列であり、画面上に出力されます
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    //ポスト手渡し方式
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    //カールを実行する
    $data =curl_exec($ch);
    カール_クローズ($ch);
    //返結果
    if($data)
    {
      カール_クローズ($ch);
      $data を返します。
    }それ以外
    {
      $error =curl_errno($ch);
      echo "カール エラー、エラー コード: $error"."<br>";
      echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>エラー原因クエリ</a></br>";
      カール_クローズ($ch);
      false を返します。
    }
  }
  /*** 機能: 証明書を使用して、ポスト モードで対応するインターフェイス URL に XML を送信します。*/
  関数 postXmlSSLCurl($xml,$url,$秒=30)
  {
    $ch =curl_init();
    //残業時間
    curl_setopt($ch,CURLOPT_TIMEOUT,$秒);
    // プロキシがある場合は、ここにプロキシを設定します
    //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
    //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
    //ヘッダーを設定する
    curl_setopt($ch,CURLOPT_HEADER,FALSE);
    // 結果は文字列である必要があり、それを画面に出力します
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
    //証明書を設定する
    //証明書を使用します: 証明書とキーはそれぞれ 2 つの .pem ファイルに属します
    // デフォルトの形式は PEM であり、コメント化できます
//curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
//curl_setopt($ch,CURLOPT_SSLCERT,WxPayConf_pub::SSLCERT_PATH );
// //デフォルトの形式は PEM であり、コメント化できます
//curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
//curl_setopt($ch,CURLOPT_SSLKEY, WxPayConf_pub::SSLKEY_PATH);
    curl_setopt($ch, CURLOPT_SSLCERT,WxPayConf_pub::SSLCERT_PATH);
    curl_setopt($ch, CURLOPT_SSLKEY,WxPayConf_pub::SSLKEY_PATH);
    curl_setopt($ch, CURLOPT_CAINFO, WxPayConf_pub::SSLCA_PATH); // CA ルート証明書 (Web サイト証明書が CA によって発行されたかどうかを確認するために使用されます)
    //ポスト送信メソッド
    curl_setopt($ch,CURLOPT_POST, true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
    $data =curl_exec($ch);
    //結果を返す
    if($data){
      カール_クローズ($ch);
      $data を返します。
    }
    それ以外 {
      $error =curl_errno($ch);
      echo "カール エラー、エラー コード: $error"."<br>";
      echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>エラー原因クエリ</a></br>";
      カール_クローズ($ch);
      false を返します。
    }
  }
  /*** 関数: 配列を出力*/
  関数 printErr($wording='',$err='')
  {
    print_r('<pre class="brush:php;toolbar:false">');
    echo $wording."</br>";
    var_dump($err);
    print_r('
'); } } /*** リクエストインターフェイスの基本クラス*/ クラス Wxpay_client_pub は Common_util_pub を拡張します { var $parameters;//リクエストパラメータ、タイプは連想配列です public $response; //WeChat から返された応答 public $result;//戻りパラメータ、型は連想配列 var $url;//インターフェースリンク var $curl_timeout;//カールのタイムアウト時間 /*** 機能: リクエストパラメータの設定*/ 関数 setParameter($parameter, $parameterValue) { $this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue); } /*** 機能: 標準リクエストパラメータの設定、署名の生成、インターフェースパラメータ XML の生成*/ 関数createXml() { $this->parameters["appid"] = WxPayConf_pub::APPID;//パブリック アカウント ID $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//販売者 ID $this->parameters["nonce_str"] = $this->createNoncestr();//ランダムな文字列 $this->parameters["sign"] = $this->getSign($this->parameters);//署名 return $this->arrayToXml($this->parameters); } /*** 機能: ポストリクエスト XML*/ 関数postXml() { $xml = $this->createXml(); $this->response = $this->postXmlCurl($xml,$this->url,$this->curl_timeout); $this->レスポンスを返す; } /*** 機能: 証明書ポストを使用して XML をリクエストします*/ 関数 postXmlSSL() { $xml = $this->createXml(); $this->response = $this->postXmlSSLCurl($xml,$this->url,$this->curl_timeout); $this->レスポンスを返す; } /*** 機能: 結果を取得します。デフォルトでは証明書は使用されません*/ 関数 getResult() { $this->postXml(); $this->result = $this->xmlToArray($this->response); $this->結果を返す; } } /*** 統合された支払いインターフェイス クラス*/ クラス UnifiedOrder_pub は Wxpay_client_pub を拡張します { function__construct() { //インターフェースリンクを設定する $this->url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //カールのタイムアウトを設定する $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT; } /*** インターフェースパラメータ XML を生成*/ 関数createXml() { 試す { //必要なパラメータを検出する if($this->parameters["out_trade_no"] == null) { throw new SDKRuntimeException("統合決済インターフェイスの必須パラメータ out_trade_no が見つかりません!"."
"); }elseif($this->parameters["body"] == null){ throw new SDKRuntimeException("統合決済インターフェイスの必須パラメータ本体がありません!"."
"); }elseif ($this->parameters["total_fee"] == null ) { throw new SDKRuntimeException("統合支払いインターフェイスの必須パラメータ total_fee が見つかりません!"."
"); }elseif ($this->parameters["notify_url"] == null) { throw new SDKRuntimeException("統合決済インターフェイスの必須パラメータnotify_urlが見つかりません!"."
"); }elseif ($this->parameters["trade_type"] == null) { throw new SDKRuntimeException("統合決済インターフェイスの必須パラメータ trade_type がありません!"."
"); }elseif ($this->parameters["trade_type"] == "JSAPI" && $this->parameters["openid"] == NULL){ throw new SDKRuntimeException("統合決済インターフェイスに必須パラメータ openid がありません! trade_type が JSAPI の場合、openid は必須パラメータです!"."
"); } $this->parameters["appid"] = WxPayConf_pub::APPID;//パブリック アカウント ID $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//販売者 ID $this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];//ターミナルIP $this->parameters["nonce_str"] = $this->createNoncestr();//ランダムな文字列 $this->parameters["sign"] = $this->getSign($this->parameters);//署名 return $this->arrayToXml($this->parameters); }catch (SDKRuntimeException $e) { die($e->errorMessage()); } } /*** prepay_id を取得する*/ 関数 getPrepayId() { $this->postXml(); $this->result = $this->xmlToArray($this->response); $prepay_id = $this->result["prepay_id"]; $prepay_id を返します; } } /*** 注文クエリインターフェイス*/ クラス OrderQuery_pub は Wxpay_client_pub を拡張します { function__construct() { //インターフェースリンクを設定する $this->url = "https://api.mch.weixin.qq.com/pay/orderquery"; //カールのタイムアウトを設定する $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT; } /*** インターフェースパラメータ XML を生成*/ 関数createXml() { 試す { //必要なパラメータを検出する if($this->parameters["out_trade_no"] == null && $this->parameters["transaction_id"] == null) { throw new SDKRuntimeException("注文クエリ インターフェイスで、少なくとも 1 つの out_trade_no とtransaction_id を入力してください!"."
"); } $this->parameters["appid"] = WxPayConf_pub::APPID;//パブリック アカウント ID $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//販売者 ID $this->parameters["nonce_str"] = $this->createNoncestr();//ランダムな文字列 $this->parameters["sign"] = $this->getSign($this->parameters);//署名 return $this->arrayToXml($this->parameters); }catch (SDKRuntimeException $e) { die($e->errorMessage()); } } } /*** 返金申請インターフェース*/ クラスRefund_pubはWxpay_client_pubを拡張します { 関数 __construct() { //インターフェースリンクを設定する $this->url = "https://api.mch.weixin.qq.com/secapi/pay/refund"; //カールのタイムアウトを設定する $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT; } /*** インターフェースパラメータ XML を生成*/ 関数createXml() { 試す { //必要なパラメータを検出する if($this->parameters["out_trade_no"] == null && $this->parameters["transaction_id"] == null) { throw new SDKRuntimeException("返金アプリケーション インターフェイスで、少なくとも 1 つの out_trade_no とtransaction_id を入力してください!"."
"); }elseif($this->parameters["out_refund_no"] == null){ throw new SDKRuntimeException("返金アプリケーション インターフェイスに、必須パラメータ out_refund_no がありません!"."
"); }elseif($this->parameters["total_fee"] == null){ throw new SDKRuntimeException("返金アプリケーション インターフェイスに、必須パラメータ total_fee がありません!"."
"); }elseif($this->parameters["refund_fee"] == null){ throw new SDKRuntimeException("返金アプリケーション インターフェイスに、必要なパラメータrefund_feeがありません!"."
"); }elseif($this->parameters["op_user_id"] == null){ throw new SDKRuntimeException("返金アプリケーション インターフェイスに、必須パラメータ op_user_id がありません。"."
"); } $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号 $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串 $this->parameters["sign"] = $this->getSign($this->parameters);//签名 return $this->arrayToXml($this->parameters); }catch (SDKRuntimeException $e) { die($e->errorMessage()); } } /*** 機能: 結果の取得と証明書を使用した通信*/ 関数 getResult() { $this->postXmlSSL(); $this->result = $this->xmlToArray($this->response); $this->結果を返す; } } /*** 返金クエリインターフェース*/ class RefundQuery_pub は Wxpay_client_pub を拡張します { 関数 __construct() { // インターフェイス接続を設定します $this->url = "https://api.mch.weixin.qq.com/pay/refundquery"; //カールを設定します超時間 $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT; } /*** インターフェースパラメータ XML を生成*/ 関数createXml() { 試す { if($this->parameters["out_refund_no"] == null && $this->parameters["out_trade_no"] == null && $this->parameters["transaction_id"] == null && $this->parameters["refund_id "] == null) { throw new SDKRuntimeException("退去条件查询インターフェイス中,out_refund_no、out_trade_no、transaction_id、refund_id四个パラメータ必須一个!"."
"); } $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号 $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串 $this->parameters["sign"] = $this->getSign($this->parameters);//签名 return $this->arrayToXml($this->parameters); }catch (SDKRuntimeException $e) { die($e->errorMessage()); } } /*** 機能: 結果の取得と証明書を使用した通信*/ 関数 getResult() { $this->postXmlSSL(); $this->result = $this->xmlToArray($this->response); $this->結果を返す; } } /*** 請求インターフェース*/ クラス DownloadBill_pub は Wxpay_client_pub を拡張します { 関数 __construct() { // インターフェイス接続を設定します $this->url = "https://api.mch.weixin.qq.com/pay/downloadbill"; //カールを設定します超時間 $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT; } /*** インターフェースパラメータ XML を生成*/ 関数createXml() { 試す { if($this->parameters["bill_date"] == null ) { throw new SDKRuntimeException("对账单接口中,缺少必装パラメータbill_date!"."
"); } $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号 $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串 $this->parameters["sign"] = $this->getSign($this->parameters);//签名 return $this->arrayToXml($this->parameters); }catch (SDKRuntimeException $e) { die($e->errorMessage()); } } /*** 機能: 結果を取得します。デフォルトでは証明書は使用されません*/ 関数 getResult() { $this->postXml(); $this->result = $this->xmlToArray($this->result_xml); $this->結果を返す; } } /*** ショートリンク変換インターフェース*/ クラス ShortUrl_pub は Wxpay_client_pub を拡張します { 関数 __construct() { // インターフェイス接続を設定します $this->url = "https://api.mch.weixin.qq.com/tools/shorturl"; //カールを設定します超時間 $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT; } /*** インターフェースパラメータ XML を生成*/ 関数createXml() { 試す { if($this->parameters["long_url"] == null ) { throw new SDKRuntimeException("短链接转换接口中,缺少必須充填パラメータlong_url!"."
"); } $this->parameters["appid"] = WxPayConf_pub::APPID;//パブリック アカウント ID $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//販売者 ID $this->parameters["nonce_str"] = $this->createNoncestr();//ランダムな文字列 $this->parameters["sign"] = $this->getSign($this->parameters);//署名 return $this->arrayToXml($this->parameters); }catch (SDKRuntimeException $e) { die($e->errorMessage()); } } /*** prepay_id を取得する*/ 関数 getShortUrl() { $this->postXml(); $prepay_id = $this->result["short_url"]; $prepay_id を返します; } } /***レスポンシブインターフェイスの基本クラス*/ クラス Wxpay_server_pub は Common_util_pub を拡張します { public $data;//受信データ、型は連想配列 var $returnParameters; // パラメータを返します。型は連想配列です。 /*** データ処理を容易にするために、WeChat のリクエスト XML を連想配列に変換します。*/ 関数 saveData($xml) { $this->data = $this->xmlToArray($xml); } 関数 checkSign() { $tmpData = $this->data; unset($tmpData['sign']); $sign = $this->getSign($tmpData);//ローカル署名 if ($this->data['sign'] == $sign) { TRUEを返します。 } FALSEを返します。 } /*** WeChatリクエストデータの取得*/ 関数 getData() { $this->データを返す; } /*** WeChatに返されるxmlデータを設定します*/ 関数 setReturnParameter($parameter, $parameterValue) { $this->returnParameters[$this->trimString($parameter)] = $this->trimString($parameterValue); } /*** インターフェースパラメータ XML を生成*/ 関数createXml() { return $this->arrayToXml($this->returnParameters); } /*** XMLデータをWeChatに返す*/ 関数 returnXml() { $returnXml = $this->createXml(); $returnXml を返します。 } } /*** ユニバーサル通知インターフェース*/ クラス Notify_pub は Wxpay_server_pub を拡張します { } /*** 加盟店に製品情報インターフェースの取得を要求します*/ クラス NativeCall_pub は Wxpay_server_pub を拡張します { /*** インターフェースパラメータ XML を生成*/ 関数createXml() { if($this->returnParameters["return_code"] == "SUCCESS"){ $this->returnParameters["appid"] = WxPayConf_pub::APPID;//パブリック アカウント ID $this->returnParameters["mch_id"] = WxPayConf_pub::MCHID;//販売者番号 $this->returnParameters["nonce_str"] = $this->createNoncestr();//ランダムな文字列 $this->returnParameters["sign"] = $this->getSign($this->returnParameters);//署名 } return $this->arrayToXml($this->returnParameters); } /*** product_id を取得します*/ 関数 getProductId() { $product_id = $this->data["product_id"]; $product_id を返します; } } /*** 静的リンク QR コード*/ クラス NativeLink_pub は Common_util_pub を拡張します { var $parameters; //静的リンクパラメータ var $url;//静的リンク function__construct() { } /***パラメータの設定*/ 関数 setParameter($parameter, $parameterValue) { $this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue); } /*** ネイティブ支払いリンク QR コードを生成します*/ 関数createLink() { 試す { if($this->parameters["product_id"] == null) { throw new SDKRuntimeException("ネイティブ支払い QR コード リンクの必須パラメータ product_id がありません!"."
"); } $this->parameters["appid"] = WxPayConf_pub::APPID;//パブリック アカウント ID $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//販売者 ID $time_stamp = 時間(); $this->parameters["time_stamp"] = "$time_stamp";//タイムスタンプ $this->parameters["nonce_str"] = $this->createNoncestr();//ランダムな文字列 $this->parameters["sign"] = $this->getSign($this->parameters);//署名 $bizString = $this->formatBizQueryParaMap($this->パラメータ, false); $this->url = "weixin://wxpay/bizpayurl?".$bizString; }catch (SDKRuntimeException $e) { die($e->errorMessage()); } } /*** リターンリンク*/ 関数getUrl() { $this->createLink(); $this->urlを返します; } } /*** JSAPI 支払い - H5 Web ページが支払いインターフェイスを呼び出します*/ クラス JsApi_pub は Common_util_pub を拡張します { var $code;//openid を取得するコードを記述します var $openid;//ユーザーのopenid var $parameters;//jsapi パラメータ、形式は json var $prepay_id;// 統合された支払いインターフェースを使用して取得された前払い ID var $curl_timeout;//カールのタイムアウト時間 function__construct() { //カールのタイムアウトを設定する $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT; } /*** 機能: コードを取得できる URL を生成します。*/ 関数 createOauthUrlForCode($redirectUrl) { $urlObj["appid"] = WxPayConf_pub::APPID; $urlObj["redirect_uri"] = "$redirectUrl"; $urlObj["応答タイプ"] = "コード"; $urlObj["スコープ"] = "snsapi_base"; $urlObj["状態"] = "状態"."#wechat_redirect"; $bizString = $this->formatBizQueryParaMap($urlObj, false); return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString; } /*** 機能: openidを取得できるURLを生成*/ 関数 createOauthUrlForOpenid() { $urlObj["appid"] = WxPayConf_pub::APPID; $urlObj["シークレット"] = WxPayConf_pub::APPSECRET; $urlObj["コード"] = $this->コード; $urlObj["認可タイプ"] = "認可コード"; $bizString = $this->formatBizQueryParaMap($urlObj, false); return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString; } /*** 機能: openid を取得するために、curl を通じてコードを WeChat に送信します。*/ 関数 getOpenid() { $url = $this->createOauthUrlForOpenid(); //カールを初期化する $ch =curl_init(); //タイムアウトを設定する curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //curl を実行すると、結果が jason の形式で返されます。 $res =curl_exec($ch); カール_クローズ($ch); //openidを取得する $data = json_decode($res,true); $this->openid = $data['openid']; $this->openid を返す; } /*** 機能: prepay_id を設定します*/ 関数 setPrepayId($prepayId) { $this->prepay_id = $prepayId; } /*** 機能: コードを設定します。*/ 関数 setCode($code_) { $this->コード = $code_; } /*** 機能: jsapi のパラメータを設定します*/ パブリック関数 getParameters() { $jsApiObj["appId"] = WxPayConf_pub::APPID; $timeStamp = 時間(); $jsApiObj["タイムスタンプ"] = "$タイムスタンプ"; $jsApiObj["nonceStr"] = $this->createNoncestr(); $jsApiObj["パッケージ"] = "prepay_id=$this->prepay_id"; $jsApiObj["signType"] = "MD5"; $jsApiObj["paySign"] = $this->getSign($jsApiObj); $this->parameters = json_encode($jsApiObj); $this->パラメータを返す; } } /***現金赤い封筒インターフェイス * @作者gaoyl101*/ クラス Redpack_pub は Wxpay_client_pub を拡張します { var $code;//openid を取得するためのコードを記述します。 var $openid;//ユーザーのopenid function__construct() { //インターフェースリンクを設定する $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; //カールのタイムアウトを設定する $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT; } /*** インターフェースパラメータ XML を生成*/ 関数createXml() { 試す { //必要なパラメータを検出する if($this->parameters["mch_billno"] == null) { throw new SDKRuntimeException("赤い封筒送信インターフェイスに必要なパラメータ mch_billno がありません!"."
"); }elseif($this->parameters["nick_name"] == null){ throw new SDKRuntimeException("赤い封筒の送信インターフェイスに必要なパラメーター nick_name がありません!"."
"); }elseif ($this->parameters["send_name"] == null ) { throw new SDKRuntimeException("赤い封筒の送信インターフェイスに必要なパラメーター send_name がありません!"."
"); }elseif ($this->parameters["total_amount"] == null) { throw new SDKRuntimeException("赤い封筒送信インターフェイスに必要なパラメータ total_amount がありません!"."
"); }elseif($this->parameters["min_value"] == null){ throw new SDKRuntimeException("赤い封筒送信インターフェイスに必要なパラメーター min_value がありません!"."
"); }elseif ($this->parameters["max_value"] == null ) { throw new SDKRuntimeException("赤い封筒の送信インターフェイスに必要な max_value パラメーターがありません!"."
"); }elseif ($this->parameters["total_num"] == null) { throw new SDKRuntimeException("赤い封筒送信インターフェイスに必要なパラメーター total_num が見つかりません!"."
"); }elseif ($this->parameters["wishing"] == null) { throw new SDKRuntimeException("赤い封筒送信インターフェイスに必要な必須パラメータがありません!"."
"); }elseif ($this->parameters["act_name"] == null) { throw new SDKRuntimeException("赤い封筒送信インターフェイスの必須パラメータ act_name がありません!"."
"); }elseif ($this->parameters["remark"] == null) { throw new SDKRuntimeException("赤い封筒送信インターフェイスに必要なパラメータの注釈がありません!"."
"); } $this->parameters["wxappid"] = WxPayConf_pub::APPID;//パブリックアカウントID $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//販売者 ID $this->parameters["client_ip"] = $_SERVER['REMOTE_ADDR'];//ターミナルIP $this->parameters["nonce_str"] = $this->createNoncestr();//ランダムな文字列 $this->parameters["re_openid"] = $this->parameters["re_openid"]; //$this->openid;//ユーザーのopenid $this->parameters["sign"] = $this->getSign($this->parameters);//署名 return $this->arrayToXml($this->parameters); }catch (SDKRuntimeException $e) { die($e->errorMessage()); } } 関数 sendRedpack() { $this->postXmlSSL(); $this->result = $this->xmlToArray($this->response); $this->結果を返す; } /*** 機能: コードを取得できる URL を生成します。*/ 関数 createOauthUrlForCode($redirectUrl) { $urlObj["appid"] = WxPayConf_pub::APPID; $urlObj["redirect_uri"] = "$redirectUrl"; $urlObj["応答タイプ"] = "コード"; $urlObj["スコープ"] = "snsapi_base"; $urlObj["状態"] = "状態"."#wechat_redirect"; $bizString = $this->formatBizQueryParaMap($urlObj, false); return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString; } /*** 機能: openidを取得できるURLを生成*/ 関数 createOauthUrlForOpenid() { $urlObj["appid"] = WxPayConf_pub::APPID; $urlObj["シークレット"] = WxPayConf_pub::APPSECRET; $urlObj["コード"] = $this->コード; $urlObj["認可タイプ"] = "認可コード"; $bizString = $this->formatBizQueryParaMap($urlObj, false); return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString; } /*** 機能:curl を介してコードを WeChat に送信し、openid を取得します。*/ 関数 getOpenid() { $url = $this->createOauthUrlForOpenid(); //カールを初期化する $ch =curl_init(); //タイムアウトを設定する curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //curl を実行すると、結果が jason の形式で返されます。 $res =curl_exec($ch); カール_クローズ($ch); //openidを取得する $data = json_decode($res,true); $this->openid = $data['openid']; $this->openid を返す; } /*** 機能: コードを設定します。*/ 関数 setCode($code_) { $this->コード = $code_; } } /*** 赤い封筒の支払いインターフェース * @作者gaoyl101*/ クラス Groupredpack_pub は Wxpay_client_pub を拡張します { var $code;//openid を取得するためのコードを記述します。 var $openid;//ユーザーのopenid function__construct() { //インターフェースリンクを設定する $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack"; //カールのタイムアウトを設定する $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT; } /*** インターフェースパラメータ XML を生成*/ 関数createXml() { 試す { //必要なパラメータを検出する if($this->parameters["mch_billno"] == null) { throw new SDKRuntimeException("赤い封筒送信インターフェイスに必要なパラメータ mch_billno がありません!"."
"); }elseif ($this->parameters["send_name"] == null ) { throw new SDKRuntimeException("赤い封筒の送信インターフェイスに必要なパラメーター send_name がありません!"."
"); }elseif ($this->parameters["total_amount"] == null) { throw new SDKRuntimeException("赤い封筒送信インターフェイスに必要なパラメーター total_amount が見つかりません!"."
"); }elseif ($this->parameters["total_num"] == null) { throw new SDKRuntimeException("赤い封筒送信インターフェイスに必要なパラメータ total_num がありません!"."
"); }elseif ($this->parameters["amt_type"] == null) { throw new SDKRuntimeException("赤い封筒送信インターフェイスに必要なパラメータ amt_type が見つかりません!"."
"); }elseif ($this->parameters["wishing"] == null) { throw new SDKRuntimeException("赤い封筒の送信インターフェイスに必要な必須パラメータがありません!"."
"); }elseif ($this->parameters["act_name"] == null) { throw new SDKRuntimeException("赤い封筒送信インターフェイスの必須パラメータ act_name がありません!"."
"); }elseif ($this->parameters["remark"] == null) { throw new SDKRuntimeException("赤い封筒の送信インターフェイスに必要なパラメータの注釈がありません!"."
"); } $this->parameters["wxappid"] = WxPayConf_pub::APPID;//パブリックアカウントID $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//販売者 ID $this->parameters["nonce_str"] = $this->createNoncestr();//ランダムな文字列 $this->parameters["re_openid"] = $this->openid;//ユーザーopenid $this->parameters["sign"] = $this->getSign($this->parameters);//署名 return $this->arrayToXml($this->parameters); }catch (SDKRuntimeException $e) { die($e->errorMessage()); } } 関数 sendRedpack() { $this->postXmlSSL(); $this->result = $this->xmlToArray($this->response); $this->結果を返す; } /*** 機能: コードを取得できる URL を生成します。*/ 関数 createOauthUrlForCode($redirectUrl) { $urlObj["appid"] = WxPayConf_pub::APPID; $urlObj["redirect_uri"] = "$redirectUrl"; $urlObj["応答タイプ"] = "コード"; $urlObj["スコープ"] = "snsapi_base"; $urlObj["状態"] = "状態"."#wechat_redirect"; $bizString = $this->formatBizQueryParaMap($urlObj, false); return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString; } /*** 機能: openidを取得できるURLを生成*/ 関数 createOauthUrlForOpenid() { $urlObj["appid"] = WxPayConf_pub::APPID; $urlObj["シークレット"] = WxPayConf_pub::APPSECRET; $urlObj["コード"] = $this->コード; $urlObj["認可タイプ"] = "認可コード"; $bizString = $this->formatBizQueryParaMap($urlObj, false); return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString; } /*** 機能:curl を介してコードを WeChat に送信し、openid を取得します。*/ 関数 getOpenid() { $url = $this->createOauthUrlForOpenid(); //カールを初期化する $ch =curl_init(); //タイムアウトを設定する curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //curl を実行すると、結果が jason の形式で返されます。 $res =curl_exec($ch); カール_クローズ($ch); //openidを取得する $data = json_decode($res,true); $this->openid = $data['openid']; $this->openid を返す; } /*** 機能: コードを設定します。*/ 関数 setCode($code_) { $this->コード = $code_; } } /**※赤い封筒を振って注文してください * @作者じおせん*/ クラス Yhb_pub は Wxpay_client_pub を拡張します { var $code;//openid を取得するコードを記述します var $openid;//ユーザーのopenid function__construct() { //インターフェースリンクを設定する $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder"; //カールのタイムアウトを設定する $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT; } /*** インターフェースパラメータ XML を生成*/ 関数createXml() { 試す { //必要なパラメータを検出する if($this->parameters["mch_billno"] == null) { throw new SDKRuntimeException("赤い封筒送信インターフェイスに必要なパラメータ mch_billno がありません!"."
"); }elseif ($this->parameters["send_name"] == null ) { throw new SDKRuntimeException("赤い封筒の送信インターフェイスに必要なパラメーター send_name がありません!"."
"); }elseif ($this->parameters["total_amount"] == null) { throw new SDKRuntimeException("赤い封筒送信インターフェイスに必要なパラメーター total_amount が見つかりません!"."
"); }elseif ($this->parameters["total_num"] == null) { throw new SDKRuntimeException("赤い封筒送信インターフェイスに必要なパラメータ total_num が見つかりません!"."
"); }elseif ($this->parameters["wishing"] == null) { throw new SDKRuntimeException("赤い封筒の送信インターフェイスに必要な必須パラメータがありません!"."
"); }elseif ($this->parameters["act_name"] == null) { throw new SDKRuntimeException("赤い封筒送信インターフェイスの必須パラメータ act_name がありません!"."
"); }elseif ($this->parameters["remark"] == null) { throw new SDKRuntimeException("赤い封筒の送信インターフェイスに必要なパラメータの注釈がありません!"."
ログイン後にコピー
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!