getId() method is undefined in sandbox Square Checkout API script - php

I'm hoping to get a little assistance with something that is probably pretty basic -- I'm attempting to deploy the Square Checkout API with my website. I've been able to successfully install the SDK, and I've used it to successfully pull my sandbox location ID, to test it's function.
I've proceeded to build a page employing only the demo script on the Checkout API page, as seen below:
<?php
#Set the required includes globally
require_once '../config.php';
require INC_PATH . '/squareup/autoload.php';
/*
** Script for submitting payment information
** Utilizing Square API documentation at:
** https://docs.connect.squareup.com/payments/checkout/setup
*/
//Replace your access token and location ID
$accessToken = '<MY SANDBOX KEY>'; // Sandbox
$locationId = '<MY SANDBOX LOCATION ID>'; // Sandbox
// Create and configure a new API client object
$defaultApiConfig = new \SquareConnect\Configuration();
$defaultApiConfig->setAccessToken($accessToken);
$defaultApiClient = new \SquareConnect\ApiClient($defaultApiConfig);
$checkoutClient = new SquareConnect\Api\CheckoutApi($defaultApiClient);
//Create a Money object to represent the price of the line item.
$price = new \SquareConnect\Model\Money;
$price->setAmount(600);
$price->setCurrency('USD');
//Create the line item and set details
$book = new \SquareConnect\Model\CreateOrderRequestLineItem;
$book->setName('The Shining');
$book->setQuantity('2');
$book->setBasePriceMoney($price);
//Puts our line item object in an array called lineItems.
$lineItems = array();
array_push($lineItems, $book);
// Create an Order object using line items from above
$order = new \SquareConnect\Model\CreateOrderRequest();
$order->setIdempotencyKey(uniqid()); //uniqid() generates a random string.
//sets the lineItems array in the order object
$order->setLineItems($lineItems);
## STEP 2: Create a checkout object
$checkout = new \SquareConnect\Model\CreateCheckoutRequest();
$checkout->setIdempotencyKey(uniqid()); //uniqid() generates a random string.
$checkout->setOrder($order); //this is the order we created in the previous step
try {
$result = $checkoutClient->createCheckout(
$locationId,
$checkout
);
//Save the checkout ID for verifying transactions
$checkoutId = $result->getId();
//Get the checkout URL that opens the checkout page.
$checkoutUrl = $result->getCheckoutPageUrl();
print_r('Complete your transaction: ' + $checkoutUrl);
}
catch (Exception $e) {
echo 'Exception when calling CheckoutApi->createCheckout: ', $e->getMessage(), PHP_EOL;
}
I get a 500 error from my webserver when attempting to run this script through my browser, in my httpd error_log I get the following error message:
PHP Fatal error: Uncaught Error: Call to undefined method SquareConnect\\Model\\CreateCheckoutResponse::getId() in <LOCATION>:62\nStack trace:\n#0 {main}\n thrown in <LOCATION> on line 62
Any thoughts on why the getId() method is undefined? Thanks.
UPDATE
I commented out the function calls after the createCheckout() portion of the try{} block, and then ran a var_dump() on $result to make sure I was in fact getting some sort of response. I am getting back the expected result! So I KNOW the API/SDK is working now, I just can't figure out why the $result object is unable to accept the follow-up functions.
Revised try block:
try {
$result = $checkoutClient->createCheckout(
$locationId,
$checkout
);
/*
//Save the checkout ID for verifying transactions
$checkoutId = $result->getId();
//Get the checkout URL that opens the checkout page.
$checkoutUrl = $result->getCheckoutPageUrl();
print_r('Complete your transaction: ' + $checkoutUrl);
*/
}
catch (\Exception $e) {
echo 'Exception when calling CheckoutApi->createCheckout: ', $e->getMessage(), PHP_EOL;
}
var_dump($result); //test to see if any non-zero response to createCheckout() function.
Any thoughts based on this revision? -A

The CreateCheckoutResponse doesn't have the getId() function. It has getCheckout() and getErrors(). So you need to:
$checkoutId = $result->getCheckout()->getId();
Reference: https://github.com/square/connect-php-sdk/blob/master/docs/Model/CreateCheckoutResponse.md

