Skip to content

Commit

Permalink
fix: use SHA256 as sign type for epay
Browse files Browse the repository at this point in the history
  • Loading branch information
M1Screw committed Oct 20, 2023
1 parent 2913fcb commit 8980609
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 32 deletions.
10 changes: 5 additions & 5 deletions src/Services/Gateway/Epay.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct()
$this->epay['apiurl'] = Setting::obtain('epay_url');//易支付API地址
$this->epay['partner'] = Setting::obtain('epay_pid');//易支付商户pid
$this->epay['key'] = Setting::obtain('epay_key');//易支付商户Key
$this->epay['sign_type'] = strtoupper('MD5'); //签名方式
$this->epay['sign_type'] = strtoupper('SHA256'); //签名方式
$this->epay['input_charset'] = strtolower('utf-8');//字符编码
$this->epay['transport'] = 'https';//协议 http 或者https
}
Expand Down Expand Up @@ -97,16 +97,16 @@ public function purchase(ServerRequest $request, Response $response, array $args
'sitename' => $_ENV['appName'],
];

$alipaySubmit = new EpaySubmit($this->epay);
$html_text = $alipaySubmit->buildRequestForm($data);
$epaySubmit = new EpaySubmit($this->epay);
$html_text = $epaySubmit->buildRequestForm($data);

return $response->write($html_text);
}

public function notify($request, $response, $args): ResponseInterface
{
$alipayNotify = new EpayNotify($this->epay);
$verify_result = $alipayNotify->verifyNotify();
$epayNotify = new EpayNotify($this->epay);
$verify_result = $epayNotify->verifyNotify();

if ($verify_result) {
$out_trade_no = $_GET['out_trade_no'];
Expand Down
18 changes: 6 additions & 12 deletions src/Services/Gateway/Epay/EpayNotify.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,20 @@

final class EpayNotify
{
private array $alipay_config;
private array $epay_config;

public function __construct($alipay_config)
public function __construct($epay_config)
{
$this->alipay_config = $alipay_config;
$this->epay_config = $epay_config;
}

public function verifyNotify(): bool
{
if (is_null($_GET)) {//判断POST来的数组是否为空
return false;
}
//生成签名结果
$isSign = $this->getSignVeryfy($_GET, $_GET['sign']);
//获取支付宝远程服务器ATN结果(验证是否是支付宝发来的消息)
$responseTxt = 'true';
//验证
//$responsetTxt的结果不是true,与服务器设置问题、合作身份者ID、notify_id一分钟失效有关
//isSign的结果不是true,与安全校验码、请求时的参数格式(如:带自定义参数等)、编码格式有关
if (preg_match('/true$/i', $responseTxt) && $isSign) {

if ($this->getSignVeryfy($_GET, $_GET['sign'])) {
return true;
}

Expand All @@ -43,6 +37,6 @@ public function getSignVeryfy($para_temp, $sign): bool
//把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
$prestr = EpayTool::createLinkstring($para_sort);

return EpayTool::verify($prestr, $sign, $this->alipay_config['key']);
return EpayTool::verify($prestr, $sign, $this->epay_config['key']);
}
}
26 changes: 13 additions & 13 deletions src/Services/Gateway/Epay/EpaySubmit.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@

final class EpaySubmit
{
private array $alipay_config;
private string $alipay_gateway_new;
private array $epay_config;
private string $epay_gateway;

public function __construct($alipay_config)
public function __construct($epay_config)
{
$this->alipay_config = $alipay_config;
$this->alipay_gateway_new = $this->alipay_config['apiurl'] . 'submit.php?';
$this->epay_config = $epay_config;
$this->epay_gateway = $this->epay_config['apiurl'] . 'submit.php?';
}

public function buildRequestMysign($para_sort): string
{
//把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
$prestr = EpayTool::createLinkstring($para_sort);

return EpayTool::sign($prestr, $this->alipay_config['key']);
return EpayTool::sign($prestr, $this->epay_config['key']);
}

public function buildRequestPara($para_temp)
Expand All @@ -33,7 +33,7 @@ public function buildRequestPara($para_temp)
$mysign = $this->buildRequestMysign($para_sort);
//签名结果与签名方式加入请求提交参数组中
$para_sort['sign'] = $mysign;
$para_sort['sign_type'] = strtoupper(trim($this->alipay_config['sign_type']));
$para_sort['sign_type'] = strtoupper(trim($this->epay_config['sign_type']));

return $para_sort;
}
Expand All @@ -42,16 +42,16 @@ public function buildRequestForm($para_temp, $method = 'POST', $button_name = '
{
//待请求参数数组
$para = $this->buildRequestPara($para_temp);
$sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='".
$this->alipay_gateway_new . "' method='" . $method . "'>";
$html = "<form id='alipaysubmit' name='alipaysubmit' action='".
$this->epay_gateway . "' method='" . $method . "'>";

foreach ($para as $key => $val) {
$sHtml .= "<input type='hidden' name='".$key."' value='".$val."'/>";
$html .= "<input type='hidden' name='".$key."' value='".$val."'/>";
}
//submit按钮控件请不要含有name属性
$sHtml .= "<input type='submit' value='".$button_name."'></form>";
$sHtml .= "<script>document.forms['alipaysubmit'].submit();</script>";
$html .= "<input type='submit' value='".$button_name."'></form>";
$html .= "<script>document.forms['alipaysubmit'].submit();</script>";

return $sHtml;
return $html;
}
}
4 changes: 2 additions & 2 deletions src/Services/Gateway/Epay/EpayTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public static function sign($prestr, $key): string
public static function verify($prestr, $sign, $key): bool
{
$prestr .= $key;
$correct_sgin = hash('sha256', $prestr);
$correct_sign = hash('sha256', $prestr);

return $correct_sgin === $sign;
return $correct_sign === $sign;
}

public static function createLinkstring($para): string
Expand Down

0 comments on commit 8980609

Please sign in to comment.