This commit is contained in:
wangjinlei
2025-02-24 09:24:50 +08:00
parent 2ed8c60fe9
commit 3620475d2d
3 changed files with 172 additions and 57 deletions

View File

@@ -12,9 +12,11 @@ use PaypalServerSdkLib\Models\Builders\PaymentSourceBuilder;
use PaypalServerSdkLib\Models\Builders\PaypalWalletBuilder; use PaypalServerSdkLib\Models\Builders\PaypalWalletBuilder;
use PaypalServerSdkLib\Models\Builders\PaypalWalletExperienceContextBuilder; use PaypalServerSdkLib\Models\Builders\PaypalWalletExperienceContextBuilder;
use PaypalServerSdkLib\Models\Builders\PurchaseUnitRequestBuilder; use PaypalServerSdkLib\Models\Builders\PurchaseUnitRequestBuilder;
use PaypalServerSdkLib\Models\PaymentSource;
use PaypalServerSdkLib\PaypalServerSdkClientBuilder; use PaypalServerSdkLib\PaypalServerSdkClientBuilder;
use think\Db; use think\Db;
use think\db\exception\DataNotFoundException; use think\db\exception\DataNotFoundException;
use think\Env;
use think\db\exception\ModelNotFoundException; use think\db\exception\ModelNotFoundException;
use think\Exception; use think\Exception;
use think\exception\DbException; use think\exception\DbException;
@@ -49,6 +51,12 @@ class Order extends base{
if(!$rule->check($data)){ if(!$rule->check($data)){
return jsonError($rule->getError()); return jsonError($rule->getError());
} }
if(isset($data['type'])&&$data['type']==2){
$payType = 2;
}else{
$payType = 0;
}
$frag = [];
$article_info = $this->article_obj->where("article_id",$data['article_id'])->find(); $article_info = $this->article_obj->where("article_id",$data['article_id'])->find();
if($article_info['is_buy']==1){ if($article_info['is_buy']==1){
return jsonError("paid"); return jsonError("paid");
@@ -56,34 +64,88 @@ class Order extends base{
$journal_info = $this->journal_obj->where("journal_id",$article_info['journal_id'])->find(); $journal_info = $this->journal_obj->where("journal_id",$article_info['journal_id'])->find();
$check = $this->order_obj->where("user_id",$article_info['user_id'])->where("article_id",$data['article_id'])->whereIn("state",[0,1])->find(); $check = $this->order_obj->where("user_id",$article_info['user_id'])->where("article_id",$data['article_id'])->whereIn("state",[0,1])->find();
if($check){ if($check){
$re['detail'] = $check; if($payType==0){
$ii = $this->createPaypalOrder($check['real_fee']); $ii = $this->createPaypalOrder($check['real_fee']);
if(!isset($ii['jsonResponse']['status'])||$ii['jsonResponse']['status']!="CREATED"){ // if(!isset($ii['jsonResponse']['status'])||$ii['jsonResponse']['status']!="CREATED"){
return jsonError("system error!"); if(!isset($ii['jsonResponse']['status'])){
return jsonError("system error!");
}else{
$check['paypal_order_id'] = $ii['jsonResponse']['id'];
}
$check['pay_type'] = 0;
$this->order_obj->update($check);
$re['detail'] = $check;
$re['paypal'] = $ii;
$re['paystation'] = null;
return jsonSuccess($re);
}else{ }else{
$check['paypal_order_id'] = $ii['jsonResponse']['id']; $cache_sn = 'TMR'.date('Ymd') . strtoupper(bin2hex(random_bytes(8)));
$accessToken = createPayStationToken();
$data_array = [
'paystation_id' => Env::get("paystation.client_id"),
'gateway_id' => "PAYSTATION",//GATEWAY_ID,
"order_name" => $cache_sn,
"amount" =>100,
"test_mode"=>true
];
$data = json_encode($data_array);
$purchase = postPayStationQuery('v1/payme/purchases', $accessToken, $data);
$paystation_res = object_to_array(json_decode($purchase));
$check['pay_type'] = 2;
$check['paystation_url'] = $paystation_res['payme_url'];
$this->order_obj->update($check);
$re['detail'] = $check;
$re['paypal'] = null;
$re['paystation'] = $paystation_res;
return jsonSuccess($re);
} }
$this->order_obj->update($check);
$re['paypal'] = $ii;
return jsonSuccess($re);
} }
$insert['order_sn'] = 'TMR'.date('Ymd') . strtoupper(bin2hex(random_bytes(8)));
$insert['user_id'] = $article_info['user_id']; if($payType==0){//支付方式为paypal
$insert['type'] = 0; $insert['order_sn'] = 'TMR'.date('Ymd') . strtoupper(bin2hex(random_bytes(8)));
$insert["article_id"] = $data['article_id']; $insert['user_id'] = $article_info['user_id'];
$insert['order_fee'] = $journal_info['fee']; $insert['type'] = 0;
$insert['real_fee'] = $journal_info['fee']; $insert["article_id"] = $data['article_id'];
$paypal = $this->createPaypalOrder($insert['real_fee']); $insert['order_fee'] = $journal_info['fee'];
if(!isset($paypal['jsonResponse']['status'])||$paypal['jsonResponse']['status']!="CREATED"){ $insert['real_fee'] = $journal_info['fee'];
return jsonError("system error"); $frag["paypal"] = $this->createPaypalOrder($insert['real_fee']);
}else{ if(!isset($paypal['jsonResponse']['status'])||$paypal['jsonResponse']['status']!="CREATED"){
$insert['paypal_order_id'] = $paypal['jsonResponse']['id']; return jsonError("system error");
}else{
$insert['paypal_order_id'] = $paypal['jsonResponse']['id'];
}
$insert['ctime'] = time();
$id = $this->order_obj->insertGetId($insert);
$frag['paystation'] = null;
}elseif ($payType==2){//支付方式为paystation
$ca_sn = 'TMR'.date('Ymd') . strtoupper(bin2hex(random_bytes(8)));
$insert['order_sn'] = $ca_sn;
$insert['user_id'] = $article_info['user_id'];
$insert['type'] = 2;
$insert["article_id"] = $data['article_id'];
$insert['order_fee'] = $journal_info['fee'];
$insert['real_fee'] = $journal_info['fee'];
$accessToken = createPayStationToken();
$data_array = [
'paystation_id' => Env::get("paystation.client_id"),
'gateway_id' => "PAYSTATION",//GATEWAY_ID,
"order_name" => $ca_sn,
"amount" =>100,
"test_mode"=>true
];
$data = json_encode($data_array);
$purchase = postPayStationQuery('v1/payme/purchases', $accessToken, $data);
$paystation_res = object_to_array(json_decode($purchase));
$insert['paystation_url'] = $paystation_res['payme_url'];
$insert['ctime'] = time();
$id = $this->order_obj->insertGetId($insert);
$frag['paystation'] = $paystation_res;
$frag["paypal"] = null;
}else{//暂时不处理,其他情况
return jsonError("pay_type error");
} }
$insert['ctime'] = time(); $frag['detail'] = $this->order_obj->where("order_id",$id)->find();
$id = $this->order_obj->insertGetId($insert); return jsonSuccess($frag);
$re['detail'] = $this->order_obj->where("order_id",$id)->find();
$re['paypal'] = $paypal;
return jsonSuccess($re);
} }
@@ -150,12 +212,15 @@ class Order extends base{
PurchaseUnitRequestBuilder::init( PurchaseUnitRequestBuilder::init(
AmountWithBreakdownBuilder::init("USD", $fee)->build() AmountWithBreakdownBuilder::init("USD", $fee)->build()
)->build(), )->build(),
// PaymentSourceBuilder::init()->paypal(PaypalWalletBuilder::init()->experienceContext( ])
// PaypalWalletExperienceContextBuilder::init()->returnUrl("https://www.baidu.com")->build() ->paymentSource(
// )->build())->build(), PaymentSourceBuilder::init()->paypal(
])->applicationContext( PaypalWalletBuilder::init()->experienceContext(
OrderApplicationContextBuilder::init()->returnUrl("www.baidu.com")->build() PaypalWalletExperienceContextBuilder::init()->returnUrl("https://www.baidu.com")->build()
)->build(), )->build()
)->build()
)
->build(),
]; ];
$apiResponse = $client->getOrdersController()->ordersCreate($orderBody); $apiResponse = $client->getOrdersController()->ordersCreate($orderBody);

