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 * @throws DbException * @throws PDOException * @throws Exception * @throws \Exception */ public function creatArticleOrder(){ $data = $this->request->post(); $rule = new Validate([ "article_id"=>"require" ]); if(!$rule->check($data)){ 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(); if($article_info['is_buy']==1){ return jsonError("paid"); } $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(); if($check){ if($payType==0){ $ii = $this->createPaypalOrder($check['real_fee']); // if(!isset($ii['jsonResponse']['status'])||$ii['jsonResponse']['status']!="CREATED"){ 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{ $re['detail'] = $check; $re['paypal'] = null; $re['paystation'] = $check['ps_id']==0?null:$this->paystation_obj->where("ps_id",$check['ps_id'])->find(); return jsonSuccess($re); } } if($payType==0){//支付方式为paypal $insert['order_sn'] = 'TMR'.date('Ymd') . strtoupper(bin2hex(random_bytes(8))); $insert['user_id'] = $article_info['user_id']; $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'])){ 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))); $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, "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/hosted/purchases', $accessToken, $data); $paystation_res = object_to_array(json_decode($purchase)); // 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{//暂时不处理,其他情况 return jsonError("pay_type error"); } $frag['detail'] = $this->order_obj->where("order_id",$id)->find(); return jsonSuccess($frag); } public function getUserOrder(){ $data = $this->request->post(); $rule = new Validate([ "user_id"=>"require", "state"=>"require" ]); if(!$rule->check($data)){ return jsonError($rule->getError()); } $list = $this->order_obj->where("user_id",$data['user_id'])->where("state",$data['state'])->select(); foreach ($list as $k=>$v){ $article = $this->article_obj->where("article_id",$v['article_id'])->find(); $list[$k]['article_detail'] = $article; $list[$k]['journal_detail'] = $this->journal_obj->where("journal_id",$article['journal_id'])->find(); } $re['list'] = $list; return jsonSuccess($re); } private function handleResponse($response) { $jsonResponse = json_decode($response->getBody(), true); return [ "jsonResponse" => $jsonResponse, "httpStatusCode" => $response->getStatusCode(), ]; } public function preOrderDetail(){ $data = $this->request->post(); $rule = new Validate([ "article_id"=>"require" ]); if(!$rule->check($data)){ return jsonError($rule->getError()); } $article_info = $this->article_obj->where("article_id",$data['article_id'])->find(); $journal_info = $this->journal_obj->where("journal_id",$article_info['journal_id'])->find(); $user_info = $this->user_obj->where("user_id",$article_info['user_id'])->find(); $re['article_detail'] = $article_info; $re['journal_detail'] = $journal_info; $re['user_detail'] = $user_info; return jsonSuccess($re); } private function createPaypalOrder($fee) { $client = $this->createClient(); $orderBody = [ "body" => OrderRequestBuilder::init("CAPTURE", [ PurchaseUnitRequestBuilder::init( AmountWithBreakdownBuilder::init("USD", $fee)->build() )->build(), ]) ->paymentSource( PaymentSourceBuilder::init()->paypal( PaypalWalletBuilder::init()->experienceContext( PaypalWalletExperienceContextBuilder::init()->returnUrl("https://www.baidu.com")->build() )->build() )->build() ) ->build(), ]; $apiResponse = $client->getOrdersController()->ordersCreate($orderBody); return $this->handleResponse($apiResponse); } public function completeOrder(){ $data = $this->request->post(); $rule = new Validate([ "order_id"=>"require" ]); if(!$rule->check($data)){ return jsonError($rule->getError()); } $order_info = $this->order_obj->where("order_id",$data['order_id'])->find(); $this->captureOrder($order_info['paypal_order_id']); $this->article_obj->where("article_id",$order_info['article_id'])->update(['is_buy'=>1]); $this->order_obj->where("order_id",$data['order_id'])->update(['state'=>1]); return jsonSuccess([]); } public function mytest(){ $data = $this->request->post(); $rule = new Validate([ "order_id"=>"require" ]); if(!$rule->check($data)){ return jsonError($rule->getError()); } $info = $this->order_obj->where("order_id",$data['order_id'])->find(); $re = $this->getOrderStatus($info['paypal_order_id']); return jsonSuccess($re); } private function getOrderStatus($orderId){ $client = $this->createClient(); return $client->getOrdersController()->ordersGet(["id"=>$orderId]); } private function createClient(){ return PaypalServerSdkClientBuilder::init() ->clientCredentialsAuthCredentials( ClientCredentialsAuthCredentialsBuilder::init( $this->PAYPAL_CLIENT_ID, $this->PAYPAL_CLIENT_SECRET ) ) ->environment(Environment::SANDBOX) ->build(); } private function captureOrder($orderID) { $client = $this->createClient(); $captureBody = [ "id" => $orderID, ]; $apiResponse = $client->getOrdersController()->ordersCapture($captureBody); return $this->handleResponse($apiResponse); } }