123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- namespace app\expand\controller;
- use alipay\Wappay;
- use app\common\service\HelperService;
- use think\Config;
- use think\Log;
- use think\Validate;
- use think\Cache;
- /**
- * 阿里支付接口
- * Class Alipay
- * @package app\expand\controller
- */
- class AliPay extends BaseAuth
- {
- private $_Account = null;
- private $_AliSellerId = null;
- private $_AppAuthToken = null;
- private $_SysServiceProviderId = null;
- public function __construct(){
- parent::__construct();
-
- if($this->_inWhiteList){
- return true;
- }
- $this->_Account = $this->getKey($this->_apiCode);
- //验证是否具有访问这个接口的权限
- if(!isset($this->_Account['ali_seller_id'])
- || !isset($this->_Account['app_auth_token'])
- || !isset($this->_Account['sys_service_provider_id'])
- || !isset($this->_Account['app_id'])
- || !isset($this->_Account['alipay_url'])){
-
- HelperService::returnJson(['code'=>400,'msg'=>'alipay interface unauthorized access','data'=>[]]);
- }
- $this->_AliSellerId = $this->_Account['ali_seller_id'];
- $this->_AppAuthToken = $this->_Account['app_auth_token'];
- $this->_SysServiceProviderId = $this->_Account['sys_service_provider_id'];
- $this->_AppId = $this->_Account['app_id'];
- Config::set('AliSellerId',$this->_AliSellerId);
- }
- public function platformAlipay(){
- $params = $this->_params;
- $rule = [
- 'return_url'=>'require',
- 'out_trade_no'=>'require',
- 'total_amount'=>'require',
- 'discountable_amount'=>'require',
- 'subject'=>'require',
- 'goods_detail'=>'require',
- 'store_id'=>'require',
- 'terminal_id'=>'require',
- 'extend_params'=>'require',
- ];
- $validate = new Validate($rule);
- if(!$validate->check($params)){
- HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()]);
- }
- Log::record("platformAlipay:".$params);
- $body = "";
- try {
- $apiCode = $this->_apiCode;
- $submitForm = Wappay::pay(array_filter([
- 'out_trade_no' => $params['out_trade_no'],
- 'subject' => "{$params['subject']}($body)",//售卖项目(Iphone6 16G)
- 'total_amount' => $params['total_amount'],
- 'discountable_amount' => $params['discountable_amount'],
- 'body' => $body,
- 'auth_token' => $this->_AppAuthToken,
- 'extend_params'=>['sys_service_provider_id'=>$this->_SysServiceProviderId],
- 'seller_id' => $this->_AliSellerId,
- 'operator_id' => isset($params['operator_id']) ? $params['operator_id'] : 'cs_vapi',
- 'terminal_id' => $params['terminal_id'],
- 'timeout_express' => '1m'
- ]), [
- 'gatewayUrl' => 'https://openapi.alipay.com/gateway.do',
- 'app_id' => $this->_AppId,
- 'merchant_private_key' => file_get_contents("./COMPANY_LIST/$apiCode/alipay/app_private_key.pem"),
- 'alipay_public_key' => file_get_contents("./COMPANY_LIST/$apiCode/alipay/app_public_key.pem"),
- 'sign_type' => 'RSA2',
- 'format' => 'JSON',
- 'return_url' => $params['return_url'],
- 'notify_url' => "http://{$_SERVER['SERVER_NAME']}/expand/Alipay/notifyPlatformAlipay",
- ]);
- Log::record("$submitForm");
- $this->assign('submitForm', $submitForm);
- }catch (\Exception $ex){
- 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>");
- }
- return $this->fetch('alipay/pay');
- }
- /**
- * 支付宝异步通知
- */
- public function notifyPlatformAlipay(){
- $this->getInput();
- $params = $this->_sysParams;
- $rule = [
- 'out_trade_no'=>'require',
- 'total_amount'=>'require',
- 'trade_no'=>'require'
- ];
- $validate = new Validate($rule);
- if(!$validate->check($params)){
- HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()]);
- }
-
- $outTradeNoArr = explode('_', "{$params['out_trade_no']}");
- $orderNo = current($outTradeNoArr);
- $companyCode = end($outTradeNoArr);
-
- if(Cache::has("ali_{$orderNo}")){
- die('success');
- }
- try {
- $data = [
- 'order_no' => "{$orderNo}",
- 'total_fee' => "{$params['total_amount']}",
- 'out_trade_no' => "{$params['out_trade_no']}",
- 'add_ts' => time(),
- 'alipay_msg' => json_encode($params),
- 'trade_no' => "{$params['trade_no']}",
- 'company_code' => $companyCode,
- ];
- $this->_Account = $this->getKey($companyCode);
- $alipayUrl = is_array($this->_Account['alipay_url'])?$this->_Account['alipay_url']:["{$this->_Account['alipay_url']}"];
-
- foreach($alipayUrl as $aliUrl){
-
- Cache::set("ali_{$orderNo}",time(),86400*365);
-
- $times = 3;
- while($times--){
- $res = HelperService::httpPost($aliUrl, json_encode($data));
- $return = HelperService::$_httpReturn;
- HelperService::addLog($return,$aliUrl,'alipay');
-
- if($res !== false){
- break;
- }
- }
-
- }
-
- die('success');
-
- }catch (\Exception $ex){
- HelperService::addLog($ex->getMessage(),"",'alipay');
- die('fail(exception)');
- }
- }
- }
|