PayPal REST API支票完成订单
我有电子商务商店,目前我正在开发 Paypal Rest API。
整个逻辑是这样的:
- 客户想要查看他的产品(checkout.php:一堆$_POST变量)
- 他可以选择他的送货和账单地址以及付款选项
- 目前有2个付款选项:PayPal和现金
- 他选择后他的付款选项(假设他选择了现金),他正在提交表单 (order.php)
- Order.php 运行completeOrder() 方法,该方法将所有 $_POST 数据保存到数据库中。例如,送货地址等等
现在我遇到了困难。
让我们假设客户选择PayPal付款方式。正如我之前所说,他提交了表单 (order.php),但现在运行completeOrder() 方法,它重定向到 PayPal,他可以在其中取消付款并确认。一旦他确认,就会重定向到 order.php。
现在我想要的是检查状态是否已确认,并仅在付款时运行completeOrder()!
现在的问题是,当客户被重定向到 PayPal 页面时,他会丢失所有 $_POST 值,并且无法再运行completeOrder()。
Warning: Undefined array key "payment" in /var/www/html/src/Services/CheckoutService.php on line 77
Warning: Undefined array key "cartid" in /var/www/html/src/Services/CheckoutService.php on line 104
Warning: Undefined array key "billingaddress" in /var/www/html/src/Services/CheckoutService.php on line 107
Warning: Undefined array key "deliveryaddress" in /var/www/html/src/Services/CheckoutService.php on line 108
在 Paypal API 内部,我添加了返回 url(付款后)order.php 和取消 url cart.php
Order.php(结帐控制器)
$success = $this->checkoutService->completeOrder();
if (isset($_SESSION['orderId'], $_GET['token'])) {
$orderId = $_SESSION['orderId'];
$accessToken = $this->paypal->getAccessToken();
$token = $_GET['token'];
$orderConfirm = $this->paypal->capturePayment($accessToken,$orderId,$token);
if($orderConfirm['id'] === $token && $orderConfirm['status']==='COMPLETED'){
$this->checkoutService->finishOrder();
header('Location: success');
}else {
header('Location: error');
}
}
完成(结帐服务.php)
public function completeOrder(): bool
{
//Security reason: Line 83
$cart = $this->cartService->getCart();
//Payment method (Cash or PayPal)
$payment = $_POST['payment'];
//Extra security for checkout: getCart()
if ($cart) {
$totalPrice = $cart->getTotalprice();
if ($payment === 'paypal') {
$accessToken = $this->paypal->getAccessToken();
$this->paypal->setOrderPrice($totalPrice);
$this->paypal->cretePaypalOrder($accessToken);
} else {
$this->finishOrder();
return true;
}
}
return false;
}
完成订单(结帐服务.php)->插入数据库
public function finishOrder(): void
{
//Current Date
$date = date('Y-m-j');
//Customer, Cart ID
$customerId = $_SESSION['customerId'];
$cartId = $_POST['cartid'];
//Billing, Delivery Address
$billingAdress = $_POST['billingaddress'];
$deliveryAddress = $_POST['deliveryaddress'];
//Finish the order -> Update Cart
$this->checkoutRepository->orderProducts($date, $customerId);
//Insert Billing, and delivery Address
$this->addressRepository->insertOrderedBillingAddress($cartId, $billingAdress);
if ($deliveryAddress !== $billingAdress) {
$this->addressRepository->insertOrderedDeliveryAddress($cartId, $deliveryAddress);
} else {
//Insert delivery address same to billing
$this->addressRepository->insertSameAsBillingAddress($cartId, $billingAdress, $_POST['sameasbilling']);
}
}
总结: 我想确保付款已完成。如果是,则保存到数据库。如果不是,则抛出错误。 感谢您的帮助
I got E-Commerce shop, and currently I am developing the Paypal Rest API.
The whole logic goes like this:
- Customer want to check out his products (checkout.php: Bunch of $_POST variables)
- He can choose his delivery and billing address, and payment option
- There are currently 2 Payment options: PayPal and Cash
- After he selects his payment option (let’s assume he selected cash), he is submitting his form (order.php)
- Order.php runs completeOrder() method, which saves all the $_POST data to the database. Such as, delivery address and so on
Now comes the thing where I am stuck.
Let’s assume that Customer selects PayPal payment method. Like I stated before, he submits his form (order.php), but now instead running completeOrder() method, it redirect to PayPal, where he can cancel his Payment, and confirm. As soon as he confirms, is going to redirect to order.php.
Now what I want here, is to check if the Status is confirmed, and run completeOrder() only when its payed!
Now the problem is, when customer gets redirected to the PayPal page, he looses all $_POST values, and it can’t run completeOrder() anymore.
Warning: Undefined array key "payment" in /var/www/html/src/Services/CheckoutService.php on line 77
Warning: Undefined array key "cartid" in /var/www/html/src/Services/CheckoutService.php on line 104
Warning: Undefined array key "billingaddress" in /var/www/html/src/Services/CheckoutService.php on line 107
Warning: Undefined array key "deliveryaddress" in /var/www/html/src/Services/CheckoutService.php on line 108
Inside Paypal API I added as return url (after payment) order.php, and as cancel url cart.php
Order.php (Checkout Controller)
$success = $this->checkoutService->completeOrder();
if (isset($_SESSION['orderId'], $_GET['token'])) {
$orderId = $_SESSION['orderId'];
$accessToken = $this->paypal->getAccessToken();
$token = $_GET['token'];
$orderConfirm = $this->paypal->capturePayment($accessToken,$orderId,$token);
if($orderConfirm['id'] === $token && $orderConfirm['status']==='COMPLETED'){
$this->checkoutService->finishOrder();
header('Location: success');
}else {
header('Location: error');
}
}
Complete (Checkout Service.php)
public function completeOrder(): bool
{
//Security reason: Line 83
$cart = $this->cartService->getCart();
//Payment method (Cash or PayPal)
$payment = $_POST['payment'];
//Extra security for checkout: getCart()
if ($cart) {
$totalPrice = $cart->getTotalprice();
if ($payment === 'paypal') {
$accessToken = $this->paypal->getAccessToken();
$this->paypal->setOrderPrice($totalPrice);
$this->paypal->cretePaypalOrder($accessToken);
} else {
$this->finishOrder();
return true;
}
}
return false;
}
Finish Order (Checkout Service.php) -> Insert to Database
public function finishOrder(): void
{
//Current Date
$date = date('Y-m-j');
//Customer, Cart ID
$customerId = $_SESSION['customerId'];
$cartId = $_POST['cartid'];
//Billing, Delivery Address
$billingAdress = $_POST['billingaddress'];
$deliveryAddress = $_POST['deliveryaddress'];
//Finish the order -> Update Cart
$this->checkoutRepository->orderProducts($date, $customerId);
//Insert Billing, and delivery Address
$this->addressRepository->insertOrderedBillingAddress($cartId, $billingAdress);
if ($deliveryAddress !== $billingAdress) {
$this->addressRepository->insertOrderedDeliveryAddress($cartId, $deliveryAddress);
} else {
//Insert delivery address same to billing
$this->addressRepository->insertSameAsBillingAddress($cartId, $billingAdress, $_POST['sameasbilling']);
}
}
TO SUM UP:
I want to make sure that the payment was completed. And if yes, than save to databse. If no, throw error.
Thanks for help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用任何重定向。这是一种旧的集成方法,适用于旧网站。
相反,请遵循 PayPal Checkout 集成 指南并在您的服务器上创建 2 条路由,一项用于“创建订单”,一项用于“捕获订单”(请参阅“添加和修改代码”中的可选步骤 5)。您可以使用 Checkout-PHP-SDK 进行这些 API 调用(不是已弃用的 PayPal) -PHP-SDK)。两条路由都应仅返回/输出 JSON 数据(无 HTML 或文本)。在第二条路线中,当捕获 API 成功时,您可以将其生成的付款详细信息存储在数据库中(特别是
purchase_units[0]. payments.captures[0].id
,这是 PayPal 交易 ID )并在将 JSON 响应转发给前端调用者之前立即执行任何必要的业务逻辑(例如发送确认电子邮件或预订产品)。将这 2 条路由与此前端审批流程配对:https://developer.paypal。 com/demo/checkout/#/pattern/server
Don't use any redirects. That is an old integration method, for old websites.
Instead follow the PayPal Checkout integration guide and make 2 routes on your server, one for 'Create Order' and one for 'Capture Order' (see the optional step 5 in 'Add and modify the code'). You can use the Checkout-PHP-SDK for these API calls (not the deprecated PayPal-PHP-SDK). Both of the 2 routes should return/output only JSON data (no HTML or text). Inside the 2nd route, when the capture API is successful you can store its resulting payment details in your database (particularly
purchase_units[0].payments.captures[0].id
, which is the PayPal transaction ID) and perform any necessary business logic (such as sending confirmation emails or reserving product) immediately before forwarding the JSON response to the frontend caller.Pair those 2 routes with this frontend approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server