Current Solution:
function objectToArray ($object) {
if(!is_object($object) && !is_array($object))
return $object;
return array_map('objectToArray', (array) $object);
}
$result_array = objectToArray($result);
echo '<pre>';
var_dump($result_array); //test to see if any non-zero response to createCheckout() function.
echo '</pre>';
Since I am getting a valid object back, and all I need to do is extract the ID and checkout URL's from the $result object, I used the function above to convert the object to an array, and from here I'll extract the information I need by key => Value pairing. It's ugly, and it doesn't solve why these post API-call functions included with the SDK aren't working, but it's meeting my immediate solution.
If anyone can tell me what actually happened that prevented the SDK function calls from being defined, I'd appreciate it.

Related

Xero Create new Tracking Option

I can't seem to get tracking options created, the Category itself is creating fine.
However firstly - I should point out I believe there is a bug in the Xero-API for PHP, when debugging adding an option according to the documentation here the PUT should be
https://api.xero.com/api.xro/2.0/TrackingCategories/{TrackingCategoryID}/Options
However in the php lib it is
https://api.xero.com/api.xro/2.0/TrackingCategories/{TrackingCategoryID}/TrackingOptions
Even when that is resolved, I get no error however not tracking Option is created, any ideas?
$options = ['US', 'UK'];
$title = 'Region';
$trackingCategory = null;
if(!$trackingCategory) {
$trackingCategory = new \XeroPHP\Models\Accounting\TrackingCategory($xero);
$trackingCategory->setName($title);
$trackingCategory->save();
}
try {
foreach($options as $option) {
$to = new \XeroPHP\Models\Accounting\TrackingCategory\TrackingOption($xero);
$to->setName($option);
$trackingCategory->setOption($option);
$trackingCategory->save();
}
} catch(\Exception $e) {
$this->logger()->info($e->getTraceAsString());
$this->logger()->info("TRACKING: ". $e->getMessage());
return false;
}
So this would appear it is a bug as reported here
The source has not been fixed, however the above link resolves the problem for anyone else searching.
Changing TrackingOptions to Options in XeroPHP worked soughta... but I was still getting a different error. Ended up creating the Option manually
Note: $this->_xero_oauth_object is my \XeroPHP\Application\PublicApplication from authentication
// Create the URL object based on an absolute URL
$url = new \XeroPHP\Remote\URL($this->_xero_oauth_object, "https://api.xero.com/api.xro/2.0/TrackingCategories/{TrackCategoryGuid}/Options");
// Pass this to the request as a PUT request
$request = new \XeroPHP\Remote\Request($this->_xero_oauth_object, $url, \XeroPHP\Remote\Request::METHOD_PUT);
// Probably a better way but I just copied and paste the XML from the Xero API docs.
$request->setBody("<Options><Option><Name>My New Option Name</Name></Option></Options>");
// I wrapped this in a try - if it exists, there will be an error as you cant have duplicates.
try {
$request->send();
} catch (Exception $e) {
\Log::warn("Xero error: " . print_r($request->getResponse(), true));
}
// now option is created, new add the option to the tracking category
$tracking = new \XeroPHP\Models\Accounting\TrackingCategory($this->_xero_oauth_object);
$tracking->setTrackingCategoryID('3fceedc7-764e-490a-ac27-25684473af78');
// tracking category name - not sure if I need this
$tracking->setName('Contractor');
// match the option name above
$tracking->setOption('My New Option Name');

Amazon MWS (PHP) - Report Request API functions return without data, no error thrown

