我以编程方式为magento创建一个代码。

时间:2015-12-09 10:54:09

标签: paypal-sandbox magento-1.9

它的工作正常,但当我使用付款方式时," paypal_express "它重定向错误如: PayPal网关已拒绝请求。无效令牌(#10410:无效令牌) 给出正确的建议如何解决它。

我的代码赞:

$ch = curl_init();
        $clientId = "******";
        $secret = "*******";


        curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

        //curl_setopt($ch, CURLOPT_HTTPAUTH, true);
        $result = curl_exec($ch);

        if(empty($result))die("Error: No response.");
        else
        {
            $json = json_decode($result);
            $type = $json->token_type;
            $token = $type." ".$json->access_token;
            $proId = $this->getRequest()->getParam('payid');
            //$proId1 = "PAY-6PY01870PN0146745KZTHFDY";
            $proUrl = "https://api.sandbox.paypal.com/v1/payments/payment/".$proId;

            $headers = array( 
                        "Content-type: application/json", 
                        "Authorization: ".$token,
                        );
            curl_setopt($ch, CURLOPT_URL, $proUrl);
            //curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            //curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            $orderResult = curl_exec($ch);
            $json = json_decode($orderResult);
            $PayerID = $json->payer->payer_info->payer_id;
            $PayerEmail = $json->payer->payer_info->email;
            $PayerStatus = $json->payer->status;
            $Merchant = $json->transactions[0]->related_resources[0]->sale->protection_eligibility;
            $TransactionId = $json->transactions[0]->related_resources[0]->sale->id;
            // echo $json->state."<br>";
            // echo $json->payer->payment_method."<br>";

        }

        curl_close($ch);

        /* End Access Token */

        $userid = $this->getRequest()->getParam('userid');
        $firstname = $this->getRequest()->getParam('firstname');
        $lastname = $this->getRequest()->getParam('lastname');
        $telephone = $this->getRequest()->getParam('telephone');
        $street[0] = $this->getRequest()->getParam('street');
        $city = $this->getRequest()->getParam('city');
        $region_id = $this->getRequest()->getParam('region_id');
        $region = $this->getRequest()->getParam('region');
        $country_id = $this->getRequest()->getParam('country_id');
        $postcode = $this->getRequest()->getParam('postcode');
        $shippingmethod = $this->getRequest()->getParam('shippingmethod');
        $shippingprice = $this->getRequest()->getParam('shippingprice');
        $paymentmethod = $this->getRequest()->getParam('payment');
        $email = $this->getRequest()->getParam('email');
        $productId = $this->getRequest()->getParam('product_id');
        $qty = $this->getRequest()->getParam('qty');
        $websiteId = Mage::app()->getWebsite()->getId();
        $optionValue = $this->getRequest()->getParam('optionvalue');
        //echo "pro".$productId."--wty--".$qty."--op--".$optionValue;die();
        $store = Mage::app()->getStore();

        $customerAccountNo = $userid;
        if($customerAccountNo)
        {
            // load customer object
            $customerObj = Mage::getModel('customer/customer')->load($customerAccountNo);
            // assign this customer to quote object, before any type of magento order, first create quote.
            $quoteObj = Mage::getModel('sales/quote')->assignCustomer($customerObj);

            $quoteObj = $quoteObj->setStoreId(Mage::app()->getStore()->getId());

            // product id
            $productId = 1781;
            $productModel = Mage::getModel('catalog/product');
            $productObj = $productModel->load($productId);

            // for simple product
            if ($productObj->getTypeId() == 'simple') 
            {
                $quoteObj->addProduct($productObj , 1);
                // for downloadable product
            } 
            else if ($productObj->getTypeId() == 'downloadable') 
            {
                $params = array();
                $links = Mage::getModel('downloadable/product_type')->getLinks( $productObj );
                $linkId = 0;
                foreach ($links as $link) {
                    $linkId = $link->getId();
                }
                $params['product'] = $productId;
                $params['qty'] = $qty;
                $params['links'] = array($linkId);
                $request = new Varien_Object();
                $request->setData($params);
                $quoteObj->addProduct($productObj , $request);
            }
            elseif ($productObj->getTypeId() == 'configurable') 
            {

                $optionValue = $this->getRequest()->getParam('optionvalue');
                $param = array(
                            'product' => $productId,
                            'super_attribute' => array(
                            184 => 50
                            ),
                            'qty' => 2
                        );

                $quoteObj->addProduct($productObj,new Varien_Object($param));
            }
            // sample billing address
            $billingAddress = array
            (
                'email' => $email,
                'firstname' => $firstname,
                'lastname' => $lastname,
                'telephone' => $telephone,
                'street' => $street[0],
                'country_id' => $country_id,
                'city' => $city,
                'postcode' => $postcode ,
                'region_id' => $region_id,
                'region' => $region,
                'customer_address_id' => NULL,
            );

            $quoteBillingAddress = Mage::getModel('sales/quote_address');
            $quoteBillingAddress->setData($billingAddress);
            $quoteObj->setBillingAddress($quoteBillingAddress);

            //if product is not virtual
            if (!$quoteObj->getIsVirtual()) 
            {
                $shippingAddress = $billingAddress;
                $quoteShippingAddress = Mage::getModel('sales/quote_address');
                $quoteShippingAddress->setData($shippingAddress);
                $quoteObj->setShippingAddress($quoteShippingAddress);
                // fixed shipping method
                $quoteObj->getShippingAddress()->setShippingMethod('ups_03');
                $quoteObj->getShippingAddress()->setCollectShippingRates(true);
                $quoteObj->getShippingAddress()->collectShippingRates();
                $quoteObj->getShippingAddress()->setPaymentMethod('paypal_express');
            }

            $quoteObj->collectTotals();
            $quoteObj->save();


            $transaction = Mage::getModel('core/resource_transaction');
            if ($quoteObj->getCustomerId()) 
            {
                $transaction->addObject($quoteObj->getCustomer());
            }
            $transaction->addObject($quoteObj);
            $quoteObj->reserveOrderId();

            $quotePaymentObj = $quoteObj->getPayment();
            $quotePaymentObj->setMethod('paypal_express');
            $quoteObj->setPayment($quotePaymentObj);


            $convertQuoteObj = Mage::getSingleton('sales/convert_quote');
            if ($quoteObj->getIsVirtual()) 
            {
                $orderObj = $convertQuoteObj->addressToOrder($quoteObj->getBillingAddress());
            } 
            else 
            {
                $orderObj = $convertQuoteObj->addressToOrder($quoteObj->getShippingAddress());
            }

            $orderPaymentObj = $convertQuoteObj->paymentToOrderPayment($quotePaymentObj);

            $orderObj->setBillingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getBillingAddress()));
            $orderObj->setPayment($convertQuoteObj->paymentToOrderPayment($quoteObj->getPayment()));
            if (!$quoteObj->getIsVirtual()) 
            {
                $orderObj->setShippingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getShippingAddress()));
            }

            // set payment options

            $orderObj->setPayment($convertQuoteObj->paymentToOrderPayment($quoteObj->getPayment()));


            $items=$quoteObj->getAllItems();

            foreach ($items as $item) 
            {
            //@var $item Mage_Sales_Model_Quote_Item
                $orderItem = $convertQuoteObj->itemToOrderItem($item);
                if ($item->getParentItem()) {
                    $orderItem->setParentItem($orderObj->getItemByQuoteItemId($item->getParentItem()->getId()));
                }
                $orderObj->addItem($orderItem);
            }

            $orderObj->setCanShipPartiallyItem(false);

            $totalDue = $orderObj->getTotalDue();

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

            try 
            {
                $transaction->save();
            } catch (Exception $e){
                echo $e->getMessage();
                //Mage::throwException('Order Cancelled Bad Response from Credit Authorization.');
            }

            $orderObj->sendNewOrderEmail();

            Mage::dispatchEvent('checkout_type_onepage_save_order_after', array('order'=>$orderObj, 'quote'=>$quoteObj));

            $quoteObj->setIsActive(0);
            $quoteObj->save();
        }

0 个答案:

没有答案