我在 symfony 中创建了这段代码:
#[Route('/buy/{subjectId}', name: 'buy_subject', methods: ['POST'])]
public function buySubject(Request $request, $subjectId, ManagerRegistry $managerRegistry, $stripeSK): Response
{
$entityManager = $managerRegistry->getManager();
$subject = $entityManager->getRepository(Subject::class)->find($subjectId);
error_log('Subject ID: ' . $subjectId);
if (!$subject) {
throw $this->createNotFoundException('Subject not found');
}
dump($subject);
Stripe::setApiKey($stripeSK);
error_log('Stripe API Key: ' . $stripeSK);
$totalAmount = $subject->getSubjectPrice() ;
error_log('Total Amount: ' . $totalAmount);
$checkoutSession = Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'product_data' => [
'name' => $subject->getSubjectName(),
],
'unit_amount' => $totalAmount,
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $this->generateUrl('subject_success'),
'cancel_url' => $this->generateUrl('subject_cancel'),
]);
return $this->redirect($checkoutSession->url, 303);
}
仅出现此错误:
[申请] 4月30日 23:52:43 |CRITICA| REQUES 未捕获的 PHP 异常 Stripe\Exception\InvalidRequestException:“不是有效的 URL” C:\Users\user\Downloads\TW2.0-Starter-Project-main endor\stripe\stripe-php\lib\Exception\ApiErrorException.php 38号线
我希望点击“购买”按钮会将我带到条纹结账页面
因为您的实现使用
generateUrl
生成的 url 不会返回绝对 URL,仅返回绝对路径。
需要传递第三个参数来生成绝对URL
$this->generateUrl('subject_success', [], UrlGeneratorInterface::ABSOLUTE_URL);