pimcore / payment-provider-paypal-smart-payment-button
Pimcore Payment Provider - Paypal Smart Payment Button
Installs: 65 926
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 11
Forks: 2
Open Issues: 0
Type:pimcore-bundle
Requires
- pimcore/ecommerce-framework-bundle: ^1.0.0
- pimcore/pimcore: ^11.0.0
Requires (Dev)
- phpstan/phpstan: ^1.9
README
Installation
Install latest version with Composer:
composer require pimcore/payment-provider-paypal-smart-payment-button
Enable bundle via console or extensions manager in Pimcore backend:
php bin/console pimcore:bundle:enable PimcorePaymentProviderPayPalSmartPaymentButtonBundle php bin/console pimcore:bundle:install PimcorePaymentProviderPayPalSmartPaymentButtonBundle
For configuration details see further below. For additional information of PayPal API credentials see API Docs
Configuration
The Payment Manager is responsible for implementation of different Payment Provider to integrate them into the framework.
For more information about Payment Manager, see Payment Manager Docs.
Configure payment provider in the pimcore_ecommerce_config.payment_manager
config section:
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
Implementation
Integrate the PayPal payment button to your view template
Integrate PayPal payment button and overwrite a few methods like in the sample. At least
createOrder
and onApprove
need to be overwritten.
<?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>
- Create a startPaymentAction in your controller
Initialize checkout manager, call startOrderPaymentWithPaymentProvider
of the payment
implementation. It is creating an order at PayPal and its response is the default PayPal
response, which need to be returned as a json response of the action.
//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); }
- Handle Response of PayPal
In handle response just call handlePaymentResponseAndCommitOrderPayment
of checkout manager.
It does the rest - which is checking at PayPal if payment was authorized by the payer and
committing the order.
Depending on your settings (see below), the payment is also automatically captured. If not
you need to capture the payment manually by calling $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 }
Further Information
For further information and/or more custom integrations, also have a look at following resources: