3

I am looking to build a SOAP API that passes a String to a portal via webservice after a payment token is received from PayPal and the customer is directed to the success page. I've been working through the Observer and Event XML, but there has been some snags like the service not firing and the success page showing up blank and I am unsure of why. Not exactly as familiar with SOAP as I would like, but it needs to be in that format as that is how the third party works. This is what I have now:

vendor/module/Api/Data/api.php

   <?php

   namespace vendor\module\Api\Data;




   $mage_url = >'http://test.webservice.com/Authentication.asmx?wsdl';

   $mage_user = 'test';

   $mage_api_key = 'test';

   // Initialize the SOAP client
   $soap = new SoapClient( $mage_url );
   // Login to Magento

   $session_id = $soap->login( $mage_user, $mage_api_key );

   $result = $soap->call($session_id, 'api');

   $this->tokenCredentials = >array("username"=>$mage_user,"password"=>$mage_api_key,"url"=>$mage_url);
   $this->url = >'http://test.webservice.com/Functions.asmx?wsdl';

   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

   $session = $objectManager->get('Magento\Customer\Model\Session');

   $custID = $session->getCustomerId();

   $orderId = $this->getOrderId();

   $order = $objectManager->create('Magento\Sales\Model\Order')->load($orderId);

   $paymentAdditionalInfo = $order->getPayment()->getAdditionalInformation();

   // get PayPal express checkout token
   $token = $paymentAdditionalInfo['paypal_express_checkout_token'];

   // get PayPal correlation ID
   $correlationId = $paymentAdditionalInfo['paypal_correlation_id'];

   $shippingAddress = $order->getShippingAddress();

   $email = $shippingAddress->getEmail();

   $total = $order->getGrandTotal();

   $items = $order->getAllItems();

   $subtotal = $order->getSubtotal();

   $firstName = $shippingAddress->getFirstname();

   $lastName = $shippingAddress->getLastname();

      $prods = array();
      foreach($items as $i) {
          $productId = $i->getProductId();
           $product = $i->getProduct();
           $_product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
           $finalprice = $product->getResource()->getAttribute('final_price')->getFrontend()->getValue($product);
           $qty = $i->getQtyOrdered();
           $prodName = $i->getName();
   array_push($prods, "['$productId','$prodName','$qty', '$finalprice']");

       }

   $prodArray = implode(",", $prods);


   $apiString = $orderId . ',' . $custId . ',' . $firstName . ' ' . $lastName . ',' . $email  . ',' . $prodArray;

   echo $apiString;

   $header = new SoapHeader('example.com/', 'UserCredentials',
   array(
   'userName' => "test",
   'password' => "test",
   )
   );
   $client->__setSoapHeaders(array($header));

   print_r($client->add(array("$apiString")));

vendor/module/etc/frontend/events.xml

   <?xml version="1.0"?>
   <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
       <event name="sales_order_place_after">
           <observer name="get_place_order" instance="vendor\module\Observer\PlaceOrder"/>
       </event>
   </config>

vendor/module/Observer/PlaceOrder.xml

   <?php

   namespace vendor\module\Observer;

   use Magento\Framework\Event\ObserverInterface;
   use Magento\Framework\Event\Observer;
   use Vendor\Customer\Model\Customer;
   use Vendor;

   class PlaceOrder implements ObserverInterface
   {
        /** @var \Magento\Framework\Logger\Monolog */

       protected $_logger;
       protected $_customer;
       public function __construct(
           \Psr\Log\LoggerInterface $logger, //log injection
           \Vendor\Customer\Model\Customer $customer)
        {
           $this->_logger = $logger;
           //parent::__construct($data);
       }
       public function execute(Observer $observer) {
       {
           $order = $observer->getEvent()->getOrder(); 
           $result = (array) $this->_customer->getOrderData($order);
       }
   }

Any help would be greatly appreciated!

1

I found that I was able to actually take the entire webservice and bind it within the observer and that worked fine after some troubleshooting. I just removed the classes, set the functions and wrapped them in a try-catch among other things. If anyone would like to see an example of my solution just comment and let me know.

| improve this answer | |
  • 1
    can you please post your solution? with SOAP example working? – Magento2 Devloper Mar 25 '19 at 6:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.