2016-07-11 23 views
4

私はmagentoサイト用のハイブリッドモバイルアプリケーションを開発しています。今私はプログラム的に注文を作成する必要があります。私はpaypalを統合し、paypalの成功の反応を得ることができます。一度私はマンスにデータを掲示しているペイパルの応答を得た。ここにエラーが表示されますプログラムでmagento注文作成でのPaypalの投稿エラー

Uncaught exception 'Mage_Core_Exception' with message 'PayPal gateway has rejected request. 
Invalid token (#10410: Invalid token).' in public_html/app/code/core/Mage/Paypal/Model/Api/Nvp.php:1062 

以下は私の注文作成コードです。私は続い

<?php 

// Mage Path 
require_once(dirname(__FILE__).'/../app/Mage.php'); 
umask(0); 
// Initialize Magento ... 
Mage::app("default"); 

$id=11; // get Customer Id 
$customer = Mage::getModel('customer/customer')->load($id); 

$transaction = Mage::getModel('core/resource_transaction'); 
$storeId = $customer->getStoreId(); 
$reservedOrderId = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId($storeId); 

$order = Mage::getModel('sales/order') 
->setIncrementId($reservedOrderId) 
->setStoreId($storeId) 
->setQuoteId(0) 
->setGlobal_currency_code('USD') 
->setBase_currency_code('USD') 
->setStore_currency_code('USD') 
->setOrder_currency_code('USD'); 
//Set your store currency USD or any other 

// set Customer data 
$order->setCustomer_email($customer->getEmail()) 
->setCustomerFirstname($customer->getFirstname()) 
->setCustomerLastname($customer->getLastname()) 
->setCustomerGroupId($customer->getGroupId()) 
->setCustomer_is_guest(0) 
->setCustomer($customer); 

// set Billing Address 
$billing = $customer->getDefaultBillingAddress(); 
$billingAddress = Mage::getModel('sales/order_address') 
->setStoreId($storeId) 
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING) 
->setCustomerId($customer->getId()) 
->setCustomerAddressId($customer->getDefaultBilling()) 
->setCustomer_address_id($billing->getEntityId()) 
->setPrefix($billing->getPrefix()) 
->setFirstname($billing->getFirstname()) 
->setMiddlename($billing->getMiddlename()) 
->setLastname($billing->getLastname()) 
->setSuffix($billing->getSuffix()) 
->setCompany($billing->getCompany()) 
->setStreet($billing->getStreet()) 
->setCity($billing->getCity()) 
->setCountry_id($billing->getCountryId()) 
->setRegion($billing->getRegion()) 
->setRegion_id($billing->getRegionId()) 
->setPostcode($billing->getPostcode()) 
->setTelephone($billing->getTelephone()) 
->setFax($billing->getFax()); 
$order->setBillingAddress($billingAddress); 

$shipping = $customer->getDefaultShippingAddress(); 
$shippingAddress = Mage::getModel('sales/order_address') 
->setStoreId($storeId) 
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING) 
->setCustomerId($customer->getId()) 
->setCustomerAddressId($customer->getDefaultShipping()) 
->setCustomer_address_id($shipping->getEntityId()) 
->setPrefix($shipping->getPrefix()) 
->setFirstname($shipping->getFirstname()) 
->setMiddlename($shipping->getMiddlename()) 
->setLastname($shipping->getLastname()) 
->setSuffix($shipping->getSuffix()) 
->setCompany($shipping->getCompany()) 
->setStreet($shipping->getStreet()) 
->setCity($shipping->getCity()) 
->setCountry_id($shipping->getCountryId()) 
->setRegion($shipping->getRegion()) 
->setRegion_id($shipping->getRegionId()) 
->setPostcode($shipping->getPostcode()) 
->setTelephone($shipping->getTelephone()) 
->setFax($shipping->getFax()); 

$order->setShippingAddress($shippingAddress) 
->setShipping_method('flatrate_flatrate'); 
/*->setShippingDescription($this->getCarrierName('flatrate'));*/ 
/*some error i am getting here need to solve further*/ 

//you can set your payment method name here as per your need 
$orderPayment = Mage::getModel('sales/order_payment') 
->setStoreId($storeId) 
->setCustomerPaymentId("PAY-9G20599927263253EK6BC2SA") 
->setMethod('paypal_express') 
->setPo_number('8EV04718DA563240K'); 
$order->setPayment($orderPayment); 

// let say, we have 2 products 
//check that your products exists 
//need to add code for configurable products if any 
$subTotal = 0; 
$products = array(
    '1' => array(
    'qty' => 2 
    )); 