I am currently working with the Amazon MWS to integrate some features into wordpress via a plugin. I am using the client libraries provided by amazon found here:
https://developer.amazonservices.com/api.html?group=bde&section=reports&version=latest
Using these client libraries and the sample php files included I have set up my plugin to make two API calls. The first is requestReport
public function requestInventoryReport() {
AWI_Amazon_Config::defineCredentials(); // Defines data for API Call
$serviceUrl = "https://mws.amazonservices.com";
$config = array (
'ServiceURL' => $serviceUrl,
'ProxyHost' => null,
'ProxyPort' => -1,
'MaxErrorRetry' => 3,
);
$service = new MarketplaceWebService_Client(
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
$config,
APPLICATION_NAME,
APPLICATION_VERSION);
$request = new MarketplaceWebService_Model_RequestReportRequest();
$request->setMerchant(MERCHANT_ID);
$request->setReportType('_GET_MERCHANT_LISTINGS_DATA_');
self::invokeRequestReport($service, $request);
}
private function invokeRequestReport(MarketplaceWebService_Interface $service, $request) {
try {
$response = $service->requestReport($request);
if ($response->isSetRequestReportResult()) {
// Print Out Data
}
} catch (MarketplaceWebService_Exception $ex) {
// Print Out Error
}
}
and the second is getReportRequestList which has code similar to the first function. I am able to run these functions without any errors. The issue that I am having is that $response->isSetRequestReportResult() returns false. From my understanding and looking into the response object, this would suggest that the response object does not have the result. (Upon printing out the response object I can see that the FieldValue of the result array is NULL.) The call, however, does not throw an error but neither does it have the result.
I did some digging through the code and found that the result does actually get returned from the api call but never gets set to the return object when the library attempts to parse it from XML. I've tracked the error down to this block of code (This code is untouched by me and directly from the amazon mws reports library).
private function fromDOMElement(DOMElement $dom)
{
$xpath = new DOMXPath($dom->ownerDocument);
$xpath->registerNamespace('a', 'http://mws.amazonaws.com/doc/2009-01-01/');
foreach ($this->fields as $fieldName => $field) {
$fieldType = $field['FieldType'];
if (is_array($fieldType)) {
if ($this->isComplexType($fieldType[0])) {
// Handle Data
} else {
// Handle Data
}
} else {
if ($this->isComplexType($fieldType)) {
// Handle Data
} else {
$element = $xpath->query("./a:$fieldName/text()", $dom);
$data = null;
if ($element->length == 1) {
switch($this->fields[$fieldName]['FieldType']) {
case 'DateTime':
$data = new DateTime($element->item(0)->data,
new DateTimeZone('UTC'));
break;
case 'bool':
$value = $element->item(0)->data;
$data = $value === 'true' ? true : false;
break;
default:
$data = $element->item(0)->data;
break;
}
$this->fields[$fieldName]['FieldValue'] = $data;
}
}
}
}
}
The data that should go into the RequestReportResult exists at the beginning of this function as a node in the dom element. The flow of logic takes it into the last else statement inside the foreach. The code runs its query and returns $element however $element->length = 13 in my case which causes it to fail the if statement and never set the data to the object. I have also looked into $element->item(0) to see what was in it and it appears to be a dom object itself matching the original dom object but with a bunch of empty strings.
Now, I'm new to working with the MWS and my gut feeling is that I am missing a parameter somewhere in my api call that is messing up how the data is returned and is causing this weird error, but I'm out of ideas at this point. If anyone has any ideas or could point me in the right direction, I would greatly appreciate it.
Thanks for your time!
** Also as a side note, Amazon Scratchpad does return everything properly using the same parameters that I am using in my code **
These works for me, check if you are missing anything.
For RequestReportRequest i am doing this:
$request = new MarketplaceWebService_Model_RequestReportRequest();
$marketplaceIdArray = array("Id" => array($pos_data['marketplace_id']));
$request->setMarketplaceIdList($marketplaceIdArray);
$request->setMerchant($pos_data['merchant_id']);
$request->setReportType($this->report_type);
For GetReportRequestList i am doing this:
$service = new MarketplaceWebService_Client($pos_data['aws_access_key'], $pos_data['aws_secret_access_key'], $pos_data['config'], $pos_data['application_name'], $pos_data['application_version']);
$report_request = new MarketplaceWebService_Model_GetReportRequestListRequest();
$report_request->setMerchant($pos_data["merchant_id"]);
$report_type_request = new MarketplaceWebService_Model_TypeList();
$report_type_request->setType($this->report_type);
$report_request->setReportTypeList($report_type_request);
$report_request_status = $this->invokeGetReportRequestList($service, $report_request, $report_requestID);

Amazon Associates - Can't find Associate Tag, API and Secret Key

