Gravity Forms – napojení na dopravce GLS

Plugin Gravity Forms je velmi oblíbený plugin na formuláře ve WordPressu. Klienti ho používají na leccos, od jednoduchých kontaktních formulářů po složitější objednávkové formuláře.

Jedním ze složitějších formulářů byl formulář na objednávku svozu opravy elektroniky na webu rgservice.cz. Pro zjednodušení admnistrativy si klient přál napojit formulář přes API  GLS tak, aby se doprava automaticky vložila do systému GLS po odeslání formuláře.

Kód, který to zařídí, vypadá cca takto:

[php]
<?php
namespace RG_Computer;
define("APP_WEB_ROOT", realpath(dirname(__FILE__)));

class GF {
private $client;
private $username = ‚123456789‘;
private $password = ‚password‘;
private $sender_id = ‚123456789‘;
private $email = ‚some@company.cz‘;

function __construct()
{
// Setup the client
$this->client = new \nusoap_client(‚http://online.gls-czech.com/webservices/soap_server.php?wsdl&ver=16.12.15.01‘, ‚wsdl‘); // for live server
//$this->client = new \nusoap_client(‚http://truzicka.dev.online.gls-czech.com/webservices/soap_server.php?wsdl&ver=16.12.15.01‘, ‚wsdl‘); // for test server

$this->client->soap_defencoding = ‚UTF-8‘;
$this->client->decode_utf8 = false;
}

/**
* Initialize the action
*/
function initialize() {
add_action( ‚gform_after_submission_1′, array($this,’send_order_to_gls‘), 10, 2 );
}

/**
* On Gravity forms submission, send request to GLS
* @param $entry
* @param $form
*/
function send_order_to_gls($entry, $form) {
// Check if the user requested to order the shipping, if so, send the request to GLS
if (rgar($entry,’10.2′) == ‚OBJEDNAT SVOZ‘) {
$data = array(
‚username‘ => $this->username,
‚password‘ => $this->password,
‚senderid‘ => $this->sender_id,

‚sender_name‘ => rgar($entry,’14.3′),
‚sender_address‘ => rgar($entry,’15.1′),
‚sender_city‘ => rgar($entry,’15.3′),
‚sender_zipcode‘ => rgar($entry,’15.5′),
‚sender_country‘ => ‚CZ‘,
‚sender_contact‘ => rgar($entry,’25.3′),
‚sender_phone‘ => rgar($entry,’19‘),
‚sender_email‘ => rgar($entry,’20‘),

‚consig_name‘ => ‚Firma s.r.o.‘,
‚consig_address‘ => ‚Hradecká 22‘,
‚consig_city‘ => ‚Praha‘,
‚consig_zipcode‘ => ‚12345‘,
‚consig_country‘ => ‚CZ‘,
‚consig_contact‘ => ‚Jan Vomáčka‘,
‚consig_phone‘ => ‚607963703‘,
‚consig_email‘ => $this->email,

‚pcount‘ => 1,
‚pickupdate‘ => rgar($entry,’11‘),
‚content‘ => “,
‚codamount‘ => 0,
‚services‘ => array(
array(‚code‘ => ‚PRS‘, ‚info‘ => rgar($entry,’11‘))
),
‚timestamp‘ => date(‚YmdHis‘,strtotime(‚now + 2 hours‘)),
‚hash‘ => “,
‚customlabel‘ => false,
‚is_autoprint_pdfs‘ => false
);

$data[‚hash‘] = $this->getHash($data);

$this->printlabel($data);
}
}

/**
* Return the Hash from data.
* @param array $data
* @return string
*/
function getHash($data)
{
$hashBase = “;

foreach($data as $key => $value)
{
if ($key != ‚services‘
&& $key != ‚hash‘
&& $key != ‚timestamp‘
&& $key != ‚printit‘
&& $key != ‚printertemplate‘
&& $key != ‚customlabel‘
&& $key != ‚is_autoprint_pdfs‘)
{
$hashBase .= $value;
}
}

//$hashBase = preg_replace(‚/\r\n|\r|\n/‘, “, $hashBase);
return sha1($hashBase);
}

/**
* Send request and show result.
* @param array $client
* @param array $auth
*/
function printlabel($data)
{

$return = $this->client->call(‚printlabel‘, $data);

if ($return)
{
if ($return[‚successfull‘])
{
wp_mail($this->email,’GLS doprava byla objednána‘,’Doprava pro ‚.$data[‚sender_name‘].‘ byla úspěšně objednána.‘);
}
else
{
wp_mail($this->email,’GLS dopravu se nepodařilo objednat‘,’Dopravu pro ‚.$data[‚sender_name‘].‘ se nepoda5ilo objednat. Chyba: ‚.$return[‚errdesc‘]);
}
}
}
}
$gf = new GF();
$gf->initialize();
[/php]

Pokud budete potřebovat pomoci s napojením WordPress formulářů (nejen Gravity Forms, ale třeba Contact Form 7 nebo WP Forms) na služby třetích stran, neváhejte mě kontaktovat

Přidat komentář