定制 pimcore/payment-provider-paypal-smart-payment-button 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pimcore/payment-provider-paypal-smart-payment-button

Composer 安装命令:

composer require pimcore/payment-provider-paypal-smart-payment-button

包简介

Pimcore Payment Provider - Paypal Smart Payment Button

README 文档

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>
  1. 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);

} 
  1. 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:

pimcore/payment-provider-paypal-smart-payment-button 适用场景与选型建议

pimcore/payment-provider-paypal-smart-payment-button 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 108.8k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 02 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 pimcore/payment-provider-paypal-smart-payment-button 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pimcore/payment-provider-paypal-smart-payment-button 我们能提供哪些服务?
定制开发 / 二次开发

基于 pimcore/payment-provider-paypal-smart-payment-button 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 108.8k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 17
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 6
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2021-02-02