PayNotifyCallback.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. define('AOP_SDK_DIR', __DIR__);
  3. require_once __DIR__ . '/AopSdk.php';
  4. require_once __DIR__ . '/AlipayConfig.php';
  5. require_once __DIR__ . '/aop/AopClient.php';
  6. require_once __DIR__ . '/aop/SignData.php';
  7. require_once __DIR__ . '/aop/request/AlipayTradeQueryRequest.php';
  8. class PayNotifyCallback{
  9. /**
  10. * 状态查询
  11. * @param string $orderSn
  12. * @return bool
  13. * @throws Exception
  14. */
  15. public function queryOrder($orderSn = '')
  16. {
  17. if (empty($orderSn)) {
  18. throw new Exception('订单号不能为空');
  19. }
  20. $aop = new AopClient ();
  21. $aop->signType = "RSA2";
  22. $aop->gatewayUrl = AlipayConfig::GATEWAY;
  23. $aop->appId = AlipayConfig::APPID;
  24. $aop->rsaPrivateKey = AlipayConfig::APPPRIVATEKEY;
  25. // 注意:这里是支付宝公钥,不是应用公钥
  26. $aop->alipayrsaPublicKey = AlipayConfig::ALIPAYPUBLICKEY;
  27. $tradeNo = '';
  28. $request = new AlipayTradeQueryRequest ();
  29. $request->setBizContent("{" .
  30. "\"out_trade_no\":\"$orderSn\"," .
  31. "\"trade_no\":\"$tradeNo\"" .
  32. "}");
  33. $result = $aop->execute($request);
  34. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  35. $resultCode = $result->$responseNode->code;
  36. $ttbPay = model('TtbAutoList');
  37. $orderInfo = $ttbPay->fetchOrderInfo($orderSn);
  38. if(! empty($resultCode) && $resultCode == 10000) {
  39. if (empty($orderInfo)) {
  40. return false;
  41. }
  42. $tradeStatus = $result->$responseNode->trade_status;
  43. if ($tradeStatus == 'TRADE_SUCCESS') {
  44. // 已支付
  45. if ($orderInfo['pay_status'] == 1) {
  46. return true;
  47. }
  48. return $ttbPay->paySuccess($orderSn, $result->$responseNode->trade_no);
  49. /*if ($result->$responseNode->total_amount == $orderInfo['pay_amount']) {
  50. // 更新订单状态,用户账户余额,账户变化日志
  51. return $ttbPay->paySuccess($orderSn, $result->$responseNode->trade_no);
  52. }*/
  53. } elseif ($tradeStatus == 'TRADE_CLOSED') {
  54. // 交易关闭
  55. if ($orderInfo['pay_status'] != 3) {
  56. $ttbPay->updateOrder(
  57. ['pay_status' => 3, 'update_time' => NOW_TIMESTAMP],
  58. ['order_sn' => $orderSn]
  59. );
  60. }
  61. return 'TRADE_CLOSED';
  62. }
  63. }
  64. if (! empty($resultCode) && $resultCode == 40004) {
  65. // 支付宝返回交易不存在, 麻花网络数据库订单状态更改为已关闭
  66. if ($orderInfo['pay_status'] != 3) {
  67. $ttbPay->updateOrder(
  68. ['pay_status' => 3, 'update_time' => NOW_TIMESTAMP],
  69. ['order_sn' => $orderSn]
  70. );
  71. }
  72. return 'TRADE_CLOSED';
  73. }
  74. return false;
  75. }
  76. // 支付回调验证
  77. public function callback()
  78. {
  79. $aop = new AopClient ();
  80. $aop->alipayrsaPublicKey = C('ALIPAY.ALI_PUBLIC_KEY');
  81. // 这里也需要支付宝公钥,不是应用公钥
  82. $result = $aop->rsaCheckV1($_POST, null, $_POST['sign_type']);
  83. if ($result) {
  84. if ($this->queryOrder($_POST['out_trade_no'])) {
  85. echo 'success';
  86. exit;
  87. }
  88. }
  89. echo 'error'; exit;
  90. }
  91. }