Pushorder.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace WY\app\model;
  3. use WY\app\libs\Http;
  4. use WY\app\libs\Controller;
  5. if (!defined('WY_ROOT')) {
  6. exit;
  7. }
  8. class Pushorder extends Controller
  9. {
  10. public function __construct($orderid)
  11. {
  12. parent::__construct();
  13. $this->orderid = $orderid;
  14. $this->orders = $this->model()->select()->from('orders')->where(array('fields' => 'orderid=?', 'values' => array($this->orderid)))->fetchRow();
  15. if (!$this->orders) {
  16. exit;
  17. }
  18. }
  19. private function getOrderInfo()
  20. {
  21. return $this->model()->select()->from('orderinfo')->where(array('fields' => 'id=?', 'values' => array($this->orders['orderinfoid'])))->fetchRow();
  22. }
  23. private function getApiKey()
  24. {
  25. $users = $this->model()->select('apikey')->from('users')->where(array('fields' => 'id=?', 'values' => array($this->orders['userid'])))->fetchRow();
  26. return $users['apikey'];
  27. }
  28. public function saltData($data)
  29. {
  30. $apikey = $this->getApiKey();
  31. $signstr = 'customerid=' . $data['customerid'] . '&status=' . $data['status'] . '&sdpayno=' . $data['sdpayno'] . '&sdorderno=' . $data['sdorderno'] . '&total_fee=' . $data['total_fee'] . '&paytype=' . $data['paytype'] . '&' . $apikey;
  32. $data += array('sign' => md5($signstr));
  33. return $data;
  34. }
  35. public function notify()
  36. {
  37. $orderinfo = $this->getOrderInfo();
  38. $http = new Http($orderinfo['notifyurl'], $this->saltData($this->getParams($orderinfo)));
  39. $http->toUrl();
  40. $resCode = $http->getResCode();
  41. $resContent = substr(str_replace(' ', '', strip_tags($http->getResContent())), 0, 50);
  42. $errInfo = $http->getErrInfo();
  43. $is_notify = $resContent == 'success' ? 1 : 2;
  44. $ret = array('code' => $resCode, 'content' => $resContent, 'info' => $errInfo);
  45. $data = array('is_notify' => $is_notify);
  46. $this->model()->from('orders')->updateSet($data)->where(array('fields' => 'orderid=?', 'values' => array($this->orderid)))->update();
  47. if ($ordernotify = $this->model()->select('times,nexts')->from('ordernotify')->where(array('fields' => 'orid=?', 'values' => array($this->orders['id'])))->fetchRow()) {
  48. $data = array('is_status' => $is_notify, 'retmsg' => json_encode($ret), 'times' => 1 + $ordernotify['times'], 'nexts' => 30 + $ordernotify['nexts']);
  49. $this->model()->from('ordernotify')->updateSet($data)->where(array('fields' => 'orid=?', 'values' => array($this->orders['id'])))->update();
  50. } else {
  51. $data = array('orid' => $this->orders['id'], 'is_status' => $is_notify, 'retmsg' => json_encode($ret), 'addtime' => time(), 'times' => 1, 'nexts' => 30);
  52. $this->model()->from('ordernotify')->insertData($data)->insert();
  53. }
  54. return true;
  55. }
  56. public function sync()
  57. {
  58. $orderinfo = $this->getOrderInfo();
  59. $newData = $this->saltData($this->getParams($orderinfo));
  60. $params = '';
  61. foreach ($newData as $key => $val) {
  62. $params .= $params ? '&' : '';
  63. $params .= $key . '=' . $val;
  64. }
  65. $this->res->redirect($orderinfo['returnurl'] . '?' . $params);
  66. }
  67. public function ajax()
  68. {
  69. $orderinfo = $this->getOrderInfo();
  70. $newData = $this->saltData($this->getParams($orderinfo));
  71. $params = '';
  72. foreach ($newData as $key => $val) {
  73. $params .= $params ? '&' : '';
  74. $params .= $key . '=' . $val;
  75. }
  76. if ($newData['status']==1){
  77. $url=$orderinfo['returnurl'] . '?' . $params;
  78. //$this->res->redirect($url);
  79. //exit;
  80. return $orderinfo['returnurl'] . '?' . $params;
  81. }else{
  82. return "T";
  83. }
  84. //$this->res->redirect($orderinfo['returnurl'] . '?' . $params);
  85. }
  86. public function getParams($orderinfo)
  87. {
  88. $data = array('status' => $this->orders['is_state'], 'customerid' => $this->orders['userid'], 'sdpayno' => $this->orderid, 'sdorderno' => $this->orders['sdorderno'], 'total_fee' => $this->orders['realmoney'], 'paytype' => $orderinfo['paytype'], 'remark' => $orderinfo['remark']);
  89. return $data;
  90. }
  91. }