Wxjspay_payment.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EaseTHINK 易想团购系统
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2010 http://www.easethink.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. $payment_lang = array(
  8. 'name' => '微信支付(PC扫码支付)',
  9. 'appid' => '微信公众号ID',
  10. 'appsecret' => '微信公众号SECRT',
  11. 'mchid' => '微信支付MCHID',
  12. // 'partnerid' => '商户ID',
  13. //'partnerkey' => '商户key',
  14. 'key' => '商户支付密钥Key/api秘钥',
  15. //'sslcert'=>'apiclient_cert证书路径',
  16. //'sslkey'=>'apiclient_key证书路径',
  17. );
  18. $config = array(
  19. 'appid' => array(
  20. 'INPUT_TYPE' => '0',
  21. ), //微信公众号ID
  22. 'appsecret' => array(
  23. 'INPUT_TYPE' => '0',
  24. ), //微信公众号SECRT
  25. 'mchid' => array(
  26. 'INPUT_TYPE' => '0',
  27. ), //微信支付MCHID
  28. 'key' => array(
  29. 'INPUT_TYPE' => '0',
  30. ), //商户支付密钥Key
  31. // 'sslcert' => array(
  32. // 'INPUT_TYPE' => '0',
  33. // ), //apiclient_cert证书路径
  34. // 'sslkey' => array(
  35. // 'INPUT_TYPE' => '0',
  36. // ), //apiclient_key证书路径
  37. );
  38. /* 模块的基本信息 */
  39. if (isset($read_modules) && $read_modules == true) {
  40. $module['class_name'] = 'Wxjspay';
  41. /* 名称 */
  42. $module['name'] = $payment_lang['name'];
  43. /* 支付方式:1:在线支付;0:线下支付;2:手机wap;3:手机sdk */
  44. $module['online_pay'] = '1';
  45. /* 配送 */
  46. $module['config'] = $config;
  47. $module['lang'] = $payment_lang;
  48. $module['reg_url'] = '';
  49. return $module;
  50. }
  51. // 支付宝手机支付模型
  52. require_once APP_ROOT_PATH . 'system/libs/payment.php';
  53. class Wxjspay_payment implements payment
  54. {
  55. public function __construct()
  56. {
  57. require_once APP_ROOT_PATH . "system/payment/Wxjspay/WxPayPubHelper.php";
  58. }
  59. /**
  60. * 获取登录信息
  61. * @return [type] [description]
  62. */
  63. protected static function getUserId()
  64. {
  65. $user_info = es_session::get("user_info");
  66. if (!$user_info) {
  67. ajax_return(array(
  68. 'status' => 0,
  69. 'info' => '未登录',
  70. ));
  71. }
  72. return $user_info['id'];
  73. }
  74. /**
  75. * 根据订单id获取订单
  76. * @param [type] $payment_notice_id [description]
  77. * @param string $field [description]
  78. * @return [type] [description]
  79. */
  80. protected static function getPaymentNotice($payment_notice_id, $field = '*')
  81. {
  82. $table = DB_PREFIX . 'payment_notice';
  83. $where = 'id =' . intval($payment_notice_id);
  84. return $GLOBALS['db']->getRow("SELECT * FROM $table WHERE $where");
  85. }
  86. /**
  87. * 根据订单SN获取订单
  88. * @param [type] $sn [description]
  89. * @param string $field [description]
  90. * @return [type] [description]
  91. */
  92. protected static function getPaymentNoticeBySN($sn, $field = '*')
  93. {
  94. $table = DB_PREFIX . 'payment_notice';
  95. $where = "`notice_sn` ='$sn'";
  96. return $GLOBALS['db']->getRow("SELECT * FROM $table WHERE $where");
  97. }
  98. /**
  99. * 获取配置信息
  100. * @param [type] $payment_id [description]
  101. * @param string $field [description]
  102. * @return [type] [description]
  103. */
  104. protected static function getPaymentConfig($payment_id, $field = '*')
  105. {
  106. $table = DB_PREFIX . 'payment';
  107. $where = 'id =' . intval($payment_id);
  108. $payment_info = $GLOBALS['db']->getRow("SELECT `config` FROM $table WHERE $where");
  109. return unserialize($payment_info['config']);
  110. }
  111. /**
  112. * 统一下单
  113. * @param [type] $payment_notice_id [description]
  114. * @return [type] [description]
  115. */
  116. public function get_payment_code($payment_notice_id)
  117. {
  118. $field = '`notice_sn`,`money`,`payment_id`,`order_id`,`num`,`order_status`';
  119. $payment_notice = self::getPaymentNotice(intval($payment_notice_id), $field);
  120. $order_sn = $payment_notice['notice_sn'];
  121. $money = round($payment_notice['money'], 2) * 100;
  122. $notify_url = "http://live.huangjiaxiuchang.com/wxpay_web/notify_url.php";
  123. $wx_config = self::getPaymentConfig($payment_notice['payment_id']);
  124. $unifiedOrder = new UnifiedOrder_pub();
  125. $unifiedOrder->update_config(
  126. $wx_config['appid'],
  127. $wx_config['appsecret'],
  128. $wx_config['mchid'],
  129. $wx_config['partnerid'],
  130. $wx_config['partnerkey'],
  131. $wx_config['key'],
  132. $wx_config['sslcert'],
  133. $wx_config['sslkey']
  134. );
  135. require_once APP_ROOT_PATH . 'system/extend/ip.php';
  136. $iplocation = new iplocate();
  137. $unifiedOrder->setParameter('spbill_create_ip', $iplocation->getIP());
  138. $unifiedOrder->setParameter('out_trade_no', $order_sn);
  139. $unifiedOrder->setParameter('total_fee', $money);
  140. $unifiedOrder->setParameter('notify_url', $notify_url);
  141. $unifiedOrder->setParameter('body', '充值');
  142. $unifiedOrder->setParameter('trade_type', 'NATIVE');
  143. $result = $unifiedOrder->getResult();
  144. if ($result['return_code'] == 'SUCCESS') {
  145. if ($result['result_code'] == 'SUCCESS') {
  146. $data_info['code_url'] = $result['code_url'];
  147. return array(
  148. 'status' => 1,
  149. 'code_url' => $result['code_url'],
  150. 'method' => 'img',
  151. 'id' => $payment_notice_id,
  152. );
  153. } else {
  154. $error_info = $result['err_code_des'] . ',原因:';
  155. switch ($result['err_code']) {
  156. case 'NOAUTH':
  157. $error_info .= '请商户前往申请此接口权限';
  158. break;
  159. case 'NOTENOUGH':
  160. $error_info .= '用户帐号余额不足,请用户充值或更换支付卡后再支付';
  161. break;
  162. case 'ORDERPAID':
  163. $error_info .= '商户订单已支付,无需更多操作';
  164. break;
  165. case 'ORDERCLOSED':
  166. $error_info .= '当前订单已关闭,请重新下单';
  167. break;
  168. case 'SYSTEMERROR':
  169. $error_info .= '系统异常,请用相同参数重新调用单';
  170. break;
  171. case 'APPID_NOT_EXIST':
  172. $error_info .= '请检查APPID是否正确';
  173. break;
  174. case 'MCHID_NOT_EXIST':
  175. $error_info .= '请检查MCHID是否正确';
  176. break;
  177. case 'APPID_MCHID_NOT_MATCH':
  178. $error_info .= '请确认appid和mch_id是否匹配';
  179. break;
  180. case 'LACK_PARAMS':
  181. $error_info .= '请检查参数是否齐全';
  182. break;
  183. case 'OUT_TRADE_NO_USED':
  184. $error_info .= '请核实商户订单号是否重复提交';
  185. break;
  186. case 'SIGNERROR':
  187. $error_info .= '请检查签名参数和方法是否都符合签名算法要求';
  188. break;
  189. case 'XML_FORMAT_ERROR':
  190. $error_info .= '请检查XML参数格式是否正确';
  191. break;
  192. case 'REQUIRE_POST_METHOD':
  193. $error_info .= '请检查请求参数是否通过post方法提交';
  194. break;
  195. case 'POST_DATA_EMPTY':
  196. $error_info .= '请检查post数据是否为空';
  197. break;
  198. case 'NOT_UTF8':
  199. $error_info .= '请使用NOT_UTF8编码格式';
  200. break;
  201. default:
  202. $error_info = '';
  203. }
  204. return array(
  205. 'status' => 0,
  206. 'info' => $error_info,
  207. );
  208. }
  209. } else {
  210. return array(
  211. 'status' => 0,
  212. 'info' => $result['return_msg'],
  213. );
  214. }
  215. }
  216. /**
  217. * 扫码反馈
  218. * @param [type] $request [description]
  219. * @return [type] [description]
  220. */
  221. public function response($request)
  222. {
  223. $payment_notice_id = intval($request['payment_notice_id']);
  224. if (!$payment_notice_id) {
  225. ajax_return(array(
  226. 'status' => 0,
  227. 'info' => '参数错误',
  228. ));
  229. }
  230. $user_id = self::getUserId();
  231. $payment_notice = self::getPaymentNotice($payment_notice_id, '`notice_sn`,`payment_id`,`user_id`');
  232. if ($user_id != $payment_notice['user_id']) {
  233. ajax_return(array(
  234. 'status' => 0,
  235. 'info' => '用户订单信息错误',
  236. ));
  237. }
  238. $order_sn = $payment_notice['notice_sn'];
  239. $wx_config = self::getPaymentConfig($payment_notice['payment_id']);
  240. $order_query = new OrderQuery_pub();
  241. $order_query->update_config(
  242. $wx_config['appid'],
  243. $wx_config['appsecret'],
  244. $wx_config['mchid'],
  245. $wx_config['partnerid'],
  246. $wx_config['partnerkey'],
  247. $wx_config['key'],
  248. $wx_config['sslcert'],
  249. $wx_config['sslkey']
  250. );
  251. $order_query->setParameter('out_trade_no', $order_sn);
  252. $result = $order_query->getResult();
  253. if ($result['return_code'] == 'SUCCESS') {
  254. if ($result['result_code'] == 'SUCCESS') {
  255. ajax_return(array(
  256. 'status' => 1,
  257. 'info' => $result['trade_state'],
  258. // SUCCESS—支付成功
  259. // REFUND—转入退款
  260. // NOTPAY—未支付
  261. // CLOSED—已关闭
  262. // REVOKED—已撤销(刷卡支付)
  263. // USERPAYING--用户支付中
  264. // PAYERROR--支付失败(其他原因,如银行返回失败)
  265. ));
  266. } else {
  267. ajax_return(array(
  268. 'status' => $result['err_code'],
  269. 'info' => $result['err_code_des'],
  270. ));
  271. }
  272. } else {
  273. ajax_return(array(
  274. 'status' => $result['return_code'],
  275. 'info' => $result['return_msg'],
  276. ));
  277. }
  278. }
  279. /**
  280. * 支付通知
  281. * @param [type] $request [description]
  282. * @return [type] [description]
  283. */
  284. public function notify($request)
  285. {
  286. $payment_notice = self::getPaymentNoticeBySN($request['out_trade_no'], '`payment_id`');
  287. $wx_config = self::getPaymentConfig($payment_notice['payment_id']);
  288. $notify = new Notify_pub();
  289. $notify->setReturnParameter('return_code', 'FAIL');
  290. if (!$wx_config) {
  291. $notify->setReturnParameter('return_msg', '配置错误');
  292. die($notify->returnXml());
  293. }
  294. $notify->update_config(
  295. $wx_config['appid'],
  296. $wx_config['appsecret'],
  297. $wx_config['mchid'],
  298. $wx_config['partnerid'],
  299. $wx_config['partnerkey'],
  300. $wx_config['key'],
  301. $wx_config['sslcert'],
  302. $wx_config['sslkey']
  303. );
  304. $notify->setData($request);
  305. if (!$notify->checkSign()) {
  306. $notify->setReturnParameter('return_msg', '签名失败');
  307. die($notify->returnXml());
  308. }
  309. require_once APP_ROOT_PATH . "system/libs/cart.php";
  310. $rs = payment_paid($request['out_trade_no'], $request['trade_no']);
  311. $notify->setReturnParameter('return_code', 'SUCCESS');
  312. die($notify->returnXml());
  313. }
  314. /**
  315. * 展示代码
  316. * @return [type] [description]
  317. */
  318. public function get_display_code()
  319. {
  320. }
  321. }