72 lines
5.4 KiB
PHP
72 lines
5.4 KiB
PHP
<?php
|
|
namespace App\Services\PaymentHealth;
|
|
|
|
use App\Models\Payment;
|
|
use App\Services\PaymentService;
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
use RuntimeException;
|
|
|
|
class EpayProbe
|
|
{
|
|
public function __construct(private HttpsTransport $transport,private QrEvidence $qr) {}
|
|
public function supports(Payment $p): bool
|
|
{return strcasecmp((string)$p->payment,'EPay')===0&&in_array($p->config['type']??null,['alipay','wxpay'],true)&&!empty($p->config['url'])&&!empty($p->config['pid'])&&!empty($p->config['key']);}
|
|
|
|
public function probe(Payment $p,string $tradeNo): array
|
|
{
|
|
try{
|
|
$amount=(int)config('payment_health.amount_cents',3200);
|
|
$url=$this->paymentUrl($p,$tradeNo,$amount);$fields=[];parse_str(parse_url($url,PHP_URL_QUERY)??'',$fields);
|
|
$url=preg_replace('/\?.*$/','',$url);
|
|
if(($fields['out_trade_no']??null)!==$tradeNo||!is_string($fields['sign']??null)||!preg_match('/\A[0-9a-f]{32}\z/i',$fields['sign'])||!is_scalar($fields['money']??null)||(int)round((float)$fields['money']*100)!==$amount)throw new RuntimeException('invalid_probe_payload');
|
|
$method='POST';$type=$p->config['type'];
|
|
for($hop=0;$hop<4;$hop++){
|
|
$r=$this->transport->request($method,$url,$fields,$hop>0);$status=$r['status'];$body=$r['body'];
|
|
$meta=['http'=>$status,'host'=>$r['host'],'body_sha256'=>$r['sha256']];
|
|
if($status===404||$status===410)return $this->result('hard_failure','cashier_not_found',$meta);
|
|
if($status>=500||$status===408||$status===429)return $this->result('soft_failure','cashier_transient',$meta);
|
|
if($status>=300&&$status<400&&$r['location']){$url=$this->absolute($url,$r['location']);$method='GET';$fields=[];continue;}
|
|
if($status!==200)return $this->result('indeterminate','unexpected_http',$meta);
|
|
if(preg_match('/订单.{0,5}(?:超时|过期)|签名错误|商户不存在|商户未配置|未配置支付接口商户|支付通道.{0,10}(?:关闭|维护|不可用)|invalid merchant|payment unavailable|<title>\s*404/iu',$body))return $this->result('hard_failure','cashier_business_error',$meta);
|
|
if($this->qr->valid($body,$type,$amount))return $this->result('healthy','fresh_cashier_qr',$meta+['amount_cents'=>$amount,'qr_evidence'=>true]);
|
|
// Follow only recognized provider handoffs; every host is rechecked by the transport.
|
|
$doc=new \DOMDocument();@$doc->loadHTML($body);$form=$doc->getElementsByTagName('form')->item(0);
|
|
if($form){
|
|
$next=[];foreach($form->getElementsByTagName('input')as$input)if($input->hasAttribute('name'))$next[$input->getAttribute('name')]=$input->getAttribute('value');
|
|
if(count($next)>30)return $this->result('indeterminate','unexpected_form',$meta);
|
|
if(isset($next['money'])&&(int)round((float)$next['money']*100)!==$amount)return $this->result('hard_failure','cashier_amount_mismatch',$meta);
|
|
if(!in_array(strtoupper($form->getAttribute('method')?:'GET'),['GET','POST'],true))return $this->result('indeterminate','unexpected_form_method',$meta);
|
|
$url=$this->absolute($url,$form->getAttribute('action'));$method=strtoupper($form->getAttribute('method')?:'GET');$fields=$next;
|
|
if($method==='GET'){$url.=(str_contains($url,'?')?'&':'?').http_build_query($fields);$fields=[];}
|
|
continue;
|
|
}
|
|
if(preg_match('/(?:window\.)?location\.replace\((["\x27])(.*?)\1\)|(?:window\.)?location\.href\s*=\s*(["\x27])(.*?)\3/s',$body,$m)){
|
|
$url=$this->absolute($url,html_entity_decode(str_replace('\\/','/',($m[2]??'')?:($m[4]??'')),ENT_QUOTES));$method='GET';$fields=[];continue;
|
|
}
|
|
return $this->result('indeterminate','no_verified_qr',$meta);
|
|
}
|
|
return $this->result('indeterminate','handoff_limit');
|
|
}catch(ConnectionException $e){return $this->result('soft_failure','network_timeout');}
|
|
catch(RuntimeException $e){
|
|
$reason=$e->getMessage();
|
|
if(in_array($reason,['unapproved_host','unsafe_url','nonpublic_dns','ip_literal_not_allowed'],true))return $this->result('review_required',$reason);
|
|
if($reason==='dns_unavailable')return $this->result('soft_failure',$reason);
|
|
return $this->result('indeterminate','probe_error');
|
|
}catch(\Throwable $e){return $this->result('indeterminate','probe_error');}
|
|
}
|
|
protected function paymentUrl(Payment $p,string $tradeNo,int $cents): string
|
|
{
|
|
$result=(new PaymentService($p->payment,$p->id))->pay(['trade_no'=>$tradeNo,'total_amount'=>$cents,'user_id'=>0,'stripe_token'=>null]);
|
|
if(($result['type']??null)!==1||!is_string($result['data']??null))throw new RuntimeException('unsupported_response');
|
|
return $result['data'];
|
|
}
|
|
private function absolute(string $base,string $value):string
|
|
{
|
|
if(str_starts_with($value,'//'))return 'https:'.$value;
|
|
if(parse_url($value,PHP_URL_SCHEME)!==null)return $value;
|
|
$host=parse_url($base,PHP_URL_HOST);return 'https://'.$host.(str_starts_with($value,'/')?'':rtrim(dirname(parse_url($base,PHP_URL_PATH)?:'/'),'/').'/').$value;
|
|
}
|
|
private function result(string $status,string $reason,array $meta=[]):array
|
|
{return ['status'=>$status,'reason'=>$reason]+$meta;}
|
|
}
|