1
This commit is contained in:
150
application/api/controller/Order.php
Normal file
150
application/api/controller/Order.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\api\controller\Base;
|
||||
use PaypalServerSdkLib\Authentication\ClientCredentialsAuthCredentialsBuilder;
|
||||
use PaypalServerSdkLib\Environment;
|
||||
use PaypalServerSdkLib\Models\Builders\AmountWithBreakdownBuilder;
|
||||
use PaypalServerSdkLib\Models\Builders\OrderRequestBuilder;
|
||||
use PaypalServerSdkLib\Models\Builders\PurchaseUnitRequestBuilder;
|
||||
use PaypalServerSdkLib\PaypalServerSdkClientBuilder;
|
||||
use think\Db;
|
||||
use think\Queue;
|
||||
use think\Validate;
|
||||
|
||||
class Order extends base{
|
||||
protected $PAYPAL_CLIENT_ID="ARyoAhBNlTMDEBb6QvJYmK0gA4cfSS6WY0Vr2uJhX3NOe7V9qVCJuNwuRHRO09WGcTgS5AkuTIgRZDcx";
|
||||
protected $PAYPAL_CLIENT_SECRET="EPrmkePbt1hFGssdQkAHM11AACGKzcHoc-RjmyomOWhKJSpTDXlLdpwzjgM24XajiwwAIXyvbYd8j7Uo";
|
||||
|
||||
public function __construct(\think\Request $request = null)
|
||||
{
|
||||
parent::__construct($request);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function creatArticleOrder(){
|
||||
$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();
|
||||
$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;
|
||||
return jsonSuccess($re);
|
||||
}
|
||||
$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'];
|
||||
$insert['ctime'] = time();
|
||||
$id = $this->order_obj->insertGetId($insert);
|
||||
$re['detail'] = $this->order_obj->where("order_id",$id)->find();
|
||||
return jsonSuccess($re);
|
||||
}
|
||||
|
||||
|
||||
|
||||
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();
|
||||
$re['list'] = $list;
|
||||
|
||||
return jsonSuccess($re);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private function handleResponse($response)
|
||||
{
|
||||
$jsonResponse = json_decode($response->getBody(), true);
|
||||
return [
|
||||
"jsonResponse" => $jsonResponse,
|
||||
"httpStatusCode" => $response->getStatusCode(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function mytest(){
|
||||
$url = 'https://api-m.sandbox.paypal.com/v1/oauth2/token';
|
||||
|
||||
// PayPal client credentials (replace these with your own credentials)
|
||||
$clientId = $this->PAYPAL_CLIENT_ID; // 替换为您的 client_id
|
||||
$clientSecret = $this->PAYPAL_CLIENT_SECRET; // 替换为您的 client_secret
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v1/oauth2/token'); // PayPal 沙盒环境的 URL
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回响应而不是输出
|
||||
curl_setopt($ch, CURLOPT_POST, true); // 设置为 POST 请求
|
||||
curl_setopt($ch, CURLOPT_USERPWD, $clientId . ':' . $clientSecret); // 设置基本认证(Basic Auth)
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Content-Type: application/x-www-form-urlencoded' // 设置 Content-Type 为 x-www-form-urlencoded
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials'); // 设置表单参数
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
$response = curl_exec($ch);
|
||||
if (curl_errno($ch)) {
|
||||
// 如果有错误,输出错误信息
|
||||
echo 'Error: ' . curl_error($ch);
|
||||
} else {
|
||||
// 请求成功,解析响应
|
||||
$responseData = json_decode($response, true); // 将 JSON 响应转为数组
|
||||
if (isset($responseData['access_token'])) {
|
||||
echo "Access Token: " . $responseData['access_token']; // 输出获取的 Access Token
|
||||
} else {
|
||||
echo "Error: Unable to retrieve access token. Response: " . json_encode($responseData); // 如果没有获取到 Token,输出错误信息
|
||||
}
|
||||
}
|
||||
curl_close($ch);
|
||||
}
|
||||
|
||||
|
||||
public function mytest1(){
|
||||
$re = $this->createPaypalOrder();
|
||||
return jsonSuccess($re);
|
||||
}
|
||||
|
||||
private function createPaypalOrder()
|
||||
{
|
||||
$client = PaypalServerSdkClientBuilder::init()
|
||||
->clientCredentialsAuthCredentials(
|
||||
ClientCredentialsAuthCredentialsBuilder::init(
|
||||
$this->PAYPAL_CLIENT_ID,
|
||||
$this->PAYPAL_CLIENT_SECRET
|
||||
)
|
||||
)
|
||||
->environment(Environment::SANDBOX)
|
||||
->build();
|
||||
|
||||
$orderBody = [
|
||||
"body" => OrderRequestBuilder::init("CAPTURE", [
|
||||
PurchaseUnitRequestBuilder::init(
|
||||
AmountWithBreakdownBuilder::init("USD", "100")->build()
|
||||
)->build(),
|
||||
])->build(),
|
||||
];
|
||||
$apiResponse = $client->getOrdersController()->ordersCreate($orderBody);
|
||||
|
||||
return $this->handleResponse($apiResponse);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user