Created
March 1, 2022 09:30
-
-
Save nickfox-taterli/531650de640b41d6b0add60c9b5a585c to your computer and use it in GitHub Desktop.
V2Board Payjs 支付接口
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Payments; | |
class Payjs { | |
public function __construct($config) | |
{ | |
$this->config = $config; | |
} | |
public function form() | |
{ | |
return [ | |
'mchid' => [ | |
'label' => '商户号', | |
'description' => '', | |
'type' => 'input', | |
], | |
'key' => [ | |
'label' => '通信密钥', | |
'description' => '', | |
'type' => 'input', | |
], | |
'type' => [ | |
'label' => '支付通道(wechat/alipay)', | |
'description' => '', | |
'type' => 'input', | |
] | |
]; | |
} | |
public function pay($order) | |
{ | |
$data = [ | |
"mchid" => $this->config['mchid'], | |
"total_fee" => $order['total_amount'], | |
"out_trade_no" => $order['trade_no'], | |
"notify_url" => $order['notify_url'], | |
"body" => "增值套餐" | |
]; | |
info($this->config); | |
if(strcmp($this->config['type'],'alipay') == 0){ | |
$data['type'] = 'alipay'; | |
} | |
info($data); | |
$data = array_filter($data); | |
ksort($data); | |
$data['sign'] = strtoupper(md5(urldecode(http_build_query($data) . '&key=' . $this->config['key']))); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 10); | |
curl_setopt($ch, CURLOPT_URL, 'https://payjs.cn/api/native'); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'HTTP CLIENT'); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); | |
curl_setopt($ch, CURLOPT_HEADER, FALSE); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($ch, CURLOPT_POST, TRUE); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
$result = json_decode($data, true); | |
info($result); | |
if ($this->checkSign($result) === true) { | |
return [ | |
'type' => 0, | |
'data' => $result['code_url'] | |
]; | |
} | |
} | |
public function notify($params) | |
{ | |
if( $params['return_code'] == 1){ | |
info($params); | |
if ($this->checkSign($params) === true) { | |
return [ | |
'trade_no' => $params['out_trade_no'], | |
'callback_no' => $params['payjs_order_id'] | |
]; | |
} | |
} | |
die('success'); | |
} | |
private function checkSign($data) | |
{ | |
$in_sign = $data['sign']; | |
unset($data['sign']); | |
$data = array_filter($data); | |
ksort($data); | |
$sign = strtoupper(md5(urldecode(http_build_query($data) . '&key=' . $this->config['key']))); | |
return $in_sign == $sign ? true : false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment