Shell script to create new recurring orders
The attached script at the bottom of this page will allow you to create a new recurring order in Magento using the Magento SOAP API the same way the Subscribe Pro system does. This will allow you to diagnose and troubleshoot any connection or order creation issues.
To run the script, you will need to modify several configuration variables at the top, which will allow the script to connect to your Magento store and will create a cart with the specified product, shipping and billing information. Note that an existing payment token must be supplied in the payment method configuration.
This script will not make any changes on Subscribe Pro, such as updating the Last or Next Order Dates, and will not trigger any emails from Subscribe Pro. However, if Magento is configured to send an email to the customer upon a successful or failed order, the customer will still receive that email.
Script Source Code
<?php
/**
* Subscribe Pro - Subscriptions Management Extension
*
* PHP version 5
*
* LICENSE: This source file is subject to commercial source code license of SUBSCRIBE PRO INC.
*
* @category SFC
* @package SFC_Autoship
* @author David King <[email protected]>
* @author Garth Brantley <[email protected]>
* @copyright 2009-2016 SUBSCRIBE PRO INC. All Rights Reserved.
* @license http://www.subscribepro.com/terms-of-service/ Subscribe Pro Terms of Service
* @link http://www.subscribepro.com/
*
*/
// Configuration Variables
/** Connection Details **/
$soapApiUser = 'user';
$soapApiKey = 'apikey';
$soapApiUrl = 'https://example.com/index.php/api/soap';
$magentoStoreCode = 'default';
/** Cart Data **/
$shippingMethod = 'flatrate_flatrate';
$productData = array(
'sku' => 'test-product',
'qty' => 1,
// 'item_fulfils_subscription' => true,
// 'subscription_id' => '',
// 'subscription_interval' => 'Month', // e.g. '1 month'
);
// Set the customer information here:
$customerData = array(
'customer_id' => 1,
'firstname' => '',
'lastname' => '',
'email' => '',
'mode' => 'customer',
);
// Set the addresses
$shippingAddress = array(
'mode' => 'shipping',
'firstname' => '',
'lastname' => '',
'company' => '',
'street' => '',
'city' => '',
'region' => '', // MD, AZ, NY, etc.
'postcode' => '',
'country_id' => '', // US, UK, etc.
'telephone' => '',
'fax' => '',
'is_default_shipping' => 0,
'is_default_billing' => 0,
);
// To save time, set this to true if the addresses should be the same
// If this is true, will use the above shipping address for both
// billing and shipping
$sameBillingAndShippingAddress = true;
$billingAddress = $sameBillingAndShippingAddress ? array('mode' => 'billing') + $shippingAddress : array(
'mode' => 'billing',
'firstname' => '',
'lastname' => '',
'company' => '',
'street' => '',
'city' => '',
'region' => '',
'postcode' => '',
'country_id' => '', // US, UK, etc.
'telephone' => '',
'fax' => '',
'is_default_shipping' => 0,
'is_default_billing' => 0,
);
$creditCardData = array(
'method' => 'subscribe_pro',
//Use these commented fields with 'subscribe_pro' method
// 'payment_token' => 'xxxxxxxxxxxxxx', // grab this from the payment profile in SP
// 'is_new_card' => '1',
// 'save_card' => '0',
// 'cc_number' => '4111111111111111',
// 'cc_type' => 'VI',
// 'cc_exp_month' => '01',
// 'cc_exp_year' => '2018',
);
// End of Configuration Variables
// Do not modify below this line unless you know what you're doing.
// Add ?wsdl to the URL if needed
$soapApiUrl = (strpos($soapApiUrl, '?wsdl') === false) ? $soapApiUrl . '?wsdl' : '';
// Create the SOAP Client object
$soapClient = new \SoapClient(
$soapApiUrl,
array(
'trace' => true,
'exceptions' => true,
'cache_wsdl' => WSDL_CACHE_BOTH,
'keep_alive' => true,
)
);
try {
// Login to the SOAP API and store the session ID
$sessionId = $soapClient->login($soapApiUser, $soapApiKey);
// Make a call to the cart.create endpoint, get the Quote ID
$quoteId = $soapClient->call(
$sessionId,
'cart.create',
array($magentoStoreCode)
);
// Set the customer on the quote
$setCustomerResult = $soapClient->call(
$sessionId,
'cart_customer.set',
array(
$quoteId,
$customerData,
)
);
// foreach ($productsData as $productData) {
// Add the product to the cart
$setProductResult = $soapClient->call(
$sessionId,
'cart_product.add',
array(
$quoteId,
array($productData),
$magentoStoreCode
)
);
// }
$cart = $soapClient->call(
$sessionId,
'cart.info',
array($quoteId, $magentoStoreCode)
);
// Add customer addresses
$setAddressesResult = $soapClient->call(
$sessionId,
'cart_customer.addresses',
array(
$quoteId,
array(
$shippingAddress,
$billingAddress,
),
)
);
// Set the shipping method
$setShippingMethodResult = $soapClient->call(
$sessionId,
'cart_shipping.method',
array(
$quoteId,
$shippingMethod,
$magentoStoreCode,
)
);
// Set the payment method
$setPaymentMethodResult = $soapClient->call(
$sessionId,
'cart_payment.method',
array(
$quoteId,
$creditCardData,
$magentoStoreCode,
)
);
// Finally, create the order
$orderId = $soapClient->call(
$sessionId,
'cart.order',
array(
$quoteId,
$magentoStoreCode
)
);
echo 'Order #' . $orderId . ' created successfully.' . "\n";
} catch (\SoapFault $e) {
// Output result
echo 'SOAP request headers: ' . "\n";
echo $soapClient->__getLastRequestHeaders() . "\n";
echo 'SOAP request body: ' . "\n";
echo $soapClient->__getLastRequest() . "\n";
echo 'SOAP response headers: ' . "\n";
echo $soapClient->__getLastResponseHeaders() . "\n";
echo 'SOAP response body: ' . "\n";
echo $soapClient->__getLastResponse() . "\n";
echo "\n";
} catch(Exception $e) {
// Output error message
echo $e->getMessage();
}
The script can be run from any server that has PHP installed with an active internet connection that has access to your Magento API server. To run the script, first configure the above variables, and then upload it to the desired location and execute the following commands:
cd /location/of/script
php create_recurring_order.php
The script can also be run from a web browser if the file is in a web-accessible directory.
When the script runs successfully, it will provide output such as:
Order #100000119 created successfully.