作者 lyh

gx数据

... ... @@ -79,13 +79,13 @@ class PayStripeApi
* @method :post
* @time :2024/12/24 10:38
*/
public function createPaymentIntent($amount, $currency = 'usd')
public function createPaymentIntent($amount, $currency = 'usd', $paymentMethodTypes = 'card')
{
$url = "https://api.stripe.com/v1/payment_intents";
$data = [
'amount' => $amount,
'currency' => $currency,
'payment_method_types[]' => $this->currency_types[$currency][0],
'payment_method_types[]' => $paymentMethodTypes,
];
return $this->sendRequest($url, 'POST', $data);
}
... ... @@ -104,87 +104,6 @@ class PayStripeApi
}
/**
* @remark :创建客户
* @name :createCustomer
* @author :lyh
* @method :post
* @time :2024/12/24 10:41
*/
public function createCustomer($name, $email, $description = '')
{
$url = "https://api.stripe.com/v1/customers";
$data = [
'name' => $name,
'email' => $email,
'description' => $description
];
return $this->sendRequest($url, 'POST', $data);
}
/**
* @remark :查询客户
* @name :retrieveCustomer
* @author :lyh
* @method :post
* @time :2024/12/24 10:41
*/
public function retrieveCustomer($customerId)
{
$url = "https://api.stripe.com/v1/customers/{$customerId}";
return $this->sendRequest($url, 'GET');
}
/**
* @remark :删除客户
* @name :deleteCustomer
* @author :lyh
* @method :post
* @time :2024/12/24 10:42
*/
public function deleteCustomer($customerId)
{
$url = "https://api.stripe.com/v1/customers/{$customerId}";
return $this->sendRequest($url, 'DELETE');
}
/**
* @remark :创建支付方法
* @name :createPaymentMethod
* @author :lyh
* @method :post
* @time :2024/12/24 10:42
*/
public function createPaymentMethod($cardNumber, $expMonth, $expYear, $cvc)
{
$url = "https://api.stripe.com/v1/payment_methods";
$data = [
'type' => 'card',
'card[number]' => $cardNumber,
'card[exp_month]' => $expMonth,
'card[exp_year]' => $expYear,
'card[cvc]' => $cvc,
];
return $this->sendRequest($url, 'POST', $data);
}
/**
* @remark :绑定支付方法到客户
* @name :attachPaymentMethodToCustomer
* @author :lyh
* @method :post
* @time :2024/12/24 10:42
*/
public function attachPaymentMethodToCustomer($paymentMethodId, $customerId)
{
$url = "https://api.stripe.com/v1/payment_methods/{$paymentMethodId}/attach";
$data = [
'customer' => $customerId,
];
return $this->sendRequest($url, 'POST', $data);
}
/**
* @remark :创建退款
* @name :createRefund
* @author :lyh
... ... @@ -198,7 +117,6 @@ class PayStripeApi
if ($amount) {
$data['amount'] = $amount;
}
return $this->sendRequest($url, 'POST', $data);
}
... ... @@ -216,50 +134,72 @@ class PayStripeApi
}
/**
* @remark :创建订阅
* @name :createSubscription
* @author :lyh
* @method :post
* @time :2024/12/24 10:42
*/
public function createSubscription($customerId, $priceId)
{
$url = "https://api.stripe.com/v1/subscriptions";
$data = [
'customer' => $customerId,
'items[0][price]' => $priceId,
];
return $this->sendRequest($url, 'POST', $data);
}
/**
* @remark :取消订阅
* @name :cancelSubscription
* @remark :处理 Webhook
* @name :handleWebhook
* @author :lyh
* @method :post
* @time :2024/12/24 10:43
*/
public function cancelSubscription($subscriptionId)
public static function handleWebhook()
{
$url = "https://api.stripe.com/v1/subscriptions/{$subscriptionId}";
return $this->sendRequest($url, 'DELETE');
try {
// Webhook 签名密钥(从 Stripe 仪表盘获取)
$endpointSecret = 'whsec_garhW2TrCIrduyM3rve9mFS2sn69B9Yt';
// 获取原始请求内容
$payload = request()->getContent();
// 获取 Stripe 签名头
$sigHeader = request()->header('Stripe-Signature');
// 验证签名
if (!self::verifySignature($payload, $sigHeader, $endpointSecret)) {
http_response_code(400);
throw new Exception('Invalid signature');
}
$event = json_decode($payload, true);
// 获取事件类型
$eventType = $event['type'];
$eventData = $event['data']['object'];
// 根据事件类型处理
switch ($eventType) {
case 'payment_intent.succeeded':
// 处理支付成功逻辑
@file_put_contents(storage_path('logs/lyh_error.log'), var_export($eventType, true) . PHP_EOL, FILE_APPEND);
@file_put_contents(storage_path('logs/lyh_error.log'), var_export($eventData, true) . PHP_EOL, FILE_APPEND);
$paymentIntentId = $eventData['id'];
break;
case 'payment_intent.payment_failed':
// 处理支付失败逻辑
$error = $eventData['last_payment_error'];
break;
case 'charge.refunded':
// 处理退款逻辑
$chargeId = $eventData['id'];
break;
default:
throw new Exception('Unhandled event type: ' . $eventType);
}
return $event;
} catch (Exception $e) {
throw new Exception('Webhook Error: ' . $e->getMessage());
}
}
/**
* @remark :处理 Webhook
* @name :handleWebhook
* @remark :验证签名
* @name :verifySignature
* @author :lyh
* @method :post
* @time :2024/12/24 10:43
* @time :2024/12/24 15:55
*/
public static function handleWebhook($payload, $sigHeader, $endpointSecret)
private function verifySignature($payload, $sigHeader, $endpointSecret)
{
try {
$event = json_decode($payload, true);
// 检查事件类型
return $event; // 返回解析后的事件
} catch (Exception $e) {
throw new Exception('Webhook Error: ' . $e->getMessage());
// 获取 Stripe 签名
$signatures = [];
if (preg_match_all('/t=\d+,v1=([a-f0-9]+)/', $sigHeader, $matches)) {
$signatures = $matches[1];
}
// 计算签名哈希值
$expectedSignature = hash_hmac('sha256', $payload, $endpointSecret);
return in_array($expectedSignature, $signatures, true);
}
}
... ...
<?php
/**
* @remark :
* @name :PayStripeController.php
* @author :lyh
* @method :post
* @time :2024/12/24 16:42
*/
namespace App\Http\Controllers\Api;
use App\Enums\Common\Code;
use App\Helper\PayStripeApi;
class PayStripeController extends BaseController
{
/**
* @remark :回调方法
* @name :handleWebhook
* @author :lyh
* @method :post
* @time :2024/12/24 15:41
*/
public function handleWebhook(){
$pay = new PayStripeApi();
$data = $pay->handleWebhook();
$this->response('success',Code::SUCCESS,$data);
}
}
... ...
<?php
/**
* @remark :
* @name :PayStripeController.php
* @author :lyh
* @method :post
* @time :2024/12/24 14:46
*/
namespace App\Http\Controllers\Bside\PayStripe;
use App\Enums\Common\Code;
use App\Helper\PayStripeApi;
use App\Http\Controllers\Bside\BaseController;
class PayStripeController extends BaseController
{
/**
* @remark :获取支付方式
* @name :getPayMethod
* @author :lyh
* @method :post
* @time :2024/12/24 14:47
*/
public function getPayMethod(){
$pay = new PayStripeApi();
$data = $pay->currency_types;
$this->response('success',Code::SUCCESS,$data);
}
/**
* @remark :创建支付意图
* @name :createPaymentIntent
* @author :lyh
* @method :post
* @time :2024/12/24 14:56
*/
public function createPaymentIntent(){
$pay = new PayStripeApi();
$data = $pay->createPaymentIntent(100,'cny');
$this->response('success',Code::SUCCESS,$data);
}
}
... ...
... ... @@ -17,7 +17,7 @@ use Illuminate\Support\Facades\Route;
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::any('stripeWebhook', [\App\Http\Controllers\Api\NoticeController::class, 'handleWebhook'])->name('api.handleWebhook');
Route::any('traffic_visit', [\App\Http\Controllers\Api\NoticeController::class, 'trafficVisit'])->name('api.traffic_visit');
Route::get('optimize_project_list', [\App\Http\Controllers\Api\PrivateController::class, 'optimizeProjectList'])->name('api.optimize_project_list');
Route::get('get_project_route', [\App\Http\Controllers\Api\PrivateController::class, 'getProjectRoute'])->name('api.get_project_route');
... ...