Jump to content

Paypal module remove details


sanctusmob

Recommended Posts

Καλησπέρα στην ομάδα.

Θέλω να αφαιρέσω από το paypal το order details γιατί στην ανάλυση της πληρωμής αλλά και στο email που στέλνει το paypal έχει μέσα αυτές τις πληροφορίες.

π.χ.

image.png.1e13cf38cc87a8051d05f3fcd16f6b03.png

σε αγορές που κάνω από άλλα καταστήματα εμφανίζει μόνο το σύνολο

image.png.55de0b996e509b036ee10771cdbf679d.png

πήγα στο official module στο αρχείο classes/API/Request/V_1/PaypalOrderCreateRequest.php

και αφαίρεσα τα

104a105 >             ->setItemList($this->_itemList)

340a352 >             ->setDetails($details);

οπότε στέλνει μόνο το total.

αυτό είχε σαν αποτέλεσμα να αλλάξει το email σε αυτή την μορφή

image.png.73106f2d7f8f5884bb33dd396120c96c.png

Αλλά συνεχίζει να έχει το shipping και tax τα οποία δεν θέλω να εμφανίζονται και δεν μπορώ να βρω με τίποτα από που προστίθενται.

Υπάρχει κάνει με γνώση πάνω σε αυτό?

Παραθέτω το PaypalOrderCreateRequest.php με τις αλλαγές.

<?php
/**
 * 2007-2023 PayPal
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to [email protected] so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 *  versions in the future. If you wish to customize PrestaShop for your
 *  needs please refer to http://www.prestashop.com for more information.
 *
 *  @author 2007-2023 PayPal
 *  @author 202 ecommerce <[email protected]>
 *  @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 *  @copyright PayPal
 */

namespace PaypalAddons\classes\API\Request\V_1;

use Address;
use Cart;
use Context;
use Country;
use Exception;
use Module;
use PayPal;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\PayerInfo;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\ShippingAddress;
use PayPal\Api\Transaction;
use PaypalAddons\classes\API\Response\Error;
use PaypalAddons\classes\API\Response\ResponseOrderCreate;
use State;
use Throwable;
use Tools;

class PaypalOrderCreateRequest extends RequestAbstractMB
{
    /** @var Amount */
    protected $_amount;

    /** @var ItemList */
    protected $_itemList;

    /** @var Item[] */
    protected $_items;

    /** @var float */
    protected $_itemTotalValue;

    /** @var float */
    protected $_taxTotalValue;

    /**
     * @return ResponseOrderCreate
     */
    public function execute()
    {
        $response = new ResponseOrderCreate();

        if ($this->method->isConfigured() == false) {
            $error = new Error();
            $error->setMessage('Module is not configured');

            return $response->setSuccess(false)->setError($error);
        }

        $payer = new Payer();
        $payer->setPaymentMethod('paypal');
        $payer->setPayerInfo($this->getPayerInfo());
        // ### Itemized information
        // (Optional) Lets you specify item wise information

        $this->_itemList = new ItemList();
        $this->_amount = new Amount();

        $this->_getPaymentDetails();

        if (Context::getContext()->cart->isVirtualCart() === false) {
            $this->_itemList->setShippingAddress($this->getPayerShippingAddress());
        }

        // ### Transaction
        // A transaction defines the contract of a
        // payment - what is the payment for and who
        // is fulfilling it.

        $transaction = new Transaction();
        $transaction->setAmount($this->_amount)
            ->setDescription('Payment description')
            ->setInvoiceNumber(uniqid());

        if (is_callable([get_class($this->method), 'getIpnUrl'], true)) {
            $transaction->setNotifyUrl($this->method->getIpnUrl());
        }

        // ### Redirect urls
        // Set the urls that the buyer must be redirected to after
        // payment approval/ cancellation.

        $redirectUrls = new RedirectUrls();
        $redirectUrls
            ->setReturnUrl($this->method->getReturnUrl())
            ->setCancelUrl($this->method->getCancelUrl());

        // ### Payment
        // A Payment Resource; create one using
        // the above types and intent set to 'sale'

        $payment = new Payment();
        $payment->setIntent('sale')
            ->setPayer($payer)
            ->setRedirectUrls($redirectUrls)
            ->setTransactions([$transaction]);

        // Set application_context
        $payment->application_context = $this->getApplicationContext();

        // ### Create Payment
        // Create a payment by calling the 'create' method
        // passing it a valid apiContext.
        // The return object contains the state and the
        // url to which the buyer must be redirected to
        // for payment approval

        try {
            $payment->create($this->getApiContext());
        } catch (Throwable $e) {
            $error = new Error();
            $error
                ->setErrorCode($e->getCode())
                ->setMessage($e->getMessage());

            return $response->setError($error)->setSuccess(false);
        } catch (Exception $e) {
            $error = new Error();
            $error
                ->setErrorCode($e->getCode())
                ->setMessage($e->getMessage());

            return $response->setError($error)->setSuccess(false);
        }

        return $response
            ->setApproveLink($payment->getApprovalLink())
            ->setPaymentId($payment->getId())
            ->setSuccess(true);
    }

    protected function getApplicationContext()
    {
        if (Context::getContext()->cart->isVirtualCart()) {
            $applicationContext = [
                'shipping_preference' => 'NO_SHIPPING',
            ];
        } else {
            $applicationContext = [
                'shipping_preference' => 'SET_PROVIDED_ADDRESS',
            ];
        }

        return $applicationContext;
    }

