This commit is contained in:
wangjinlei
2025-01-07 10:07:35 +08:00
parent a54a837670
commit 28a0c44dab
5 changed files with 203 additions and 73 deletions

View File

@@ -5,7 +5,12 @@ use app\api\controller\Base;
use PaypalServerSdkLib\Authentication\ClientCredentialsAuthCredentialsBuilder;
use PaypalServerSdkLib\Environment;
use PaypalServerSdkLib\Models\Builders\AmountWithBreakdownBuilder;
use PaypalServerSdkLib\Models\Builders\ExperienceContextBuilder;
use PaypalServerSdkLib\Models\Builders\OrderApplicationContextBuilder;
use PaypalServerSdkLib\Models\Builders\OrderRequestBuilder;
use PaypalServerSdkLib\Models\Builders\PaymentSourceBuilder;
use PaypalServerSdkLib\Models\Builders\PaypalWalletBuilder;
use PaypalServerSdkLib\Models\Builders\PaypalWalletExperienceContextBuilder;
use PaypalServerSdkLib\Models\Builders\PurchaseUnitRequestBuilder;
use PaypalServerSdkLib\PaypalServerSdkClientBuilder;
use think\Db;
@@ -13,8 +18,8 @@ use think\Queue;
use think\Validate;
class Order extends base{
protected $PAYPAL_CLIENT_ID="ARyoAhBNlTMDEBb6QvJYmK0gA4cfSS6WY0Vr2uJhX3NOe7V9qVCJuNwuRHRO09WGcTgS5AkuTIgRZDcx";
protected $PAYPAL_CLIENT_SECRET="EPrmkePbt1hFGssdQkAHM11AACGKzcHoc-RjmyomOWhKJSpTDXlLdpwzjgM24XajiwwAIXyvbYd8j7Uo";
protected $PAYPAL_CLIENT_ID="ATqBigrhcNdqR8J83aDjTOoJHsAVz0U45JRY4H0stcEcv0mQrMDHQmyrydQInYd1w4lJ1ee3Wsblm2WP";
protected $PAYPAL_CLIENT_SECRET="EJL5CtykvRiMZ1apKrX4zDX03d01CuxgrUi6-D7K45NgNQAGGY0Kj0Du9tL04Zc3aDBgxgZ4JLErSQp3";
public function __construct(\think\Request $request = null)
{
@@ -32,10 +37,21 @@ class Order extends base{
return jsonError($rule->getError());
}
$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){
$re['detail'] = $check;
$ii = $this->createPaypalOrder($check['real_fee']);
if(!isset($ii['jsonResponse']['status'])||$ii['jsonResponse']['status']!="CREATED"){
return jsonError("system error!");
}else{
$check['paypal_order_id'] = $ii['jsonResponse']['id'];
}
$this->order_obj->update($check);
$re['paypal'] = $ii;
return jsonSuccess($re);
}
$insert['user_id'] = $article_info['user_id'];
@@ -43,9 +59,16 @@ class Order extends base{
$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'];
}
$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);
}
@@ -61,16 +84,16 @@ class Order extends base{
return jsonError($rule->getError());
}
$list = $this->order_obj->where("user_id",$data['user_id'])->where("state",$data['state'])->select();
$re['list'] = $list;
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);
@@ -80,65 +103,45 @@ class Order extends base{
];
}
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
public function preOrderDetail(){
$data = $this->request->post();
$rule = new Validate([
"article_id"=>"require"
]);
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输出错误信息
}
if(!$rule->check($data)){
return jsonError($rule->getError());
}
curl_close($ch);
}
public function mytest1(){
$re = $this->createPaypalOrder();
$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()
public function mytt(){
$re = $this->createPaypalOrder(600);
return jsonSuccess($re);
}
private function createPaypalOrder($fee)
{
$client = PaypalServerSdkClientBuilder::init()
->clientCredentialsAuthCredentials(
ClientCredentialsAuthCredentialsBuilder::init(
$this->PAYPAL_CLIENT_ID,
$this->PAYPAL_CLIENT_SECRET
)
)
->environment(Environment::SANDBOX)
->build();
$client = $this->createClient();
$orderBody = [
"body" => OrderRequestBuilder::init("CAPTURE", [
PurchaseUnitRequestBuilder::init(
AmountWithBreakdownBuilder::init("USD", "100")->build()
AmountWithBreakdownBuilder::init("USD", $fee)->build()
)->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(),
];
$apiResponse = $client->getOrdersController()->ordersCreate($orderBody);
@@ -147,4 +150,89 @@ 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([
"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);
}
}