AliPay.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\expand\controller;
  3. use alipay\Wappay;
  4. use app\common\service\HelperService;
  5. use think\Config;
  6. use think\Log;
  7. use think\Validate;
  8. use think\Cache;
  9. /**
  10. * 阿里支付接口
  11. * Class Alipay
  12. * @package app\expand\controller
  13. */
  14. class AliPay extends BaseAuth
  15. {
  16. private $_Account = null;
  17. private $_AliSellerId = null;
  18. private $_AppAuthToken = null;
  19. private $_SysServiceProviderId = null;
  20. public function __construct(){
  21. parent::__construct();
  22. if($this->_inWhiteList){
  23. return true;
  24. }
  25. $this->_Account = $this->getKey($this->_apiCode);
  26. //验证是否具有访问这个接口的权限
  27. if(!isset($this->_Account['ali_seller_id'])
  28. || !isset($this->_Account['app_auth_token'])
  29. || !isset($this->_Account['sys_service_provider_id'])
  30. || !isset($this->_Account['app_id'])
  31. || !isset($this->_Account['alipay_url'])){
  32. HelperService::returnJson(['code'=>400,'msg'=>'alipay interface unauthorized access','data'=>[]]);
  33. }
  34. $this->_AliSellerId = $this->_Account['ali_seller_id'];
  35. $this->_AppAuthToken = $this->_Account['app_auth_token'];
  36. $this->_SysServiceProviderId = $this->_Account['sys_service_provider_id'];
  37. $this->_AppId = $this->_Account['app_id'];
  38. Config::set('AliSellerId',$this->_AliSellerId);
  39. }
  40. public function platformAlipay(){
  41. $params = $this->_params;
  42. $rule = [
  43. 'return_url'=>'require',
  44. 'out_trade_no'=>'require',
  45. 'total_amount'=>'require',
  46. 'discountable_amount'=>'require',
  47. 'subject'=>'require',
  48. 'goods_detail'=>'require',
  49. 'store_id'=>'require',
  50. 'terminal_id'=>'require',
  51. 'extend_params'=>'require',
  52. ];
  53. $validate = new Validate($rule);
  54. if(!$validate->check($params)){
  55. HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()]);
  56. }
  57. Log::record("platformAlipay:".$params);
  58. $body = "";
  59. try {
  60. $apiCode = $this->_apiCode;
  61. $submitForm = Wappay::pay(array_filter([
  62. 'out_trade_no' => $params['out_trade_no'],
  63. 'subject' => "{$params['subject']}($body)",//售卖项目(Iphone6 16G)
  64. 'total_amount' => $params['total_amount'],
  65. 'discountable_amount' => $params['discountable_amount'],
  66. 'body' => $body,
  67. 'auth_token' => $this->_AppAuthToken,
  68. 'extend_params'=>['sys_service_provider_id'=>$this->_SysServiceProviderId],
  69. 'seller_id' => $this->_AliSellerId,
  70. 'operator_id' => isset($params['operator_id']) ? $params['operator_id'] : 'cs_vapi',
  71. 'terminal_id' => $params['terminal_id'],
  72. 'timeout_express' => '1m'
  73. ]), [
  74. 'gatewayUrl' => 'https://openapi.alipay.com/gateway.do',
  75. 'app_id' => $this->_AppId,
  76. 'merchant_private_key' => file_get_contents("./COMPANY_LIST/$apiCode/alipay/app_private_key.pem"),
  77. 'alipay_public_key' => file_get_contents("./COMPANY_LIST/$apiCode/alipay/app_public_key.pem"),
  78. 'sign_type' => 'RSA2',
  79. 'format' => 'JSON',
  80. 'return_url' => $params['return_url'],
  81. 'notify_url' => "http://{$_SERVER['SERVER_NAME']}/expand/Alipay/notifyPlatformAlipay",
  82. ]);
  83. Log::record("$submitForm");
  84. $this->assign('submitForm', $submitForm);
  85. }catch (\Exception $ex){
  86. die("<script>window.alert = function(name){var iframe = document.createElement('IFRAME');iframe.style.display='none';iframe.setAttribute('src', 'data:text/plain,');document.documentElement.appendChild(iframe);window.frames[0].window.alert(name);iframe.parentNode.removeChild(iframe);};alert('".$ex->getMessage()."');window.history.go(-1);</script>");
  87. }
  88. return $this->fetch('alipay/pay');
  89. }
  90. /**
  91. * 支付宝异步通知
  92. */
  93. public function notifyPlatformAlipay(){
  94. $this->getInput();
  95. $params = $this->_sysParams;
  96. $rule = [
  97. 'out_trade_no'=>'require',
  98. 'total_amount'=>'require',
  99. 'trade_no'=>'require'
  100. ];
  101. $validate = new Validate($rule);
  102. if(!$validate->check($params)){
  103. HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()]);
  104. }
  105. $outTradeNoArr = explode('_', "{$params['out_trade_no']}");
  106. $orderNo = current($outTradeNoArr);
  107. $companyCode = end($outTradeNoArr);
  108. if(Cache::has("ali_{$orderNo}")){
  109. die('success');
  110. }
  111. try {
  112. $data = [
  113. 'order_no' => "{$orderNo}",
  114. 'total_fee' => "{$params['total_amount']}",
  115. 'out_trade_no' => "{$params['out_trade_no']}",
  116. 'add_ts' => time(),
  117. 'alipay_msg' => json_encode($params),
  118. 'trade_no' => "{$params['trade_no']}",
  119. 'company_code' => $companyCode,
  120. ];
  121. $this->_Account = $this->getKey($companyCode);
  122. $alipayUrl = is_array($this->_Account['alipay_url'])?$this->_Account['alipay_url']:["{$this->_Account['alipay_url']}"];
  123. foreach($alipayUrl as $aliUrl){
  124. Cache::set("ali_{$orderNo}",time(),86400*365);
  125. $times = 3;
  126. while($times--){
  127. $res = HelperService::httpPost($aliUrl, json_encode($data));
  128. $return = HelperService::$_httpReturn;
  129. HelperService::addLog($return,$aliUrl,'alipay');
  130. if($res !== false){
  131. break;
  132. }
  133. }
  134. }
  135. die('success');
  136. }catch (\Exception $ex){
  137. HelperService::addLog($ex->getMessage(),"",'alipay');
  138. die('fail(exception)');
  139. }
  140. }
  141. }