This commit is contained in:
wangjinlei
2025-02-18 11:00:38 +08:00
parent bd5f4fa187
commit a38b143c9f
4 changed files with 157 additions and 7 deletions

View File

@@ -3,6 +3,7 @@
namespace app\api\controller;
use think\Db;
use think\Env;
use think\Queue;
use think\Validate;
@@ -1125,6 +1126,147 @@ class Preaccept extends Base
return jsonSuccess([]);
}
public function payStationTest(){
$accessToken = $this->payStationAccessToken();
$data_array = [
'paystation_id' => Env::get("paystation.client_id"),
'gateway_id' => "v1",//GATEWAY_ID,
'merchant_session' => "myTestSN001",
// 'merchant_reference' => 'testReference',
'amount' => 2000, //$20 in cents value
"test_mode"=>true
];
// OPTIONAL, ideally should be set on your Paystation account
$data_array += [
'return_url' => "http://192.168.110.110/tougao/public/index.php/api/Preaccept/patStationReturn",//'http://localhost:8002/3party/return.php',
// Webhook receipt page. This should be publicly accessible (see example post-response.php)
'response_url' => "https://www.tmrjournals.com"//'https://webhook.site/sample-code-webhook'
];
$data = json_encode($data_array);
// Purchase initiate request
$purchase = $this->postRequest('v1/hosted/purchases', $accessToken, $data);
return jsonSuccess($purchase);
if ($purchase) {
$result = json_decode($purchase);
if ($result && isset($result->payment_url)) {
header("Location: {$result->payment_url}");
} else {
//@todo: Replace with your own error handling
print_r($result);
return null;
}
}
}
public function payStationTest1(){
$accessToken = $this->payStationAccessToken();
$data_array = [
'paystation_id' => Env::get("paystation.client_id"),
'gateway_id' => "v1",//GATEWAY_ID,
"order_name" => "mytestOrderSn",
"amount" =>200,
"test_mode"=>true
];
$data = json_encode($data_array);
$purchase = $this->postRequest('v1/payme/purchases', $accessToken, $data);
return jsonSuccess($purchase);
}
public function patStationReturn(){
$query_string = $_SERVER['QUERY_STRING'];
parse_str($query_string, $data);
if (isset($data['code'])) {
// Add code here to display result
// Do not update transaction status here because query strings can be manipulated
// Do a transaction lookup instead to verify the results. Check documentation for more details.
if ($data['code'] == '0') {
echo 'Transaction ok: ' . $data['message'];
} else {
echo 'Transaction failed: ' . $data['message'];
}
}
}
private function payStationAccessToken(){
$bodyParams = [
'client_id' => Env::get("paystation.client_id"),
'client_secret' => Env::get("paystation.client_secret"),
'grant_type' => 'client_credentials',
'scope' => "mytest"
];
$accessTokenUrl = Env::get("paystation.api_url") . '/oauth/token';
$curlHandle = curl_init($accessTokenUrl);
$options = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_HTTPHEADER => [
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
"accept: *",
"accept-encoding: gzip, deflate",
],
CURLOPT_POSTFIELDS => http_build_query($bodyParams)
];
curl_setopt_array($curlHandle, $options);
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
$curlResponse = curl_exec($curlHandle);
$error = curl_error($curlHandle);
curl_close($curlHandle);
if ($error) {
echo "cURL error: " . $error;
} else {
$response = json_decode($curlResponse);
if (array_key_exists('access_token', $response)) {
return $response->access_token;
}
if (array_key_exists('error', $response)) {
echo $response->error_description;
}
}
}
public function postRequest($endpoint, $token, $body)
{
$curlHandle = curl_init(Env::get("paystation.api_url") . '/' . $endpoint);
$options = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER=>false,
CURLOPT_HTTPHEADER => [
"cache-control: no-cache",
"content-type: application/json",
"accept: *",
"accept-encoding: gzip, deflate",
"Authorization: Bearer " . $token
],
CURLOPT_POSTFIELDS => $body
];
curl_setopt_array($curlHandle, $options);
$response = curl_exec($curlHandle);
$error = curl_error($curlHandle);
curl_close($curlHandle);
if ($error) {
echo "cURL error: " . $error;
} else {
return $response ?: null;
}
return null;
}
// public function getArticleMains(){
// $data = $this->request->post();