diff --git a/application/api/controller/Order.php b/application/api/controller/Order.php index 2e9746d..415c2c5 100644 --- a/application/api/controller/Order.php +++ b/application/api/controller/Order.php @@ -12,9 +12,11 @@ use PaypalServerSdkLib\Models\Builders\PaymentSourceBuilder; use PaypalServerSdkLib\Models\Builders\PaypalWalletBuilder; use PaypalServerSdkLib\Models\Builders\PaypalWalletExperienceContextBuilder; use PaypalServerSdkLib\Models\Builders\PurchaseUnitRequestBuilder; +use PaypalServerSdkLib\Models\PaymentSource; use PaypalServerSdkLib\PaypalServerSdkClientBuilder; use think\Db; use think\db\exception\DataNotFoundException; +use think\Env; use think\db\exception\ModelNotFoundException; use think\Exception; use think\exception\DbException; @@ -49,6 +51,12 @@ class Order extends base{ 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"); @@ -56,34 +64,88 @@ class Order extends base{ $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){ - $re['detail'] = $check; - $ii = $this->createPaypalOrder($check['real_fee']); - if(!isset($ii['jsonResponse']['status'])||$ii['jsonResponse']['status']!="CREATED"){ - return jsonError("system error!"); + 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{ - $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']; - $insert['type'] = 0; - $insert["article_id"] = $data['article_id']; - $insert['order_fee'] = $journal_info['fee']; - $insert['real_fee'] = $journal_info['fee']; - $paypal = $this->createPaypalOrder($insert['real_fee']); - if(!isset($paypal['jsonResponse']['status'])||$paypal['jsonResponse']['status']!="CREATED"){ - return jsonError("system error"); - }else{ - $insert['paypal_order_id'] = $paypal['jsonResponse']['id']; + + 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["article_id"] = $data['article_id']; + $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"){ + 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(); - $id = $this->order_obj->insertGetId($insert); - $re['detail'] = $this->order_obj->where("order_id",$id)->find(); - $re['paypal'] = $paypal; - return jsonSuccess($re); + $frag['detail'] = $this->order_obj->where("order_id",$id)->find(); + return jsonSuccess($frag); } @@ -150,12 +212,15 @@ class Order extends base{ PurchaseUnitRequestBuilder::init( AmountWithBreakdownBuilder::init("USD", $fee)->build() )->build(), -// PaymentSourceBuilder::init()->paypal(PaypalWalletBuilder::init()->experienceContext( -// PaypalWalletExperienceContextBuilder::init()->returnUrl("https://www.baidu.com")->build() -// )->build())->build(), - ])->applicationContext( - OrderApplicationContextBuilder::init()->returnUrl("www.baidu.com")->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); diff --git a/application/api/controller/Preaccept.php b/application/api/controller/Preaccept.php index e713e19..66b0bf1 100644 --- a/application/api/controller/Preaccept.php +++ b/application/api/controller/Preaccept.php @@ -1130,7 +1130,7 @@ class Preaccept extends Base $accessToken = $this->payStationAccessToken(); $data_array = [ 'paystation_id' => Env::get("paystation.client_id"), - 'gateway_id' => "v1",//GATEWAY_ID, + 'gateway_id' => "PAYSTATION",//GATEWAY_ID, 'merchant_session' => "myTestSN001", // 'merchant_reference' => 'testReference', 'amount' => 2000, //$20 in cents value @@ -1165,35 +1165,15 @@ class Preaccept extends Base $accessToken = $this->payStationAccessToken(); $data_array = [ 'paystation_id' => Env::get("paystation.client_id"), - 'gateway_id' => "v1",//GATEWAY_ID, - "order_name" => "mytestOrderSn", - "amount" =>200, + 'gateway_id' => "PAYSTATION",//GATEWAY_ID, + "order_name" => "mytestOrderSn11", + "amount" =>100, "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']; - } - } + $r= object_to_array(json_decode($purchase)); + return jsonSuccess($r); } private function payStationAccessToken(){ @@ -1201,7 +1181,7 @@ class Preaccept extends Base 'client_id' => Env::get("paystation.client_id"), 'client_secret' => Env::get("paystation.client_secret"), 'grant_type' => 'client_credentials', - 'scope' => "mytest" + 'scope' => "read write" ]; $accessTokenUrl = Env::get("paystation.api_url") . '/oauth/token'; $curlHandle = curl_init($accessTokenUrl); diff --git a/application/common.php b/application/common.php index 07dbd72..a4249de 100644 --- a/application/common.php +++ b/application/common.php @@ -929,6 +929,76 @@ function aliemail($email,$title,$content,$has_hb=1){ 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 */