You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

231 lines
7.6 KiB

  1. <?php
  2. /**
  3. *微信商城的支付
  4. * ============================================================================
  5. * * 版权所有 蜘蛛出行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 张帅
  12. * PhpStorm weChatShop.php
  13. * Create By 2016/11/21 11:55 $
  14. */
  15. header("Content-type:text/html;charset=utf-8");
  16. ini_set('date.timezone', 'Asia/Shanghai');
  17. //error_reporting(E_ERROR);
  18. require_once "../lib/WxPay.Api.php";
  19. require_once "WxPay.JsApiPay.php";
  20. require_once 'log.php';
  21. require_once '../../Common/Function.php';
  22. //支付url
  23. $pay_url = 'http://wx.zhizhuchuxing.com/wechat/WxPay/realpay/success_notify_wechat_shop.php';
  24. //url数组
  25. $url_arr = array(
  26. 'success' => 'http://wx.zhizhuchuxing.com/weixin/pay_success.html',
  27. 'fail' => 'http://wx.zhizhuchuxing.com/weixin/order_detail.html',
  28. 'order_detail' => 'http://wx.zhizhuchuxing.com/module/order/orderlist/getDetaile',
  29. 'update_order' => 'http://wx.zhizhuchuxing.com/module/order/order/updateOrderStatus'
  30. );
  31. //获取参数
  32. //获取订单号
  33. if (!empty($_GET['order_id']) && $_GET['order_id'] !== false) {
  34. $order_id = $_GET['order_id'];
  35. } else {
  36. echo "<script>alert('订单号有误')</script>";
  37. exit;
  38. }
  39. //初始化日志
  40. $logHandler = new CLogFileHandler("../logs/" . date('Y-m-d') . '.log');
  41. $log = Log::Init($logHandler, 15);
  42. //打印输出数组信息
  43. function printf_info($data)
  44. {
  45. foreach ($data as $key => $value) {
  46. echo "<font color='#00ff55;'>$key</font> : $value <br/>";
  47. }
  48. }
  49. //①、获取用户openid
  50. $tools = new JsApiPay();
  51. $open_id = $tools->GetOpenid();
  52. //支付
  53. $jsApiParameters = pay($order_id,$url_arr,$pay_url,$open_id,$tools);
  54. //打log方法
  55. function write_log1($result)
  56. {
  57. $dir = __DIR__ . '/Log/';
  58. if (!is_dir($dir)) {
  59. mkdir($dir);
  60. }
  61. $filename = $dir . date("Y-m-d") . ".log";
  62. file_put_contents($filename, date("Y-m-d H:i:s") . " " . $result . PHP_EOL, FILE_APPEND);//订单详细日志
  63. }
  64. //判断支付支付
  65. function CheckWeChatPay($order_id,$url_arr){
  66. $order_result = WeChatShopOrderDetail($order_id,$url_arr);
  67. //已支付,跳转支付成功页面
  68. if(!$order_result){
  69. header('Location:' . $url_arr['success'] . '?order_id=' . $order_id);
  70. }
  71. //未支付
  72. $price = (int)($order_result['price']*100);
  73. //测试价格
  74. //$price = 1;
  75. //如果价格为0,修改订单状态
  76. if($price == 0){
  77. //1.修改订单状态
  78. $data = array('order_id'=>$order_id);
  79. $update_order = http_request($url_arr['update_order'],$data);
  80. $update_order = json_decode($update_order,true);
  81. if(!$update_order['flag']){
  82. echo "<script>alert('网络错误')</script>";
  83. exit;
  84. }
  85. //2.跳转支付成功页面
  86. header('Location:' . $url_arr['success'] . '?order_id=' . $order_id);
  87. }
  88. $prod_name = $order_result['prod_name'];
  89. return array(
  90. 'order_id' => $order_result['order_id'],
  91. 'price' => $price,
  92. 'prod_name' => $prod_name
  93. );
  94. }
  95. //微信商城订单详情
  96. function WeChatShopOrderDetail($order_id,$url_arr){
  97. //获取订单详情
  98. $data = array('order_id'=>$order_id,
  99. 'code'=>md5('zhizhuchuxing')
  100. );
  101. $order_detail = http_request($url_arr['order_detail'],$data);
  102. $order_detail = json_decode($order_detail,true);
  103. //订单号无效
  104. if(!$order_detail['flag']){
  105. echo "<script>alert('订单号无效')</script>";
  106. exit;
  107. }
  108. //判断是否已支付
  109. $order_detail = $order_detail['data'];
  110. if($order_detail['order_status'] == 145){
  111. return array(
  112. 'order_id' => $order_detail['order_status'],
  113. 'price' => $order_detail['price'],
  114. 'prod_name' => $order_detail[0]['line_name']
  115. );
  116. }else{
  117. return false;
  118. }
  119. }
  120. //支付方法
  121. function pay($order_id,$url_arr,$pay_url,$open_id,$tools){
  122. //验证支付
  123. $order_info = CheckWeChatPay($order_id,$url_arr);
  124. //产品名
  125. $prod_name = $order_info['prod_name'];
  126. $price = $order_info['price'];
  127. //支付
  128. $input = new WxPayUnifiedOrder();
  129. $SetBody = $prod_name . ": " . $order_id;
  130. $input->SetBody($SetBody);
  131. $SetOut_trade_no = $order_id . "-" . date("ymdHis"); //支付串号(商户单号)
  132. $input->SetOut_trade_no($SetOut_trade_no);
  133. $input->SetTotal_fee($price);
  134. $SetTime_start = date("YmdHis");
  135. $input->SetTime_start($SetTime_start);
  136. $SetTime_expire = date("YmdHis", time() + 600);
  137. $input->SetTime_expire($SetTime_expire);
  138. $SetGoods_tag = "代金券";
  139. $input->SetGoods_tag($SetGoods_tag);
  140. //打Log
  141. write_log1($pay_url);
  142. $input->SetNotify_url($pay_url); //回掉地址
  143. $SetTrade_type = "JSAPI";
  144. $input->SetTrade_type($SetTrade_type);
  145. $input->SetOpenid($open_id);
  146. //打Log
  147. write_log1($SetBody . "^" . $SetOut_trade_no . "^" . $price . "^" . $SetTime_start . "^" . $SetTime_expire . "^" . $SetGoods_tag . "^" . $pay_url . "^" . $SetTrade_type . "^" . $open_id);
  148. $order = WxPayApi::unifiedOrder($input, 15);
  149. //printf_info($order);
  150. $jsApiParameters = $tools->GetJsApiParameters($order);
  151. //print_r($jsApiParameters);
  152. //获取共享收货地址js函数参数
  153. $editAddress = $tools->GetEditAddressParameters();
  154. //打Log
  155. write_log1(json_encode($jsApiParameters));
  156. return $jsApiParameters;
  157. }
  158. ?>
  159. <script type="text/javascript">
  160. var order_id = <?php echo $order_id;?>;
  161. var success_url = '<?php echo $url_arr['success']; ?>';
  162. var fail_url = '<?php echo $url_arr['fail']; ?>';
  163. var update_order = '<?php echo $url_arr['update_order']; ?>';
  164. window.onload = function () {
  165. if (typeof WeixinJSBridge == "undefined") {
  166. if (document.addEventListener) {
  167. document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
  168. } else if (document.attachEvent) {
  169. document.attachEvent('WeixinJSBridgeReady', jsApiCall);
  170. document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
  171. }
  172. } else {
  173. jsApiCall();
  174. }
  175. };
  176. //调用微信JS api 支付
  177. function jsApiCall() {
  178. WeixinJSBridge.invoke(
  179. 'getBrandWCPayRequest',
  180. <?php echo $jsApiParameters; ?>,
  181. function (res) {
  182. WeixinJSBridge.log(res.err_msg);
  183. if (res.err_msg == 'get_brand_wcpay_request:ok') {
  184. alert('支付成功');
  185. window.location.href = success_url+'?order_id='+order_id;
  186. } else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
  187. alert('支付取消');
  188. window.location.href = fail_url+'?order_id='+order_id;
  189. } else {
  190. alert('支付失败');
  191. window.location.href = fail_url+'?order_id='+order_id;
  192. }
  193. }
  194. );
  195. }
  196. function callpay() {
  197. if (typeof WeixinJSBridge == "undefined") {
  198. if (document.addEventListener) {
  199. document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
  200. } else if (document.attachEvent) {
  201. document.attachEvent('WeixinJSBridgeReady', jsApiCall);
  202. document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
  203. }
  204. } else {
  205. jsApiCall();
  206. }
  207. }
  208. </script>