wechat_jspay_unifiedorder.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. class wechat_jspay_unifiedorder
  3. {
  4. public $appid;
  5. public $mch_id;
  6. public $key;
  7. public $notify_url;
  8. public $request_url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
  9. public $sign;
  10. public $nonce_str;
  11. public function __construct($conf)
  12. {
  13. $this->appid = $conf['appid'];
  14. $this->mch_id = $conf['payuser'];
  15. $this->key = $conf['paykey'];
  16. }
  17. public function requestOrder($attach, $body, $nonce_str, $out_trade_no, $total_fee, $trade_type, $notify_url, $openid, $sign = '')
  18. {
  19. $param['appid'] = $this->appid;
  20. $param['attach'] = $attach;
  21. $param['body'] = $body;
  22. $param['mch_id'] = $this->mch_id;
  23. $param['nonce_str'] = $nonce_str;
  24. $param['notify_url'] = $notify_url;
  25. $param['out_trade_no'] = $out_trade_no;
  26. $param['total_fee'] = $total_fee;
  27. $param['trade_type'] = $trade_type;
  28. $param['openid'] = $openid;
  29. $sign = $this->getSign($param);
  30. $this->sign = $sign;
  31. $this->nonce_str = $nonce_str;
  32. $param['sign'] = $sign;
  33. $data = $this->getXml($param);
  34. return $this->postXml($data);
  35. }
  36. public function postXml($data)
  37. {
  38. $ch = curl_init();
  39. curl_setopt($ch, CURLOPT_URL, $this->request_url);
  40. curl_setopt($ch, CURLOPT_HEADER, 0);
  41. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  42. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  43. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  44. curl_setopt($ch, CURLOPT_POST, true);
  45. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  46. $return = curl_exec($ch);
  47. curl_close($ch);
  48. return $return;
  49. }
  50. public function getSign($array)
  51. {
  52. ksort($array);
  53. $strArr = array();
  54. foreach ($array as $key => $val) {
  55. array_push($strArr, $key . '=' . $val);
  56. }
  57. $string = implode('&', $strArr);
  58. $sign = $string . '&key=' . $this->key;
  59. return md5($sign);
  60. }
  61. public function getXml($array)
  62. {
  63. $string = '';
  64. foreach ($array as $key => $val) {
  65. $string .= '<' . $key . '>' . $val . '</' . $key . '>';
  66. }
  67. return '<xml>' . $string . '</xml>';
  68. }
  69. }
  70. !defined('IN_MYMPS') && exit('ACCESS DENIED!');
  71. ?>