Skip to content

Instantly share code, notes, and snippets.

@cherrykoda
Created March 22, 2021 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cherrykoda/c75a5ad4ba02b13c99c924f6cc91f71f to your computer and use it in GitHub Desktop.
Save cherrykoda/c75a5ad4ba02b13c99c924f6cc91f71f to your computer and use it in GitHub Desktop.
AX Craft Controller
<?php
/**
* Microsoft AX module for Craft CMS 3.x
*
* Integrates Craft CMS 3.X with an instance of Microsoft Dynamics AX
*
* @link dodgyco.de
* @copyright Copyright (c) 2021 Shawna O'Neal
*/
namespace modules\microsoftax\controllers;
use modules\microsoftax\MicrosoftAx;
use Craft;
use craft\web\Controller;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use Sabre;
/**
* Default Controller
*
* Generally speaking, controllers are the middlemen between the front end of
* the CP/website and your module’s services. They contain action methods which
* handle individual tasks.
*
* A common pattern used throughout Craft involves a controller action gathering
* post data, saving it on a model, passing the model off to a service, and then
* responding to the request appropriately depending on the service method’s response.
*
* Action methods begin with the prefix “action”, followed by a description of what
* the method does (for example, actionSaveIngredient()).
*
* https://craftcms.com/docs/plugins/controllers
*
* @author Shawna O'Neal
* @package MicrosoftAx
* @since 1.0.0
*/
class DefaultController extends Controller
{
// Protected Properties
// =========================================================================
/**
* @var bool|array Allows anonymous access to this controller's actions.
* The actions must be in 'kebab-case'
* @access protected
*/
protected $allowAnonymous = ['index', 'curl-order'];
// Public Methods
// =========================================================================
/**
* Handle a request going to our module's index action URL,
* e.g.: actions/microsoftax/default
*
* @return mixed
*/
public function actionIndex()
{
/** Index Functionality Goes Here **/
}
/**
* Handle a request going to our module's actionCurlOrder URL,
* e.g.: actions/microsoftax/default/curl-order
*
* @return mixed
*/
public function actionCurlOrder()
{
$ns1url = 'http://schemas.xmlsoap.org/soap/envelope/';
$ns2url = 'http://schemas.microsoft.com/dynamics/2008/01/services';
$ns3url = 'http://schemas.microsoft.com/dynamics/2008/01/documents/SalesOrder';
// Sabre needs the urls above to include as namespace urls,
// For indicating namespacing per node, we need the curly brackets added.
$ns1 = '{' . $ns1url . '}';
$ns2 = '{' . $ns2url . '}';
$ns3 = '{' . $ns3url . '}';
// Initiate the Sabre XML service
$xmlservice = new Sabre\Xml\Service();
$xmlservice->namespaceMap = [
$ns1url => 'SOAP-ENV',
$ns2url => 'x2',
$ns3url => 'x1',
];
$xmlData = $xmlservice->write($ns1 . 'Envelope',
[
$ns1 . 'Body' => [
[
"name" => $ns2 . 'SalesOrderServiceCreateRequest',
'value' => [
[
'name' => $ns3 . 'SalesOrder',
'value' =>
[
'name' => $ns3 . 'SalesTable',
'attributes' => ['class' => 'entity'],
'value' => [
$ns3 . 'CurrencyCode' => 'USD',
$ns3 . 'CustAccount' => 'XXXXXXXXXX',
$ns3 . 'DeliveryName' => 'John Doe',
$ns3 . 'DlvMode' => 'FEDEX-HD',
$ns3 . 'Email' => 'John.Doe@microsoft.com',
$ns3 . 'InvoiceAccount' => 'XXXXXXXXXX',
$ns3 . 'PaymMode' => 'CC',
$ns3 . 'PurchaseOrderFormNum' => '0123456789',
$ns3 . 'SalesOriginId' => 'WEB',
[
'name' => $ns3 . 'TableDlvAddr',
'attributes' => ['class' => 'entity'],
'value' => [
$ns3 . 'Address' => '123 MYSTERY ROAD',
$ns3 . 'City' => 'KANSAS CITY',
$ns3 . 'CountryRegionId' => 'USA',
$ns3 . 'LocationName' => 'John Doe',
$ns3 . 'State' => 'MO',
$ns3 . 'ZipCode' => '01234',
]
],
[
'name' => $ns3 . 'SalesLine',
'attributes' => ['class' => 'entity'],
'value' => [
$ns3 . 'ItemId' => 'XXXXXX',
$ns3 . 'SalesQty' => 1.0,
$ns3 . 'SalesUnit' => 'ea',
$ns3 . 'SalesPrice' => 159.9900
]
] // End SalesLine
]
] // End SalesTable
] // End SalesOrder
] // End SalesOrderServiceCreateRequest
]
] // End Body
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://domain.com:8201/XXXXX/SERVICENAME/xppservice.svc");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "DOMAIN\username" . ":" . "password");
$headers = array();
$headers[] = "Content-Type: text/xml; charset=\"utf-8\"";
$headers[] = "Soapaction:http://schemas.microsoft.com/dynamics/2008/01/services/SalesOrderService/create";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
return $result;
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment