Crypt.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/WeChatDeveloper
  12. // +----------------------------------------------------------------------
  13. namespace WeMini;
  14. use WeChat\Contracts\BasicWeChat;
  15. use WeChat\Contracts\Tools;
  16. use WeChat\Exceptions\InvalidDecryptException;
  17. use WeChat\Exceptions\InvalidResponseException;
  18. /**
  19. * 数据加密处理
  20. * Class Crypt
  21. * @package WeMini
  22. */
  23. class Crypt extends BasicWeChat
  24. {
  25. /**
  26. * 数据签名校验
  27. * @param string $iv
  28. * @param string $sessionKey
  29. * @param string $encryptedData
  30. * @return bool
  31. */
  32. public function decode($iv, $sessionKey, $encryptedData)
  33. {
  34. require_once __DIR__ . DIRECTORY_SEPARATOR . 'crypt' . DIRECTORY_SEPARATOR . 'wxBizDataCrypt.php';
  35. $pc = new \WXBizDataCrypt($this->config->get('appid'), $sessionKey);
  36. $errCode = $pc->decryptData($encryptedData, $iv, $data);
  37. if ($errCode == 0) {
  38. return json_decode($data, true);
  39. }
  40. return false;
  41. }
  42. /**
  43. * 登录凭证校验
  44. * @param string $code 登录时获取的 code
  45. * @return array
  46. * @throws \WeChat\Exceptions\LocalCacheException
  47. */
  48. public function session($code)
  49. {
  50. $appid = $this->config->get('appid');
  51. $secret = $this->config->get('appsecret');
  52. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
  53. return json_decode(Tools::get($url), true);
  54. }
  55. /**
  56. * 换取用户信息
  57. * @param string $code 用户登录凭证(有效期五分钟)
  58. * @param string $iv 加密算法的初始向量
  59. * @param string $encryptedData 加密数据( encryptedData )
  60. * @return array
  61. * @throws InvalidDecryptException
  62. * @throws InvalidResponseException
  63. * @throws \WeChat\Exceptions\LocalCacheException
  64. */
  65. public function userInfo($code, $iv, $encryptedData)
  66. {
  67. $result = $this->session($code);
  68. if (empty($result['session_key'])) {
  69. throw new InvalidResponseException('Code 换取 SessionKey 失败', 403);
  70. }
  71. $userinfo = $this->decode($iv, $result['session_key'], $encryptedData);
  72. if (empty($userinfo)) {
  73. throw new InvalidDecryptException('用户信息解析失败', 403);
  74. }
  75. return array_merge($result, $userinfo);
  76. }
  77. /**
  78. * 用户支付完成后,获取该用户的 UnionId
  79. * @param string $openid 支付用户唯一标识
  80. * @param null|string $transaction_id 微信支付订单号
  81. * @param null|string $mch_id 微信支付分配的商户号,和商户订单号配合使用
  82. * @param null|string $out_trade_no 微信支付商户订单号,和商户号配合使用
  83. * @return array
  84. * @throws \WeChat\Exceptions\InvalidResponseException
  85. * @throws \WeChat\Exceptions\LocalCacheException
  86. */
  87. public function getPaidUnionId($openid, $transaction_id = null, $mch_id = null, $out_trade_no = null)
  88. {
  89. $url = "https://api.weixin.qq.com/wxa/getpaidunionid?access_token=ACCESS_TOKEN&openid={$openid}";
  90. if (is_null($mch_id)) $url .= "&mch_id={$mch_id}";
  91. if (is_null($out_trade_no)) $url .= "&out_trade_no={$out_trade_no}";
  92. if (is_null($transaction_id)) $url .= "&transaction_id={$transaction_id}";
  93. $this->registerApi($url, __FUNCTION__, func_get_args());
  94. return $this->callGetApi($url);
  95. }
  96. }