    /**
     * @return PayerInfo
     */
    protected function getPayerInfo()
    {
        $customer = Context::getContext()->customer;
        $addressCustomer = new Address(Context::getContext()->cart->id_address_delivery);
        $countryCustomer = new Country($addressCustomer->id_country);
        $payerInfo = new PayerInfo();
        $payerInfo->setEmail($customer->email);
        $payerInfo->setFirstName($this->formatter->formatPaypalString($customer->firstname));
        $payerInfo->setLastName($this->formatter->formatPaypalString($customer->lastname));

        if ($countryCustomer->iso_code == 'BR') {
            $payerTaxId = str_replace(['.', '-', '/'], '', $addressCustomer->vat_number);
            $payerInfo->setTaxId($payerTaxId);
            $payerInfo->setTaxIdType($this->method->getTaxIdType($payerTaxId));
        }

        return $payerInfo;
    }

    /**
     * @return ShippingAddress
     */
    protected function getPayerShippingAddress()
    {
        $addressCustomer = new Address(Context::getContext()->cart->id_address_delivery);
        $payerShippingAddress = new ShippingAddress();
        $payerShippingAddress->setCountryCode(Tools::strtoupper(Country::getIsoById($addressCustomer->id_country)));
        $payerShippingAddress->setCity($this->formatter->formatPaypalString($addressCustomer->city));
        $payerShippingAddress->setLine1($this->formatter->formatPaypalString($addressCustomer->address1));
        $payerShippingAddress->setPostalCode($addressCustomer->postcode);
        $payerShippingAddress->setRecipientName($this->formatter->formatPaypalString(implode(' ', [$addressCustomer->firstname, $addressCustomer->lastname])));

        if ((int) $addressCustomer->id_state) {
            $state = new State($addressCustomer->id_state);
            $payerShippingAddress->setState($this->formatter->formatPaypalString(Tools::strtoupper($state->iso_code)));
        }

        return $payerShippingAddress;
    }

    protected function _getPaymentDetails()
    {
        $paypal = Module::getInstanceByName($this->method->name);
        $currency = $paypal->getPaymentCurrencyIso();
        $this->_getProductsList($currency);
        $this->_getDiscountsList($currency);
        $this->_getGiftWrapping($currency);
        $this->_getHandling($currency);
        $this->_getPaymentValues($currency);
    }

    protected function _getProductsList($currency)
    {
        $products = Context::getContext()->cart->getProducts();
        foreach ($products as $product) {
            $item = new Item();
            $item->setName($this->formatter->formatPaypalString($product['name']))
                ->setCurrency($currency)
                ->setDescription(isset($product['attributes']) ? $product['attributes'] : '')
                ->setQuantity($product['quantity'])
                ->setSku($product['id_product']) // Similar to `item_number` in Classic API
                ->setPrice($this->method->formatPrice($product['price_wt']));

            $this->_items[] = $item;
            $this->_itemTotalValue += $this->method->formatPrice($product['price_wt']) * $product['quantity'];
        }
        $this->_taxTotalValue = 0;
    }

    protected function _getDiscountsList($currency)
    {
        $totalDiscounts = Context::getContext()->cart->getOrderTotal(true, Cart::ONLY_DISCOUNTS);

        if ($totalDiscounts > 0) {
            $module = Module::getInstanceByName($this->method->name);
            $discountItem = new Item();
            $discountItem->setName($module->l('Total discounts', get_class($this)))
                ->setCurrency($currency)
                ->setQuantity(1)
                ->setSku('discounts')
                ->setPrice(-1 * $totalDiscounts);

            $this->_items[] = $discountItem;
            $this->_itemTotalValue += (-1 * $totalDiscounts);
        }
    }

    protected function _getHandling($currency)
    {
        $discounts = Context::getContext()->cart->getCartRules();

        if (empty($discounts)) {
            return;
        }

        $module = Module::getInstanceByName($this->method->name);

        foreach ($discounts as $discount) {
            if ($discount['value_real'] < 0) {
                $handlingValue = abs($this->method->formatPrice($discount['value_real']));
                $handlingItem = new Item();
                $handlingItem->setName($module->l('Handling', get_class($this)))
                    ->setCurrency($currency)
                    ->setQuantity(1)
                    ->setSku('handling')
                    ->setPrice($handlingValue);

                $this->_items[] = $handlingItem;
                $this->_itemTotalValue += $handlingValue;
            }
        }
    }

    protected function _getGiftWrapping($currency)
    {
        $wrapping_price = Context::getContext()->cart->gift ? Context::getContext()->cart->getGiftWrappingPrice() : 0;
        if ($wrapping_price > 0) {
            $wrapping_price = $this->method->formatPrice($wrapping_price);
            $item = new Item();
            $item->setName('Gift wrapping')
                ->setCurrency($currency)
                ->setQuantity(1)
                ->setSku('wrapping') // Similar to `item_number` in Classic API
                ->setPrice($wrapping_price);
            $this->_items[] = $item;
            $this->_itemTotalValue += $wrapping_price;
        }
    }

    private function _getPaymentValues($currency)
    {
        $this->_itemList->setItems($this->_items);
        $context = Context::getContext();
        $cart = $context->cart;
        $shipping_cost_wt = $cart->getTotalShippingCost();
        $shipping = $this->method->formatPrice($shipping_cost_wt);
        $total = $this->method->formatPrice($cart->getOrderTotal(true, Cart::BOTH));
        $summary = $cart->getSummaryDetails();
        $subtotal = $this->method->formatPrice($summary['total_products']);
        // total shipping amount
        $shippingTotal = $shipping;

        if ($subtotal != $this->_itemTotalValue) {
            $subtotal = $this->_itemTotalValue;
        }
        //total
        $total_cart = $shippingTotal + $this->_itemTotalValue;

        if ($total != $total_cart) {
            $total = $total_cart;
        }

        // ### Amount
        // Lets you specify a payment amount.
        // You can also specify additional details
        // such as shipping, tax.
        $this->_amount->setCurrency($currency)
            ->setTotal($total)
    }
}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...