View File

@@ -1130,7 +1130,7 @@ class Preaccept extends Base
$accessToken = $this->payStationAccessToken(); $accessToken = $this->payStationAccessToken();
$data_array = [ $data_array = [
'paystation_id' => Env::get("paystation.client_id"), 'paystation_id' => Env::get("paystation.client_id"),
'gateway_id' => "v1",//GATEWAY_ID, 'gateway_id' => "PAYSTATION",//GATEWAY_ID,
'merchant_session' => "myTestSN001", 'merchant_session' => "myTestSN001",
// 'merchant_reference' => 'testReference', // 'merchant_reference' => 'testReference',
'amount' => 2000, //$20 in cents value 'amount' => 2000, //$20 in cents value
@@ -1165,35 +1165,15 @@ class Preaccept extends Base
$accessToken = $this->payStationAccessToken(); $accessToken = $this->payStationAccessToken();
$data_array = [ $data_array = [
'paystation_id' => Env::get("paystation.client_id"), 'paystation_id' => Env::get("paystation.client_id"),
'gateway_id' => "v1",//GATEWAY_ID, 'gateway_id' => "PAYSTATION",//GATEWAY_ID,
"order_name" => "mytestOrderSn", "order_name" => "mytestOrderSn11",
"amount" =>200, "amount" =>100,
"test_mode"=>true "test_mode"=>true
]; ];
$data = json_encode($data_array); $data = json_encode($data_array);
$purchase = $this->postRequest('v1/payme/purchases', $accessToken, $data); $purchase = $this->postRequest('v1/payme/purchases', $accessToken, $data);
$r= object_to_array(json_decode($purchase));
return jsonSuccess($purchase); return jsonSuccess($r);
}
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(){ private function payStationAccessToken(){
@@ -1201,7 +1181,7 @@ class Preaccept extends Base
'client_id' => Env::get("paystation.client_id"), 'client_id' => Env::get("paystation.client_id"),
'client_secret' => Env::get("paystation.client_secret"), 'client_secret' => Env::get("paystation.client_secret"),
'grant_type' => 'client_credentials', 'grant_type' => 'client_credentials',
'scope' => "mytest" 'scope' => "read write"
]; ];
$accessTokenUrl = Env::get("paystation.api_url") . '/oauth/token'; $accessTokenUrl = Env::get("paystation.api_url") . '/oauth/token';
$curlHandle = curl_init($accessTokenUrl); $curlHandle = curl_init($accessTokenUrl);

View File

@@ -929,6 +929,76 @@ function aliemail($email,$title,$content,$has_hb=1){
return $res; return $res;
} }
function createPayStationToken(){
$bodyParams = [
'client_id' => Env::get("paystation.client_id"),
'client_secret' => Env::get("paystation.client_secret"),
'grant_type' => 'client_credentials',
'scope' => "read write"
];
$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;
}
}
}
function postPayStationQuery($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;
}
/** /**
* 增加usermsg * 增加usermsg
*/ */