Файловый менеджер - Редактировать - /home/clickysoft/public_html/charliapp-v2.clickysoft.net/app/Http/Controllers/PayPalController.php
Назад
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use GuzzleHttp\Client; class PayPalController extends Controller { private $client_id; private $client_secret; private $paypalEndpoint; private $httpClient; public function __construct() { if (config('paypal.mode') == 'sandbox') { $this->client_id = config('paypal.sandbox_client_id'); $this->client_secret = config('paypal.sandbox_secret'); $this->paypalEndpoint = "https://api-m.sandbox.paypal.com/v1"; } else { $this->client_id = config('paypal.live_client_id'); $this->client_secret = config('paypal.live_secret'); $this->paypalEndpoint = "https://api-m.paypal.com/v1"; } $this->httpClient = new Client(); } public function getPayPalAccessToken() { try { $response = $this->httpClient->post($this->paypalEndpoint . '/oauth2/token', [ 'auth' => [$this->client_id, $this->client_secret], 'form_params' => [ 'grant_type' => 'client_credentials', 'ignoreCache' => 'true', 'return_authn_schemes' => 'true', 'return_client_metadata' => 'true', 'return_unconsented_scopes' => 'true', ], ]); $responseData = json_decode($response->getBody()); if (isset($responseData->error)) { return ["success" => false, "message" => $responseData->error_description]; } return [ "success" => true, "access_token" => $responseData->access_token, "expires_in" => $responseData->expires_in ]; } catch (\Exception $e) { \Log::error($e->getMessage()); return ["success" => false, "message" => "PayPal server is not responding"]; } } public function createProduct($title, $description) { try { $response = $this->httpClient->post($this->paypalEndpoint . '/catalogs/products', [ 'json' => [ 'name' => $title, 'type' => 'SERVICE', 'id' => (string) time(), 'description' => $description, 'category' => 'SOFTWARE', 'image_url' => 'https://charliiapp.com/wp-content/uploads/2022/02/Charrlii_Logo.svg', 'home_url' => 'https://charliiapp.com' ], 'headers' => [ 'Content-Type' => 'application/json', 'PayPal-Request-Id' => $this->getGuid(), 'Authorization' => 'Bearer ' . request()->get('paypalToken') ], ]); $responseData = json_decode($response->getBody()); if (isset($responseData->id)) { return ["success" => true, "paypalProductId" => $responseData->id]; } if (isset($responseData->error)) { return ["success" => false, "message" => $responseData->error_description]; } return ["success" => false, "message" => $responseData->message]; } catch (\Exception $e) { \Log::error($e->getMessage()); return ["success" => false, "message" => "Failed to create product"]; } } public function createPlan($product_id, $title, $description, $price, $interval = "MONTH") { try { $response = $this->httpClient->post($this->paypalEndpoint . '/billing/plans', [ 'json' => [ 'product_id' => $product_id, 'name' => $title . ' - ' . ($interval == "MONTH" ? "Monthly" : "Yearly"), 'description' => $description, 'status' => 'ACTIVE', 'billing_cycles' => [[ 'frequency' => [ 'interval_unit' => $interval, 'interval_count' => 1 ], 'tenure_type' => 'REGULAR', 'sequence' => 1, 'total_cycles' => 0, 'pricing_scheme' => [ 'fixed_price' => [ 'value' => $price, 'currency_code' => 'USD' ] ] ]], 'payment_preferences' => [ 'auto_bill_outstanding' => true, 'setup_fee' => [ 'value' => '0', 'currency_code' => 'USD' ], 'setup_fee_failure_action' => 'CONTINUE', 'payment_failure_threshold' => 3 ], 'taxes' => [ 'percentage' => '0', 'inclusive' => false ] ], 'headers' => [ 'Content-Type' => 'application/json', 'PayPal-Request-Id' => $this->getGuid(), 'Authorization' => 'Bearer ' . request()->get('paypalToken'), 'Prefer' => 'return=representation', ], ]); $responseData = json_decode($response->getBody()); if (isset($responseData->id)) { return ["success" => true, "paypalPlanId" => $responseData->id]; } if (isset($responseData->error)) { return ["success" => false, "message" => $responseData->error_description]; } return ["success" => false, "message" => $responseData->message]; } catch (\Exception $e) { \Log::error($e->getMessage()); return ["success" => false, "message" => "Failed to create plan"]; } } public function getAllPlans() { try { $response = $this->httpClient->get($this->paypalEndpoint . '/billing/plans', [ 'query' => [ 'page_size' => 10, 'page' => 1, 'total_required' => true ], 'headers' => [ 'Prefer' => 'return=representation', 'Authorization' => 'Bearer ' . request()->get('paypalToken') ], ]); $responseData = json_decode($response->getBody()); echo json_encode($responseData); die; } catch (\Exception $e) { \Log::error($e->getMessage()); echo json_encode(["success" => false, "message" => "Failed to fetch plans"]); die; } } private function getGuid() { // Function to generate a unique PayPal request ID if (function_exists('com_create_guid')) { return trim(com_create_guid(), '{}'); } else { mt_srand((double) microtime() * 10000); $charid = strtoupper(md5(uniqid(rand(), true))); $hyphen = chr(45); // "-" $uuid = substr($charid, 0, 8) . $hyphen . substr($charid, 8, 4) . $hyphen . substr($charid, 12, 4) . $hyphen . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12); return $uuid; } } }
| ver. 1.4 |
Github
|
.
| PHP 8.1.29 | Генерация страницы: 0 |
proxy
|
phpinfo
|
Настройка