From 3d64e9383995162606ed7bf9d240b20702a08bad Mon Sep 17 00:00:00 2001 From: wangjinlei <751475802@qq.com> Date: Tue, 25 Feb 2025 16:02:09 +0800 Subject: [PATCH] 1 --- application/api/controller/Base.php | 2 + application/api/controller/Order.php | 206 +++++++++++++++++++-------- 2 files changed, 145 insertions(+), 63 deletions(-) diff --git a/application/api/controller/Base.php b/application/api/controller/Base.php index fa8eee2..1aaa814 100644 --- a/application/api/controller/Base.php +++ b/application/api/controller/Base.php @@ -86,6 +86,7 @@ class Base extends Controller protected $article_main_check_obj = ""; protected $major_to_user_obj = ""; protected $major_to_article_obj = ""; + protected $paystation_obj = ""; public function __construct(\think\Request $request = null) @@ -167,6 +168,7 @@ class Base extends Controller $this->article_main_check_obj = Db::name("article_main_check"); $this->major_to_user_obj = Db::name("major_to_user"); $this->major_to_article_obj = Db::name("major_to_article"); + $this->paystation_obj = Db::name("paystation"); } diff --git a/application/api/controller/Order.php b/application/api/controller/Order.php index 415c2c5..600bbde 100644 --- a/application/api/controller/Order.php +++ b/application/api/controller/Order.php @@ -19,6 +19,7 @@ use think\db\exception\DataNotFoundException; use think\Env; use think\db\exception\ModelNotFoundException; use think\Exception; +use think\Request; use think\exception\DbException; use think\exception\PDOException; use think\Queue; @@ -31,10 +32,118 @@ class Order extends base{ public function __construct(\think\Request $request = null) { parent::__construct($request); + } + + public function paystationTest(){ + $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, + "merchant_session" => $sn, + "merchant_reference"=>$sn, + "amount" =>100, + "return_url"=>"https://www.tmrjournals.com/", + "response_url"=>"http://api.tmrjournals.com/public/index.php/api/Order/completePaystation" + ]; + $data = json_encode($data_array); + $purchase = postPayStationQuery('v1/hosted/purchases', $accessToken, $data); + $paystation_res = object_to_array(json_decode($purchase)); + return jsonSuccess($paystation_res); + } + + + public function completePaystation(Request $request){ + + // 获取请求的原始数据(Paystation 发送的 payload) + $payload = $request->getContent(); + + + $filePath = ROOT_PATH . '/payload_log.txt'; + + // 将payload写入文本文件 + file_put_contents($filePath, $payload . PHP_EOL, FILE_APPEND); + + + + // 获取请求头 +// $headers = $request->header(); +// +// // 获取HMAC签名和时间戳 +// $hmac_signature = $headers['x-signature'] ?? ''; +// $timestamp = $headers['x-timestamp'] ?? ''; +// +// // 使用HMAC密钥验证签名 +// $get_hmac = hash_hmac('sha512', $timestamp . $payload, config('paystation.HMAC_KEY')); +// +// if ($hmac_signature != $get_hmac) { +// Log::error('HMAC验证失败', ['timestamp' => $timestamp, 'hmac_signature' => $hmac_signature, 'calculated_hmac' => $get_hmac]); +// return 'HMAC verification failed'; +// } + + // 解析JSON格式的payload + $data = json_decode($payload); + + + + + // 检查result字段是否为空 + if (empty($data->result)) { + return jsonError("Paystation responds with no results"); + } else { + // 交易成功或失败的处理 + + + + + + + if ($data->result->success) { + return jsonSuccess([]); + } else { + return jsonError('Transaction failed'); + } + } + + + + + +// $sn = $request->param("sn"); +// $order_info = $this->order_obj->where("order_sn",$sn)->find(); +// +// $this->article_obj->where("article_id",$order_info['article_id'])->update(['is_buy'=>1]); +// $this->order_obj->where("order_sn",$order_info['order_sn'])->update(['state'=>1]); +// +// return jsonSuccess([]); + } + + + + public function testPaystationLookup(){ + $accessToken = createPayStationToken(); + $curl = curl_init(); + curl_setopt_array($curl, array( + CURLOPT_URL => 'https://api.paystation.co.nz/v1/transactions?paystation_id=616562&gateway_id=PAYSTATION&merchant_session=TMR20250225E4F6EA2F38793055&transaction_id=217661437', + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => '', + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_HTTPHEADER => array( + 'Authorization: Bearer '.$accessToken + ) + )); + $response = curl_exec($curl); + curl_close($curl); + return jsonSuccess(object_to_array(json_decode($response))); } + /** * @throws DataNotFoundException * @throws ModelNotFoundException @@ -79,24 +188,9 @@ class Order extends base{ $re['paystation'] = null; return jsonSuccess($re); }else{ - $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; + $re['paystation'] = $check['ps_id']==0?null:$this->paystation_obj->where("ps_id",$check['ps_id'])->find(); return jsonSuccess($re); } } @@ -104,12 +198,13 @@ class Order extends base{ if($payType==0){//支付方式为paypal $insert['order_sn'] = 'TMR'.date('Ymd') . strtoupper(bin2hex(random_bytes(8))); $insert['user_id'] = $article_info['user_id']; - $insert['type'] = 0; + $insert['pay_type'] = 0; $insert["article_id"] = $data['article_id']; + $insert['currency'] = "USD"; $insert['order_fee'] = $journal_info['fee']; $insert['real_fee'] = $journal_info['fee']; $frag["paypal"] = $this->createPaypalOrder($insert['real_fee']); - if(!isset($paypal['jsonResponse']['status'])||$paypal['jsonResponse']['status']!="CREATED"){ + if(!isset($paypal['jsonResponse']['status'])){ return jsonError("system error"); }else{ $insert['paypal_order_id'] = $paypal['jsonResponse']['id']; @@ -119,26 +214,43 @@ class Order extends base{ $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']; + $insert1['order_sn'] = $ca_sn; + $insert1['user_id'] = $article_info['user_id']; + $insert1['pay_type'] = 2; + $insert1["article_id"] = $data['article_id']; + $insert1["currency"] = "USD"; + $insert1['order_fee'] = $journal_info['fee']; + $insert1['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 + "merchant_session" => $ca_sn, + "merchant_reference"=>$ca_sn, + "amount" =>(int)((((int)$journal_info['fee'])*726/416)*100), +// "currency"=>"USD",//目前paystation仅支持nzd + "return_url"=>"https://www.tmrjournals.com/", + "response_url"=>"http://api.tmrjournals.com/public/index.php/api/Order/completePaystation" ]; $data = json_encode($data_array); - $purchase = postPayStationQuery('v1/payme/purchases', $accessToken, $data); + $purchase = postPayStationQuery('v1/hosted/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); + +// return jsonSuccess($paystation_res); + $ps_insert['transaction_id'] = $paystation_res['transaction_id']; + $ps_insert['session_id'] = $paystation_res['session_id']; + $ps_insert['paystation_id'] = $paystation_res['paystation_id']; + $ps_insert['currency'] = $paystation_res['currency']; + $ps_insert['amount'] = $paystation_res['amount']; + $ps_insert['merchant_session'] = $paystation_res['merchant_session']; + $ps_insert['request_time'] = $paystation_res['request_time']; + $ps_insert['payment_url'] = $paystation_res['payment_url']; + $ps_insert['data'] = json_encode($paystation_res); + $ps_id = $this->paystation_obj->insertGetId($ps_insert); + $insert1['ps_id'] = $ps_id; + $insert1['paystation_url'] = $paystation_res['payment_url']; + $insert1['ctime'] = time(); + $id = $this->order_obj->insertGetId($insert1); $frag['paystation'] = $paystation_res; $frag["paypal"] = null; }else{//暂时不处理,其他情况 @@ -196,13 +308,6 @@ class Order extends base{ return jsonSuccess($re); } - - - public function mytt(){ - $re = $this->createPaypalOrder(600); - return jsonSuccess($re); - } - private function createPaypalOrder($fee) { $client = $this->createClient(); @@ -228,31 +333,6 @@ class Order extends base{ } - - - - public function testCreat(){ - $client = PaypalServerSdkClientBuilder::init() - ->clientCredentialsAuthCredentials( - ClientCredentialsAuthCredentialsBuilder::init( - "AeTurTFJvsivep-zB7vigyuBHX_cyzLqY5K0GCHLaOrs0eHaXA5V-fwJoVEFjlvN1jK_IqYoEbKEBKiH", - "EA-F_RYl1oJBTjHYn87L7vnNv-mWp5yRyTjmkVCOD_bu5T9nfm0E6rfNRKALj1n6AH70QKqB-mmO6tsE" - ) - ) - ->environment(Environment::SANDBOX) - ->build(); - - - - $orderBody = [ - "body"=> OrderRequestBuilder::init("CAPTURE",[ - - ] - )->build() - ]; - } - - public function completeOrder(){ $data = $this->request->post(); $rule = new Validate([