Pay.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. qihooLoad('Qihoo_Util');
  3. class Qihoo_Pay
  4. {
  5. const VERIFY_URL_ONLINE = 'http://msdk.mobilem.360.cn/pay/order_verify.json';
  6. const VERIFIED = 'verified';
  7. private $_appKey;
  8. private $_appSecret;
  9. /**
  10. * @var PayApp_Interface
  11. */
  12. private $_payApp;
  13. private $_request;
  14. public function __construct(PayApp_Interface $payApp)
  15. {
  16. $this->_payApp = $payApp;
  17. $this->_appKey = $payApp->getAppKey();
  18. $this->_appSecret = $payApp->getAppSecret();
  19. if (empty($this->_appSecret)) {
  20. die("fatal: this interface is for test app only!");
  21. }
  22. }
  23. public function processRequest()
  24. {
  25. $params = $_REQUEST;
  26. $this->_request = $params;
  27. if ($params['app_key'] != $this->_appKey) {
  28. echo "not my request, I am " . $this->_appKey;
  29. return;
  30. }
  31. if (!$this->_isValidRequest($params)) {
  32. echo 'invalid request';
  33. return;
  34. }
  35. $verfifyRet = $this->_verifyOrder($params);
  36. if ($verfifyRet != self::VERIFIED) {
  37. echo $verfifyRet;
  38. return;
  39. }
  40. //支付扩展信息
  41. if (!empty($params['pay_ext'])) {
  42. $params['pay_ext'] = json_decode($params['pay_ext'], 1);
  43. }
  44. if ($this->_payApp->isValidOrder($params)) {
  45. $this->_payApp->processOrder($params);
  46. }
  47. echo 'ok';
  48. }
  49. /**
  50. *
  51. * @param type $params
  52. */
  53. private function _isValidRequest($params)
  54. {
  55. $fields = array(
  56. 'app_key',
  57. 'amount',
  58. 'product_id',
  59. 'app_uid',
  60. 'order_id',
  61. 'sign_type',
  62. 'gateway_flag',
  63. 'sign',
  64. 'sign_return',
  65. );
  66. foreach ($fields as $field) {
  67. if (empty($params[$field])) {
  68. return false;
  69. }
  70. }
  71. return $this->_isSignOk();
  72. }
  73. private function _isSignOk()
  74. {
  75. $params = $this->_request;
  76. $secret = $this->_appSecret;
  77. return Qihoo_Util::getSign($params, $secret) == $params['sign'];
  78. }
  79. private function _verifyOrder($params)
  80. {
  81. $url = self::VERIFY_URL_ONLINE;
  82. unset($params['gateway_flag'], $params['sign'], $params['sign_return']);
  83. $params['app_key'] = $this->_appKey;
  84. $params['sign'] = Qihoo_Util::getSign($params, $this->_appSecret);
  85. $url .= '?' . http_build_query($params);
  86. $ret = Qihoo_Util::request($url, Qihoo_Util::METHOD_POST);
  87. $json = json_decode($ret, TRUE);
  88. return $json['ret'];
  89. }
  90. }