pimcore / payment-provider-paypal-smart-payment-button
Pimcore 支付提供者 - Paypal 智能支付按钮
v2.0.1
2024-06-07 12:26 UTC
Requires
- pimcore/ecommerce-framework-bundle: ^1.0.0
- pimcore/pimcore: ^11.0.0
Requires (Dev)
- phpstan/phpstan: ^1.9
README
安装
使用 Composer 安装最新版本
composer require pimcore/payment-provider-paypal-smart-payment-button
通过控制台或 Pimcore 后端扩展管理器启用捆绑包
php bin/console pimcore:bundle:enable PimcorePaymentProviderPayPalSmartPaymentButtonBundle php bin/console pimcore:bundle:install PimcorePaymentProviderPayPalSmartPaymentButtonBundle
有关配置详细信息,请参阅以下内容。有关 PayPal API 凭据的更多信息,请参阅API 文档
配置
支付管理器负责实现不同的支付提供者,以便将它们集成到框架中。
有关支付管理器的更多信息,请参阅支付管理器文档。
在 pimcore_ecommerce_config.payment_manager
配置部分配置支付提供者
pimcore_ecommerce_framework: payment_manager: payment_manager_id: Pimcore\Bundle\EcommerceFrameworkBundle\PaymentManager\PaymentManager providers: paypal: provider_id: Pimcore\Bundle\EcommerceFrameworkBundle\PaymentManager\Payment\PayPalSmartPaymentButton profile: sandbox profiles: sandbox: client_id: <YOUR PAYPAL REST API CLIENT ID> client_secret: <YOUR PAYPAL REST API CLIENT SECRET> # defines, if payment caputure should take place automatic or manual, default is automatic capture_strategy: automatic # defines mode of PayPal API, default value is sandbox mode: sandbox # defines PayPal application context for shipping, default value is NO_SHIPPING # see https://developer.paypal.com/docs/api/orders/v2/#definition-application_context shipping_preference: NO_SHIPPING # defines PayPal application context for user action, default value is PAY_NOW # see https://developer.paypal.com/docs/api/orders/v2/#definition-application_context user_action: PAY_NOW live: client_id: <YOUR PAYPAL REST API CLIENT ID> client_secret: <YOUR PAYPAL REST API CLIENT SECRET> mode: live
实现
将 PayPal 支付按钮集成到视图模板中
将 PayPal 支付按钮集成并覆盖一些方法,如示例所示。至少需要覆盖 createOrder
和 onApprove
。
<?php /** @var \Pimcore\Bundle\EcommerceFrameworkBundle\PaymentManager\Payment\PayPalSmartPaymentButton $payment */ ?> <script src="<?= $payment->buildPaymentSDKLink() ?>"> </script> <div id="paypal-button-container"></div> <script> paypal.Buttons({ onCancel: function (data) { // e.g. redirect to a certain page or show message window.location.replace('...'); }, createOrder: function() { return fetch('/path/to/your/startPaymentAction', { method: 'post', headers: { 'content-type': 'application/json' } }).then(function(res) { return res.json(); }).then(function(data) { return data.id; }); }, onApprove: function(data) { // make sure you deliver orderID, payerID and paymentID to your // handle response controller action, e.g. by creating a form and // posting the data var form = document.createElement('form'); document.body.appendChild(form); form.method = 'POST'; form.action = '/path/to/your/handleResponseAction'; var orderID = document.createElement('input'); orderID.type = 'hidden'; orderID.name = 'orderID'; orderID.value = data['orderID']; form.appendChild(orderID); var payerID = document.createElement('input'); payerID.type = 'hidden'; payerID.name = 'payerID'; payerID.value = data['payerID']; form.appendChild(payerID); var paymentID = document.createElement('input'); paymentID.type = 'hidden'; paymentID.name = 'paymentID'; paymentID.value = data['paymentID']; form.appendChild(paymentID); form.submit(); } }).render('#paypal-button-container'); </script>
- 在控制器中创建 startPaymentAction
初始化结账管理器,调用支付实现的 startOrderPaymentWithPaymentProvider
。它在 PayPal 上创建一个订单,其响应是默认的 PayPal 响应,需要将其作为操作的 JSON 响应返回。
//in your payment controller, e.g. startPaymentAction public function startPaymentAction() { // ... some other stuff $paymentConfig = new AbstractRequest($config); $response = $checkoutManager->startOrderPaymentWithPaymentProvider($paymentConfig); $checkoutManager = Factory::getInstance()->getCheckoutManager($cart); $paymentInformation = $checkoutManager->initOrderPayment(); $payment = $checkoutManager->getPayment(); $config = [ 'return_url' => $returnUrl, 'cancel_url' => $cancelUrl . 'payment?error=cancel', 'OrderDescription' => 'My Order ' . $order->getOrdernumber() . ' at pimcore.org', 'InternalPaymentId' => $paymentInformation->getInternalPaymentId() ]; $paymentConfig = new AbstractRequest($config); $response = $checkoutManager->startOrderPaymentWithPaymentProvider($paymentConfig); return new \Symfony\Component\HttpFoundation\JsonResponse($response->getJsonString(), 200, [], true); }
- 处理 PayPal 响应
在处理响应时,只需调用结账管理器的 handlePaymentResponseAndCommitOrderPayment
。它完成剩余的操作——检查 PayPal 是否已授权付款,并提交订单。
根据您的设置(见下文),付款也将自动扣款。如果没有,您需要通过调用 $payment->executeDebit()
手动扣款。
public function handleResponseAction() { // ... do some stuff $checkoutManager = Factory::getInstance()->getCheckoutManager($cart); $params = array_merge($request->query->all(), $request->request->all()); $order = $checkoutManager->handlePaymentResponseAndCommitOrderPayment($params); // optional to clear payment // if this call is necessary depends on payment provider and configuration. // its possible to execute this later (e.g. when shipment is done) // $payment = $checkoutManager->getPayment(); // $paymentStatus = $payment->executeDebit(); // $orderAgent = Factory::getInstance()->getOrderManager()->createOrderAgent($order); // $orderAgent->updatePayment($paymentStatus); // ... check order state and redirect user to error page or order success page }
更多信息
有关更多信息或更多自定义集成,请参阅以下资源