foreach ($products as $productId=>$product) { 
//$_product = Mage::getModel('catalog/product')->load($productId); 
$id = Mage::getModel('catalog/product')->getResource()->getIdBySku('property_415682'); 

if ($id) { 
    $_product = Mage::getModel('catalog/product')->load($id); 
} 

$rowTotal = $_product->getPrice() * $product['qty']; 
$orderItem = Mage::getModel('sales/order_item') 
->setStoreId($storeId) 
->setQuoteItemId(0) 
->setQuoteParentItemId(NULL) 
->setProductId($productId) 
->setProductType($_product->getTypeId()) 
->setQtyBackordered(NULL) 
->setTotalQtyOrdered($product['rqty']) 
->setQtyOrdered($product['qty']) 
->setName($_product->getName()) 
->setSku($_product->getSku()) 
->setPrice($_product->getPrice()) 
->setBasePrice($_product->getPrice()) 
->setOriginalPrice($_product->getPrice()) 
->setRowTotal($rowTotal) 
->setBaseRowTotal($rowTotal); 

$subTotal += $rowTotal; 
$order->addItem($orderItem); 
} 

$order->setSubtotal($subTotal) 
->setBaseSubtotal($subTotal) 
->setGrandTotal($subTotal) 
->setBaseGrandTotal($subTotal); 

$transaction->addObject($order); 
$transaction->addCommitCallback(array($order, 'place')); 
$transaction->addCommitCallback(array($order, 'save')); 
$transaction->save(); 

リファレンス私はMagentoのとイオンアプリの開発に新たなんだ

https://github.com/paypal/PayPal-Cordova-Plugin 

はとても助けの任意の種類が高く評価され、このプラグインを使用するPayPalの支払いを統合するための

http://pragneshkaria.com/programmatically-create-order-in-magento/ 

です。

ありがとうございます。

+0

しかし、このコードで実際のPayPalアカウントを請求すると思いますか?各モデル保存のMagentoは、実際のP​​ayPalゲートウェイに接続する可能性のあるオブザーバを実行する可能性があり、もちろんPayPalには何らかの種類の認証トークンが必要です。古いデータをインポートするだけの場合は、より一般的なクエリにアプローチする必要があります。 –

+0

@PawelDubiel私はプレーンなクエリで試しましたが、私はそれも困難に直面しています。DBクエリを使用して注文を作成するためのサンプルまたは参照を共有してください。 – venkatesh

答えて

0

輸入オフこの1もし、あなたが充電顧客を望んでいない、とあなたはハックソリューションを使用する準備ができている、あなたは

app/code/core/Mage/Paypal/Model/Express.php

_placeOrder方法を変更しようとすることができますとコメントを試してください。

$api->callDoExpressCheckoutPayment(); 

この機能はtですプロセス全体を激励する。


/** 
* Place an order with authorization or capture action 
* 
* @param Mage_Sales_Model_Order_Payment $payment 
* @param float $amount 
* @return Mage_Paypal_Model_Express 
*/ 
protected function _placeOrder(Mage_Sales_Model_Order_Payment $payment, $amount) 
{ 
    $order = $payment->getOrder(); 

    // prepare api call 
    $token = $payment->getAdditionalInformation(Mage_Paypal_Model_Express_Checkout::PAYMENT_INFO_TRANSPORT_TOKEN); 
    $api = $this->_pro->getApi() 
     ->setToken($token) 
     ->setPayerId($payment-> 
      getAdditionalInformation(Mage_Paypal_Model_Express_Checkout::PAYMENT_INFO_TRANSPORT_PAYER_ID)) 
     ->setAmount($amount) 
     ->setPaymentAction($this->_pro->getConfig()->paymentAction) 
     ->setNotifyUrl(Mage::getUrl('paypal/ipn/')) 
     ->setInvNum($order->getIncrementId()) 
     ->setCurrencyCode($order->getBaseCurrencyCode()) 
     ->setPaypalCart(Mage::getModel('paypal/cart', array($order))) 
     ->setIsLineItemsEnabled($this->_pro->getConfig()->lineItemsEnabled); 

    // COMMENT LINE BELOW 
    $api->callDoExpressCheckoutPayment(); 

    $this->_importToPayment($api, $payment); 
    return $this; 
} 

あなたが日常的にそれを行う必要がある場合は、PHPのための「猿のパッチ適用」を調べて、ファイルを変更せずにその場でこのメソッドをオーバーライドすることができます。

また、おそらく、この拡張機能を拡張したり、ローカルMagentoのディレクトリにそれを置くことによって、ちょうどapp/code/core/Mage/Paypal/Model/Express.phpを上書きして、別の解決策は、カスタムを作成することができ


(どのようhttps://magento2.atlassian.net/wiki/pages/viewpage.action?pageId=14024897を上書きする)_placeOrderに余分なロジックを追加することができますPaypal_expressの代わりに使用するカスタム支払いコードを使用してMagento(http://inchoo.net/magento/how-to-create-magento-payment-module/)の支払い方法を選択します。その後、データベース内のフィールドをカスタムメソッド名からpaypal_expressに置き換えることができます。

また、「my_paypal_express」のようなカスタム名で新しい支払い方法を作成し、paypal_expressの代わりに使用することもできます。

上記のソリューションには満足していませんが、少し参考になるかもしれません。

関連する問題