paypal - Magento2 - 使用 Paypal 计费协议(protocol)创建自定义订单 ("mandatory params missing referenceId error")

标签 paypal paypal-subscriptions magento2.1

我正在创建一个自定义模块,以使用 paypal_billing_agreement 作为付款方式以编程方式创建订单。下面是我使用的订单创建代码。

<?php

namespace Vendor\Module\Model\Subscription\Order;

class Create
{
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Quote\Model\QuoteManagement $quoteManagement,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
        \Magento\Sales\Model\Service\OrderService $orderService,
        \Magento\Quote\Api\CartRepositoryInterface $cartRepositoryInterface,
        \Magento\Quote\Api\CartManagementInterface $cartManagementInterface,
        \Magento\Quote\Model\Quote\Address\Rate $shippingRate
    ) {
        $this->_storeManager = $storeManager;
        $this->_productFactory = $productFactory;
        $this->quoteManagement = $quoteManagement;
        $this->customerFactory = $customerFactory;
        $this->customerRepository = $customerRepository;
        $this->orderService = $orderService;
        $this->cartRepositoryInterface = $cartRepositoryInterface;
        $this->cartManagementInterface = $cartManagementInterface;
        $this->shippingRate = $shippingRate;
    }
    /**
     * Create Order On Your Store
     *
     * @param array $orderData
     * @return int $orderId
     *
     */
    public function createOrder($orderData) {

        //init the store id and website id @todo pass from array
        $store = $this->_storeManager->getStore();
        $websiteId = $this->_storeManager->getStore()->getWebsiteId();
        //init the customer
        $customer=$this->customerFactory->create();
        $customer->setWebsiteId($websiteId);
        $customer->loadByEmail($orderData['email']);// load customet by email address
        //check the customer
        if(!$customer->getEntityId()){
            //If not avilable then create this customer
            $customer->setWebsiteId($websiteId)
                ->setStore($store)
                ->setFirstname($orderData['shipping_address']['firstname'])
                ->setLastname($orderData['shipping_address']['lastname'])
                ->setEmail($orderData['email'])
                ->setPassword($orderData['email']);
            $customer->save();
        }
        //init the quote
        $cart_id = $this->cartManagementInterface->createEmptyCart();
        $cart = $this->cartRepositoryInterface->get($cart_id);
        $cart->setStore($store);
        // if you have already buyer id then you can load customer directly
        $customer= $this->customerRepository->getById($customer->getEntityId());
        $cart->setCurrency();
        $cart->assignCustomer($customer); //Assign quote to customer
        //add items in quote
        foreach($orderData['items'] as $item){
            $product = $this->_productFactory->create()->load($item['product_id']);
            $cart->addProduct(
                $product,
                intval($item['qty'])
            );
        }
        //Set Address to quote 
        $cart->getBillingAddress()->addData($orderData['shipping_address']);
        $cart->getShippingAddress()->addData($orderData['shipping_address']);
        // Collect Rates and Set Shipping & Payment Method
        $this->shippingRate
            ->setCode('freeshipping_freeshipping')
            ->getPrice(1);
        $shippingAddress = $cart->getShippingAddress();
        //@todo set in order data
        $shippingAddress->setCollectShippingRates(true)
            ->collectShippingRates()
            ->setShippingMethod('flatrate_flatrate'); //shipping method
        $cart->getShippingAddress()->addShippingRate($this->shippingRate);
        $cart->setPaymentMethod('paypal_billing_agreement'); //payment method
        //@todo insert a variable to affect the invetory
        $cart->setInventoryProcessed(false);
        // Set sales order payment
        $cart->getPayment()->importData(['method' => 'payapal_billing_agreement,'reference_id' => 'B-RTRHFHs8428355236']);
        // Collect total and saeve
        $cart->collectTotals();
        // Submit the quote and create the order
        $cart->save();
        $cart = $this->cartRepositoryInterface->get($cart->getId());
        $order_id = $this->cartManagementInterface->placeOrder($cart->getId());
        return $order_id;
    }
}

当我将付款方式更改为“免费”时,它起作用了。 paypal 结算协议(protocol)付款方式需要额外的数据,因为我从结账时检查了实际的结算协议(protocol)流程。

{method: "paypal_billing_agreement", additional_data: {…}}
additional_data:{ba_agreement_id: "5"}
method:"paypal_billing_agreement"

我什至试图为 getpayment 方法添加相同的导入数据,但同样的问题仍然存在。 发送到 callDoReferenceTransaction() 的请求已正确设置所有必需参数,但 REFERENCEID 设置为 NULL。

注意:使用为 magento 2.1 提供的 PayPal 默认 NVP api。

抛出的异常是:

1 exception(s):
Exception #0 (Magento\Framework\Exception\LocalizedException): PayPal gateway has rejected request. ReferenceID : Mandatory parameter missing (#81253: Missing Parameter)

.

我错过了什么?

在此先感谢您的帮助。

最佳答案

所以我找到了解决方案,

先决条件,首先我必须从后端启用计费协议(protocol)订单创建。

为此,我对 Observer 进行了更改,默认情况下将 module-paypal 中的 "is_allowed"设置为 false 到 true

然后对代码进行以下更改以使用引用交易创建订单,

$cart->setPaymentMethod('paypal_billing_agreement'); //payment method
$cart->setInventoryProcessed(false);
// Set sales order payment

$cart->getPayment()->setAdditionalInformation("ba_agreement_id","1");//To point the correct billing agreement in billing agreement table.

$cart->getPayment()->importData(['method' => 'paypal_billing_agreement,'reference_id' => 'B-RTRHFHs8428355236']);
// Collect total and saeve
$cart->collectTotals();
// Submit the quote and create the order
$cart->save();

当然还有在 try catch 中包装来处理异常的事情

关于paypal - Magento2 - 使用 Paypal 计费协议(protocol)创建自定义订单 ("mandatory params missing referenceId error"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47940843/

相关文章:

ssl - Magento 2 - SSL 配置后的 502 Bad Gateway

php - 在 Magento 2 中使用观察者应用自定义促销规则

javascript - Braintree 上未知的 paymentMethodNonce

android - Paypal 整合 - 如何?

使用 Paypal 帐户的 Paypal Direct Payment API

c# - Paypal IPN 发送但网络服务器关闭

rest - 在没有网站的情况下与 PayPal 集成

ios - PayPal iOS SDK 2.0.5 未正确验证电子邮件 ID 和密码

javascript - 使用 Bootstrap 定价 slider 的每月 Paypal 付款

css - Magento 2 主页问题 | CSS 和布局不同于任何其他页面