wechat_jspay.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. class wechat_jspay
  3. {
  4. protected $attach;
  5. protected $body;
  6. protected $nonce_str;
  7. protected $out_trade_no;
  8. protected $total_fee;
  9. protected $trade_type;
  10. protected $notify_url;
  11. public function __construct($config = NULL)
  12. {
  13. $this->attach = $this->getRand(10) . time();
  14. $this->body = $config['title'];
  15. $this->nonce_str = $this->getRand(32);
  16. $this->out_trade_no = $config['code'];
  17. $this->total_fee = $config['money'];
  18. $this->trade_type = 'JSAPI';
  19. $this->notify_url = $config['NotifyUrl'];
  20. $this->openid = $config['openid'];
  21. $this->appid = $config['appid'];
  22. $this->appsecret = $config['appsecret'];
  23. $this->payuser = $config['payuser'];
  24. $this->paykey = $config['paykey'];
  25. }
  26. public function getRand($length = 4)
  27. {
  28. $length = $length < 1 ? 1 : $length;
  29. $source = 'abcdefghijklmnopqrstuvwxyz0123456789';
  30. $arr = str_split($source);
  31. $rand_str = '';
  32. $rand_count = count($arr) - 1;
  33. for ($i = 0; $i < $length; $i++) {
  34. $rand_str .= $arr[rand(0, $rand_count)];
  35. }
  36. return strtoupper($rand_str);
  37. }
  38. public function sendPay()
  39. {
  40. require_once 'wechat_jspay_unifiedorder.php';
  41. $conf['appid'] = $this->appid;
  42. $conf['payuser'] = $this->payuser;
  43. $conf['paykey'] = $this->paykey;
  44. $unifiedorder = new wechat_jspay_unifiedorder($conf);
  45. $data = $unifiedorder->requestOrder($this->attach, $this->body, $this->nonce_str, $this->out_trade_no, $this->total_fee, $this->trade_type, $this->notify_url, $this->openid);
  46. $xml = simplexml_load_string($data);
  47. if ($xml->return_code == 'SUCCESS' && $xml->result_code == 'SUCCESS') {
  48. $appId = $this->appid;
  49. $timestamp = time();
  50. $noncestr = $this->nonce_str;
  51. $package = 'prepay_id=' . $xml->prepay_id;
  52. $signType = 'MD5';
  53. $params = array('appId' => $appId, 'timeStamp' => $timestamp, 'nonceStr' => $noncestr, 'package' => $package, 'signType' => $signType);
  54. $params['paySign'] = $this->getPaySign(array('appId' => $params['appId'], 'timeStamp' => $params['timeStamp'], 'nonceStr' => $params['nonceStr'], 'package' => $params['package'], 'signType' => $params['signType']));
  55. return $params;
  56. }
  57. else {
  58. return false;
  59. }
  60. }
  61. protected function getJsapiTicket()
  62. {
  63. $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . getBaseAccessToken($this->appid, $this->appsecret) . '&type=jsapi';
  64. $data = json_decode(get_url_contents($url), true);
  65. if (isset($data['errcode']) && intval($data['errcode']) === 0) {
  66. return $data['ticket'];
  67. }
  68. else {
  69. return false;
  70. }
  71. }
  72. protected function getPaySign($params)
  73. {
  74. $string = $this->getSignString($params);
  75. return md5($string . '&key=' . $this->paykey);
  76. }
  77. protected function getSignString($params)
  78. {
  79. ksort($params);
  80. $new_params = array();
  81. foreach ($params as $key => $val) {
  82. array_push($new_params, $key . '=' . $val);
  83. }
  84. return implode('&', $new_params);
  85. }
  86. }
  87. !defined('IN_MYMPS') && exit('ACCESS DENIED!');
  88. ?>