How do I find the necessary keys for this code to work?
<?php
/**
* For a running Search Demo see: http://amazonecs.pixel-web.org
*/
if ("cli" !== PHP_SAPI)
{
echo "<pre>";
}
defined('AWS_API_KEY') or define('AWS_API_KEY', '...');
defined('AWS_API_SECRET_KEY') or define('AWS_API_SECRET_KEY', '...');
defined('AWS_ASSOCIATE_TAG') or define('AWS_ASSOCIATE_TAG', '...');
// require '../lib/AmazonECS.class.php';
require_once ( dirname(__FILE__) . '/../classes/buyamerica/AmazonECS.class.php');
try
{
$amazonEcs = new AmazonECS(AWS_API_KEY, AWS_API_SECRET_KEY, 'com', AWS_ASSOCIATE_TAG);
// for the new version of the wsdl its required to provide a associate Tag
// #see https://affiliate-program.amazon.com/gp/advertising/api/detail/api-changes.html?ie=UTF8&pf_rd_t=501&ref_=amb_link_83957571_2&pf_rd_m=ATVPDKIKX0DER&pf_rd_p=&pf_rd_s=assoc-center-1&pf_rd_r=&pf_rd_i=assoc-api-detail-2-v2
// you can set it with the setter function or as the fourth paramameter of ther constructor above
$amazonEcs->associateTag(AWS_ASSOCIATE_TAG);
// First of all you have to set an another ResponseGroup. If not the request would not be successful
// Possible Responsegroups: BrowseNodeInfo,MostGifted,NewReleases,MostWishedFor,TopSellers
$amazonEcs->responseGroup('BrowseNodeInfo');
// $asin = "B00625Q56U";
$asin = $_REQUEST["asin"];
$res = $amazonEcs->responseGroup('Large')->lookup($asin);
var_dump($res->Items->Item);
}
catch(Exception $e)
{
echo $e->getMessage();
}
if ("cli" !== PHP_SAPI)
{
echo "</pre>";
}
To find your Associate Tag you simply log into your Affiliate Account. On the left you will find a box where it says "Tracking ID", this is what you need. Or simply follow this link: https://affiliate-program.amazon.com/gp/associates/network/your-account/manage-tracking-ids.html
In the AWS management console navigate to 'Security Credentials' under the dropdown over your name (https://console.aws.amazon.com/iam/home?#security_credential). Under 'Access Keys' you can see your root access keys (the ones assigned to your root user created when you signed up with Product Advertising API).
delete one, then generate a new one. It will show you the proper AWS key and Secret Key. These paired with the proper Associate Tag should get you access to the API.

PHP Soap calling Error

I am trying to connect to webservice using SOAP / wsdl, but I constantly get the error. I am new in soap-api in php. I have a document of api detail, it shows:
public WSGetCalendarFareResponse GetCalendarFare(WSGetCalendarFareRequest calanderFareRequest)
I made my code accordingly but still found error/exception. Please review my php code in following:
$wsdl = "http://api.abc.com/xyz/service.asmx?wsdl"; // This is a test Web Service URL
$h = array();
$opta["GetCalendarFare"]["request"]= array(
"Origin"=>"DEL",
"Destination"=>"IXR",
"DepartureDate"=>"2015-05-01T00:00:00",
"ReturnDate"=>"2015-05-01T00:00:00",
"Type"=>"OneWay",
"CabinClass"=>"All",
"PreferredCarrier"=>"",
"AdultCount"=>1,
"ChildCount"=>1,
"InfantCount"=>"0",
"SeniorCount"=>"0",
"PromotionalPlanType"=>"Normal",
"IsDirectFlight"=>false
);
$client_header = new SoapHeader('http://192.168.0.0/TEST/BookingAPI','AuthenticationData',$hparams,false);
$cliente = new SoapClient($wsdl, array('trace' => 0));
$cliente->__setSoapHeaders(array($client_header));
try{
$h= (array)$cliente->__call('GetCalendarFare',$opta);
}catch(Exception $e)
{
echo '<pre>';
var_dump($e);
}
When I execute my code, it returns following error:
"System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at BookingAPI.WSCalendarFareInput(WSGetCalendarFareRequest calanderFareRequest) in c:\inetpub\wwwroot\api.tektravel.com\TboApi_V7\App_Code\Service.cs:line 4544
at BookingAPI.GetCalendarFare(WSGetCalendarFareRequest calanderFareRequest) in c:\inetpub\wwwroot\api.tektravel.com\TboApi_V7\App_Code\Service.cs:line 4360
Can anyone please suggest that where problem exists? It hit & try many times but couldn't get the error point.
Can you debug the execution of your POST on the server side? It is heavy guessing from my side, but I assume that you do not set a mandatory value in the request, which the server needs to deserialize your object. Hence the NullReferenceException.
Problem has resolved.
The Request array should be like this:
$opta["GetCalendarFare"]["calanderFareRequest"]= array(
"Origin"=>"DEL",
"Destination"=>"IXR",
"DepartureDate"=>"2015-05-01T00:00:00",
"ReturnDate"=>"2015-05-01T00:00:00",
"Type"=>"OneWay",
"CabinClass"=>"Economy",
"PreferredCarrier"=>"",
"AdultCount"=>1,
"ChildCount"=>1,
"InfantCount"=>"0",
"SeniorCount"=>"0",
"PromotionalPlanType"=>"Normal",
"IsDirectFlight"=>false
);

Magento Integration With T-Hub

am setting up a sand box for a T-Hub Integration with Magento and quickbooks. I've set my life site up locally using WAMP server, and now Its on to trying to tie that local Magento site into T-hub.
The first error that I received stated the
"Connection to Magento store failed. Service authentication failure - Notice: Undefined index: httponly in c:\wamp\www\testsite\appcode\core\mage\Core\Model\Session\Abtract\Varien.php on line 98."
After some searching I found the the general consensus on that one was I had to put an ssl on my local server, done, that problem's gone. Now I'm get a general error message that simply says "Connection to Magento Failed"
I used the test page that atandra included with their files which returned this:
<RESPONSE Version="4.1">
<Envelope>
<Command>GETORDERS</Command>
<StatusCode>9001</StatusCode>
<StatusMessage>
Service authentication failure - Warning: array_key_exists() expects parameter 2 to be array, string given in C:\wamp\www\adamsarms\app\code\core\Mage\Captcha\Model\Observer.php on line 166
</StatusMessage>
<Provider>Magento</Provider>
</Envelope>
</RESPONSE>
Which kicks back to this is the php file:
public function checkUserLoginBackend($observer)
{
$formId = 'backend_login';
$captchaModel = Mage::helper('captcha')->getCaptcha($formId);
$loginParams = Mage::app()->getRequest()->getPost('login', array());
$login = array_key_exists('username', $loginParams) ? $loginParams['username'] : null;
if ($captchaModel->isRequired($login)) {
if (!$captchaModel->isCorrect($this->_getCaptchaString(Mage::app()->getRequest(), $formId))) {
$captchaModel->logAttempt($login);
Mage::throwException(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
}
}
$captchaModel->logAttempt($login);
return $this;
}
This line is the one it directly points to:
$login = array_key_exists('username', $loginParams) ? $loginParams['username'] : null;
I'm not sure which direction I need to go to fix this error to make t-hub start talking to magento proper, I've included everything that I've got, if someone needs more information please let me know, I just need a better understanding of what might be causing this error to possibly find a path to fixing it.
This is an issue with a Legacy codebase with the T-Hub extension. It was created for PHP 5.3 & Magento versions 1.4 & below. They really should update this thing since people are using it.
The companies official response is this: http://support4.atandra.com/index.php?/Knowledgebase/Article/View/92/4/magento-array_key_exists-error
Which is horrible because it relies on overriding core files.
What's going on is Mage_Captcha_Model_Observer has an event checkUserLoginBackend() that gets fired. This expects the POST info for 'login' to be a certain format. This is something that has changed over the years since legacy code does not have it in this format.
This is a really hacky fix. But it's better than overriding core magento files.
Change the CheckUser() function of Mage/Thub/Model/Run/Run.php to this (I've removed some of their comments):
public function CheckUser()
{
try {
$username = $this->RequestParams['USERID'];
$password = $this->RequestParams['PASSWORD'];
//here we just set the POST to our specified format..
//which is what the observer model thinks it should be
Mage::app()->getRequest()->setPost('login', array(
'username' => $username,
'password' => $password
));
$user = Mage::getSingleton('admin/user');
$userRole = Mage::getSingleton('admin/role');
if ($user->authenticate($username, $password)) {
$loadRole = $userRole->load($user->getRoles($user));
} else {
print($this->xmlErrorResponse($this->RequestParams['COMMAND'], '9000',
'Order download service authentication failure - Login/Password supplied did not match', $this->STORE_NAME, ''));
exit;
}
} catch (Exception $e) {
$this->Msg[] = "Critical Error CheckUser (Exception e)=" . $e->getMessage(); //BB 11Nov2014
print($this->xmlErrorResponse($this->RequestParams['COMMAND'], '9001',
'Service authentication failure - ' . " " . $e->getMessage(), $this->STORE_NAME, ''));
// End - <TIBB> 13Dec2011
exit;
}
}
Another alternative is to extend the Mage_Captcha_Model_Observer class with your own version that removes those array checks in checkUserLoginBackend().

Categories