Pay.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\AopClient;
  5. use app\common\library\Common;
  6. use app\common\model\Config;
  7. use app\common\model\User;
  8. use think\Db;
  9. use think\Request;
  10. use app\common\model\Order;
  11. /**
  12. * 支付成功回调
  13. */
  14. class Pay extends Api
  15. {
  16. /**
  17. * 微信支付--商品支付成功回调订单
  18. */
  19. public function pay_order(){
  20. $payXml = file_get_contents("php://input");
  21. //将xml格式转化为json格式
  22. $jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA));
  23. //将json格式转成数组格式 $result['out_trade_no']
  24. $result = json_decode($jsonXml, true);
  25. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  26. $order = Order::where('status',0)->where('order_no',$result['out_trade_no'])->find();
  27. if ($order){
  28. Db::startTrans();
  29. try {
  30. $da['pay_time'] = time();
  31. $da['status'] = 1;
  32. $da['return_success_info'] = json_encode($result,true);
  33. Order::where('id',$order['id'])->update($da);
  34. Common::order_status($order['id'],'微信支付成功');
  35. Db::commit();
  36. $arr = array(
  37. 'return_code' => 'SUCCESS',
  38. 'return_msg' => 'OK',
  39. );
  40. return $this->arrayToXml($arr);
  41. }catch (Exception $e) {
  42. Db::rollback();
  43. file_put_contents("order_pay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  44. }
  45. }
  46. }
  47. }
  48. /**
  49. * 微信支付--加班费支付成功回调订单
  50. */
  51. public function overtime_pay_order(){
  52. $payXml = file_get_contents("php://input");
  53. //将xml格式转化为json格式
  54. $jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA));
  55. //将json格式转成数组格式 $result['out_trade_no']
  56. $result = json_decode($jsonXml, true);
  57. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  58. $order = Order::where('overtime_pay_status',1)->where('overtime_pay_order_no',$result['out_trade_no'])->find();
  59. if ($order){
  60. Db::startTrans();
  61. try {
  62. $da['overtime_pay_time'] = time();
  63. $da['overtime_pay_status'] = 2;
  64. $da['overtime_return_success_info'] = json_encode($result,true);
  65. Order::where('id',$order['id'])->update($da);
  66. Common::order_status($order['id'],'微信支付加班费成功');
  67. Db::commit();
  68. $arr = array(
  69. 'return_code' => 'SUCCESS',
  70. 'return_msg' => 'OK',
  71. );
  72. return $this->arrayToXml($arr);
  73. }catch (Exception $e) {
  74. Db::rollback();
  75. file_put_contents("order_pay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * 支付宝支付--商品支付成功回调订单
  82. */
  83. public function alipay_order(){
  84. $result = input('post.');
  85. if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
  86. $order = Order::where('status',0)->where('order_no',$result['out_trade_no'])->find();
  87. if ($order){
  88. Db::startTrans();
  89. try {
  90. $da['pay_time'] = time();
  91. $da['status'] = 1;
  92. $da['return_success_info'] = json_encode($result,true);
  93. Order::where('id',$order['id'])->update($da);
  94. Common::order_status($order['id'],'支付宝支付成功');
  95. Db::commit();
  96. echo 'success';
  97. }catch (Exception $e) {
  98. Db::rollback();
  99. file_put_contents("order_alipay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * 支付宝支付--商品支付成功回调订单
  106. */
  107. public function overtime_alipay_order(){
  108. $result = input('post.');
  109. if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
  110. $order = Order::where('overtime_pay_status',1)->where('overtime_pay_order_no',$result['out_trade_no'])->find();
  111. if ($order){
  112. Db::startTrans();
  113. try {
  114. $da['overtime_pay_time'] = time();
  115. $da['overtime_pay_status'] = 2;
  116. $da['overtime_return_success_info'] = json_encode($result,true);
  117. Order::where('id',$order['id'])->update($da);
  118. Common::order_status($order['id'],'支付宝支付加班费成功');
  119. Db::commit();
  120. echo 'success';
  121. }catch (Exception $e) {
  122. Db::rollback();
  123. file_put_contents("order_alipay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  124. }
  125. }
  126. }
  127. }
  128. /**
  129. * 数组转xml
  130. * @ApiInternal
  131. */
  132. public function arrayToXml($arr)
  133. {
  134. $xml = "<xml>";
  135. foreach ($arr as $key => $val) {
  136. if (is_numeric($val)) {
  137. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  138. } else
  139. $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
  140. }
  141. $xml .= "</xml>";
  142. return $xml;
  143. }
  144. }