微信支付 - 图1
    NotifyController.class.php是异步

    1. <?php
    2. namespace Home\Controller;
    3. use Think\Controller;
    4. use Think\Hook;
    5. class NotifyController extends Controller {
    6. //初始化
    7. public function _initialize()
    8. {
    9. vendor('WxPayPubHelper.WxPayPubHelper');
    10. }
    11. // 支付回调
    12. public function notify(){
    13. $log_name= "./Public/Pay/notify_url.log";//log文件路径
    14. $wechatinfo['appid'] = M('mp_setting')->where(array('mpid'=>3,'name'=>'appid'))->getField('value');
    15. $wechatinfo['mchid'] = M('mp_setting')->where(array('mpid'=>3,'name'=>'mchid'))->getField('value');
    16. $wechatinfo['key'] = M('mp_setting')->where(array('mpid'=>3,'name'=>'paysignkey'))->getField('value');
    17. $wechatinfo['appsecret'] = M('mp_setting')->where(array('mpid'=>3,'name'=>'appsecret'))->getField('value');
    18. //存储微信的回调
    19. $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
    20. //使用通用通知接口
    21. $notify = new \Notify_pub($wechatinfo);
    22. $notify->saveData($xml);
    23. //验证签名,并回应微信。
    24. //对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,
    25. //微信会通过一定的策略(如30分钟共8次)定期重新发起通知,
    26. //尽可能提高通知的成功率,但微信不保证通知最终能成功。
    27. if($notify->checkSign() == FALSE){
    28. $notify->setReturnParameter("return_code","FAIL");//返回状态码
    29. $notify->setReturnParameter("return_msg","签名失败");//返回信息
    30. }else{
    31. $notify->setReturnParameter("return_code","SUCCESS");//设置返回码
    32. }
    33. $returnXml = $notify->returnXml();
    34. if($notify->checkSign() == TRUE)
    35. {
    36. if (($notify->data["return_code"] == "SUCCESS")&&($notify->data["result_code"] == "SUCCESS")) {
    37. $where['number'] = $notify->data["out_trade_no"];
    38. $data['wx_number'] = $notify->data["transaction_id"];
    39. $data['deal_time'] = $notify->data["time_end"];
    40. $data['is_pay'] = 1;
    41. $data['access'] = 1;
    42. // 支付完成后清除session的所有信息
    43. session('Cart',null);
    44. // 清除总价
    45. session('total',null);
    46. // 清除数量
    47. session('num',null);
    48. // 商品的id
    49. session('shop_id',null);
    50. if(!D('order')->where($where)->save($data)){
    51. file_put_contents($log_name,"【支付成功 , 数据库修改失败信息】:\n".json_encode($notify->data)."\n", FILE_APPEND);
    52. }
    53. }else{
    54. file_put_contents($log_name,"【支付失败通知】:\n".json_encode($notify->data)."\n", FILE_APPEND);
    55. }
    56. }
    57. }
    58. }
    59. //WxpayController是支付方法
    60. <?php
    61. namespace Home\Controller;
    62. use Think\Controller;
    63. use Think\Hook;
    64. class WxpayController extends Controller {
    65. //初始化
    66. public function _initialize()
    67. {
    68. if(session('userinfo.openid')){
    69. vendor('WxPayPubHelper.WxPayPubHelper');
    70. }else{
    71. cookie('controller',CONTROLLER_NAME);
    72. cookie('action',ACTION_NAME);
    73. $this->redirect('Base/getsession');
    74. }
    75. }
    76. /*
    77. 前台给支付传一个id即可
    78. */
    79. // 统一支付
    80. public function pay(){
    81. if($_GET['orderid']){
    82. $orderinfo = D('order')->find($_GET['orderid']);
    83. if(($orderinfo['is_pay'] == 0) && ($orderinfo['status'] == 1)){
    84. $wechatinfo['appid'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'appid'))->getField('value');
    85. $wechatinfo['mchid'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'mchid'))->getField('value');
    86. $wechatinfo['key'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'paysignkey'))->getField('value');
    87. $wechatinfo['appsecret'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'appsecret'))->getField('value');
    88. //统一支付
    89. $unifiedOrder = new \UnifiedOrder_pub($wechatinfo);
    90. $unifiedOrder->setParameter("openid",session('userinfo.openid'));
    91. $unifiedOrder->setParameter("body",'谢谢光顾');
    92. $unifiedOrder->setParameter("out_trade_no",$orderinfo['number']);
    93. $unifiedOrder->setParameter("total_fee",$orderinfo['cash_fee']);
    94. $unifiedOrder->setParameter("notify_url", C('HTTP_HOST') . '/index.php/Home/Notify/notify');
    95. $unifiedOrder->setParameter("trade_type","JSAPI");
    96. $prepay_id = $unifiedOrder->getPrepayId();
    97. $jsApi = new \JsApi_pub($wechatinfo);
    98. $jsApi->setPrepayId($prepay_id);
    99. $jsApiParameters = $jsApi->getParameters();
    100. $this->assign('jsApiParameters',$jsApiParameters)->assign('money',$orderinfo['cash_fee']/1000);
    101. $this->display();
    102. }
    103. }
    104. }
    105. // 退款
    106. public function refundmoney() {
    107. if($_GET['orderid']){
    108. $orderinfo = D('order')->find($_GET['orderid']);
    109. // 订单状态必须是已支付,申请退款,订单未结束
    110. if(($orderinfo['refund'] == 1) && ($orderinfo['is_pay'] == 1) && ($orderinfo['status'] == 1)){
    111. // 查出商户信息
    112. $wechatinfo['appid'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'appid'))->getField('value');
    113. $wechatinfo['mchid'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'mchid'))->getField('value');
    114. $wechatinfo['key'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'paysignkey'))->getField('value');
    115. $wechatinfo['appsecret'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'appsecret'))->getField('value');
    116. $sslcert = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'sslcert'))->getField('value');
    117. $sslkey = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'sslkey'))->getField('value');
    118. $wechatinfo['sslcert_path'] = '/cert/apiclient_'.$orderinfo['mpid'].'_cert.pem';
    119. $wechatinfo['sslkey_path'] = '/cert/apiclient_'.$orderinfo['mpid'].'_key.pem';
    120. $dirpath = explode('\App',dirname(__FILE__));
    121. $sslcertpath = $dirpath[0].'\ThinkPHP\Library\Vendor\WxPayPubHelper\cert\apiclient_'.$orderinfo['mpid'].'_cert.pem';
    122. $sslkeypath = $dirpath[0].'\ThinkPHP\Library\Vendor\WxPayPubHelper\cert\apiclient_'.$orderinfo['mpid'].'_key.pem';
    123. file_put_contents($sslcertpath, isset($sslcert) ? $sslcert : '');
    124. file_put_contents($sslkeypath, isset($sslkey) ? $sslkey : '');
    125. // 退款
    126. $refundpay = new \Refund_pub($wechatinfo);
    127. $refundpay->setParameter('transaction_id', $orderinfo['wx_number']);
    128. $refundpay->setParameter('out_refund_no', $orderinfo['cash_fee'].$orderinfo['wx_number']);
    129. $refundpay->setParameter('total_fee', $orderinfo['total_fee']);
    130. $refundpay->setParameter('refund_fee', $orderinfo['cash_fee']);
    131. $refundpay->setParameter('op_user_id', '920518');
    132. $result = $refundpay->getResult();
    133. $log_name= "./Public/Pay/refund_url.log";//log文件路径
    134. // 成功
    135. if (isset($result['return_code']) && isset($result['result_code']) && $result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
    136. $data['id'] = $_GET['orderid'];
    137. $data['wxrefund_number'] = $result['refund_id'];
    138. $data['refund'] = 2;
    139. $data['status'] = 2;
    140. if(D('order')->save($data)){
    141. $return['return_code'] = 200;
    142. $return['return_msg'] = '退款成功';
    143. }else{
    144. file_put_contents($log_name,"【退款成功 , 数据库修改失败通知】:\n".json_encode($result)."\n", FILE_APPEND);
    145. $return['return_code'] = 201;
    146. $return['return_msg'] = '退款成功,但是数据库修改失败';
    147. }
    148. }else{
    149. file_put_contents($log_name,"【退款失败通知】:\n".json_encode($result)."\n", FILE_APPEND);
    150. $return['return_code'] = 400;
    151. $return['return_msg'] = '退款失败';
    152. }
    153. unlink($sslcertpath);
    154. unlink($sslkeypath);
    155. $this->ajaxReturn($return);
    156. }
    157. }
    158. }
    159. // 二维码支付
    160. public function qrcodepay(){
    161. if($_GET['orderid'] && is_int($_GET['orderid'])){
    162. $orderinfo = D('order')->find($_GET['orderid']);
    163. if(!$orderinfo){
    164. $return['return_code'] = 201;
    165. $return['return_msg'] = '订单不存在';
    166. }else{
    167. if(($orderinfo['is_pay'] == 0) && ($orderinfo['status'] == 1)){
    168. $wechatinfo['appid'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'appid'))->getField('value');
    169. $wechatinfo['mchid'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'mchid'))->getField('value');
    170. $wechatinfo['key'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'paysignkey'))->getField('value');
    171. $wechatinfo['appsecret'] = M('mp_setting')->where(array('mpid'=>$orderinfo['mpid'],'name'=>'appsecret'))->getField('value');
    172. //统一支付
    173. $unifiedOrder = new \UnifiedOrder_pub($wechatinfo);
    174. $unifiedOrder->setParameter("body",'谢谢光顾');
    175. $unifiedOrder->setParameter("out_trade_no",$orderinfo['number']);
    176. $unifiedOrder->setParameter("total_fee",$orderinfo['cash_fee']);
    177. $unifiedOrder->setParameter("notify_url", C('HTTP_HOST') . '/index.php/Home/Notify/notify');
    178. $unifiedOrder->setParameter("trade_type","NATIVE");
    179. $unifiedOrder->setParameter("product_id",$orderinfo['number']);
    180. $prepay_url = $unifiedOrder->getPrepayUrl();
    181. $data['qrcodeimage'] = 'http://paysdk.weixin.qq.com/example/qrcode.php?data='.urlencode($prepay_url);
    182. $return['return_code'] = 200;
    183. $return['return_msg'] = '订单二维码';
    184. $return['return_data'] = $data;
    185. $this->ajaxReturn($return);
    186. }else{
    187. if($orderinfo['is_pay'] == 1){
    188. $return['return_code'] = 201;
    189. $return['return_msg'] = '订单不可重复支付';
    190. }
    191. }
    192. }
    193. }else{
    194. $return['return_code'] = 400;
    195. $return['return_msg'] = '订单格式错误';
    196. }
    197. }
    198. public function qecodeindex(){
    199. $this->display();
    200. }
    201. // 创建订单
    202. public function createorder(){
    203. $money = I('post.money');
    204. $type = I('post.type');
    205. $mpid = I('post.mpid');
    206. $data['number'] = time('YmdHis').rand(100,999).session('userinfo.id');
    207. $data['total_fee'] = $money*100;
    208. $data['cash_fee'] = $money*100;
    209. $data['type'] = 1;
    210. $data['mpid'] = session('userinfo.mpid');
    211. $data['status'] = 1;
    212. $data['userid'] = session('userinfo.id');
    213. $orderid = D('order')->add($data);
    214. if($orderid){
    215. $order['id'] = $orderid;
    216. $return['return_code'] = 200;
    217. $return['return_msg'] = '添加成功';
    218. $return['return_data'] = $order;
    219. }else{
    220. $return['return_code'] = 400;
    221. $return['return_msg'] = '添加失败';
    222. }
    223. $this->ajaxReturn($return);
    224. }
    225. // 饭厅生成二维码
    226. public function getqrcode(){
    227. vendor('phpqrcode.qrcodecreate');
    228. $qrcodeimage = new \QRcodeCreate();
    229. $result = $qrcodeimage->projectqrcode();
    230. $QR = imagecreatefromgd(file_get_contents($result));
    231. //输出图片
    232. // imagepng($QR, 'qrcode.png');
    233. // imagedestroy($QR);
    234. // return '<img src="qrcode.png" alt="使用微信扫描支付">';
    235. file_put_contents('./Public/getqrcode',$QR);
    236. }
    237. }

    tp3.2.rar
    微信支付 - 图2
    存放微信支付扩展