Magento2 - 使用 Paypal 计费协议创建自定义订单(“强制参数缺少 referenceId 错误”)
Posted
技术标签:
【中文标题】Magento2 - 使用 Paypal 计费协议创建自定义订单(“强制参数缺少 referenceId 错误”)【英文标题】:Magento2 - Custom order creation with Paypal billing agreement ("mandatory params missing referenceId error") 【发布时间】:2018-06-05 01:38:27 【问题描述】:我正在创建一个自定义模块,以编程方式创建订单,并使用 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 账单协议付款方式需要额外的数据。
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)
.
我错过了什么?
提前感谢您的帮助。
【问题讨论】:
【参考方案1】:所以我找到了解决办法,
先决条件,首先我必须从后端启用计费协议订单创建。
为此,我在观察者中进行了更改,默认情况下在 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 中来处理异常
【讨论】:
以上是关于Magento2 - 使用 Paypal 计费协议创建自定义订单(“强制参数缺少 referenceId 错误”)的主要内容,如果未能解决你的问题,请参考以下文章
使用 paypal 节点 sdk 将自定义字段添加到计费协议