Magento & Hubspot - Submitting Form on Success Page - php

I'm trying to submit a form to Hubspot on the success page of Magento. I confirmed already that the variable $str_post contains all of the info that's needed. Unfortunately, I can't determine why $response is empty. It seems like the CURL connection is not being made but I can't seem to determine why. Is there something I need to do to trigger the CURL connection other than just loading the page?
Please note I've removed the GUI/Form ID from the URL.
<?php
//Process a new form submission in HubSpot in order to create a new Contact.
$hubspotutk = $_COOKIE['hubspotutk']; //grab the cookie from the visitors browser.
$ip_addr = $_SERVER['REMOTE_ADDR']; //IP address too.
$hs_context = array(
'hutk' => $hubspotutk,
'ipAddress' => $ip_addr,
'pageUrl' => 'https://www.myfoodstorage.com/onestepcheckout/',
'pageTitle' => 'MyFoodStorage.com Cart Checkout'
);
$hs_context_json = json_encode($hs_context);
//Need to populate these varilables with values from the form.
$str_post = "firstname=" . urlencode($firstname)
. "&lastname=" . urlencode($lastname)
. "&email=" . urlencode($email)
. "&phone=" . urlencode($telephone)
. "&address=" . urlencode($street)
. "&city=" . urlencode($city)
. "&state=" . urlencode($region)
. "&country=" . urlencode($country)
. "&hs_context=" . urlencode($hs_context_json); //Leave this one be :)
//replace the values in this URL with your portal ID and your form GUID
$endpoint = 'https://forms.hubspot.com/uploads/form/v2/GUI-ID/FORM-ID';
$ch = #curl_init();
#curl_setopt($ch, CURLOPT_POST, true);
#curl_setopt($ch, CURLOPT_POSTFIELDS, $str_post);
#curl_setopt($ch, CURLOPT_URL, $endpoint);
#curl_setopt($ch, CURLOPT_HTTPHEADER, array('application/x-www-form-urlencoded'));
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = #curl_exec($ch); //Log the response from HubSpot as needed.
#curl_close($ch);
echo $response;
?>

A successful submission to the Forms API will return a 204 (No Content) response, so there would be nothing in the body of the response. You can get the status code using curl_getinfo.
$response = #curl_exec($ch); //Log the response from HubSpot as needed.
echo #curl_getinfo($ch, CURLINFO_HTTP_CODE);
#curl_close($ch);
echo $response;

Related

Adding a Mailchimp User via Notification Script after Wordpress User subscribes (Membermouse) PHP and API

I am working on a MemberMouse subscription Wordpress website. After a User signs up via a webform for a membership, I want to call a script that adds the User to a Mailchimp mailing list WITH a double opt-in. Membermouse offers an integration via Mailchimp, however only with a single opt-in. Therefore, due to government law, I am required to use a double opt-in.
I wrote the following script, that should be called under the condition that once the member is added it sends the following php sript:
<?php
require_once("wp-load.php");
require_once("wp-content/plugins/membermouse/includes/mm-constants.php");
require_once("wp-content/plugins/membermouse/includes/init.php");
// Your Membermouse API URL
$apiUrl = "MYDOMAIN/wp-content/plugins/membermouse/api/request.php";
// Your API key
$apiKey = "my API key";
// Your API secret
$apiSecret = "my API secret";
// ---- GET EVENT TYPE ----
if(!isset($_GET["mm_member_add"]))
{
// event type was not found, so exit
exit;
}
// ---- ACCESS DATA ----
// member data
$username = $_GET["username"];
$email = $_GET["email"];
$apiKey1 = 'my Mailchimp API key';
$listID1 = 'my Mailchimp list ID';
// MailChimp API URL
$memberID1 = md5(strtolower($email));
$dataCenter1 = substr($apiKey1,strpos($apiKey1,'-')+1);
$url1 = 'https://' . $dataCenter1 . '.api.mailchimp.com/3.0/lists/' . $listID1 . '/members/' . $memberID1;
// member information
$json = json_encode([
'email_address' => $email,
'status' => 'pending',
]);
// send a HTTP POST request with curl
$ch1 = curl_init($url1);
curl_setopt($ch1, CURLOPT_USERPWD, 'user:' . $apiKey1);
curl_setopt($ch1, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_TIMEOUT, 10);
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $json);
$result1 = curl_exec($ch1);
$httpCode = curl_getinfo($ch1, CURLINFO_HTTP_CODE);
curl_close($ch1);
break;
echo "<pre>".print_r($result1, true)."</pre>";
?>
However, it will not respond. The script will be executed.
So far so good. However, I am trying since a couple of days how to write this PHP to get this be working. Additionally, I am not sure if the PHP code is even correct. This was just an assumption.
For references I found this script from membermouse: https://dl.dropboxusercontent.com/u/265387542/files/member_notification_script.php
And this script for Mailchimp: https://www.codexworld.com/add-subscriber-to-list-mailchimp-api-php/
I tried to combine these two, however so far without success.
Thanks for the fast reply #scottcwilson, however I get not responds from this php file.
This is how it looks now:
<?php
require_once("wp-load.php");
require_once("wp-content/plugins/membermouse/includes/mm-constants.php");
require_once("wp-content/plugins/membermouse/includes/init.php");
// Your API URL
$apiUrl = "MYDOMAIN/wp-content/plugins/membermouse/api/request.php";
// Your API key
$apiKey = "My API key";
// Your API secret
$apiSecret = "My API secret";
// ---- GET EVENT TYPE ----
if(!isset($_GET["mm_member_add"]))
{
// event type was not found, so exit
exit;
}
// ---- ACCESS DATA ----
// member data
$username = $_GET["username"];
$email = $_GET["email"];
$apiKey1 = 'Mailchimp API';
$listID1 = 'Mailchimp List ID';
// MailChimp API URL
$memberID1 = md5(strtolower($email));
$dataCenter1 = substr($apiKey1,strpos($apiKey1,'-')+1);
$url1 = 'https://' . $dataCenter1 . '.api.mailchimp.com/3.0/lists/' . $listID1 . '/members/';
// member information
$json = json_encode([
'email_address' => $email,
'status' => 'pending',
]);
// send a HTTP POST request with curl
$ch1 = curl_init($url1);
curl_setopt($ch1, CURLOPT_USERPWD, 'api_v3:' . $apiKey1);
curl_setopt($ch1, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_TIMEOUT, 10);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $json);
$result1 = curl_exec($ch1);
$httpCode = curl_getinfo($ch1, CURLINFO_HTTP_CODE);
curl_close($ch1);
break;
echo "<pre>".print_r($result1, true)."</pre>";
?>
That is correct how you said, right?
The issues with your current code are:
1) You are supplying the memberid on the URL - this is not what you want for a POST to create a new user. So do
$url1 = 'https://' . $dataCenter1 . '.api.mailchimp.com/3.0/lists/' . $listID1 . '/members/';
instead.
2) Some of your cURL setup is wrong. For your POST fields you want
curl_setopt($ch1, CURLOPT_USERPWD, 'api_v3:' . $apiKey1);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, true);
get rid of
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);

error :024 in sms sending with smsindiahub

I am using smsindiahub to send sms as below but it is not sending the message,
function send_user_pickup_sms($mobile_number = '', $verification_code = '', $smsemailarray=array(),$date="",$time="") {
if ($mobile_number != '' && $verification_code != '') {
//$customer_name = $userDetails['FirstName'].' '.$userDetails['LastName'];
$message = 'projectname Order ID '.$verification_code.' picked up on '.$date.' at '.$time.': ';
foreach($smsemailarray as $item){
$message .= $item["Qty"].$item["name"].' ';
}
$message = urlencode($message);
//Send Registration code to user via sms
$apiURL = "http://login.smsindiahub.in/vendorsms/pushsms.aspx?user=" . SMSHUB_USERNAME . "&password=" . SMSHUB_PASSWORD . "&msisdn=" . $mobile_number . "&sid=" . SMSHUB_SENDERID . "&msg=" . $message . "&fl=" . SMSHUB_FLAG . "&gwid=" . SMSHUB_GWID;
// Initiate curl
$ch = curl_init();
// Set The Response Format to Json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL, $apiURL);
// Execute
$result = curl_exec($ch);
echo"<pre>";print_R($result);die;
// Closing
curl_close($ch);
return $result;
}
return false;
}
it print a result as below :
{"ErrorCode":"24","ErrorMessage":"Invalid template or template mismatchInvalid template or template mismatch","JobId":null,"MessageData":null}
I am not understand where is fault,can any one help me to resolve it?
Please check it in your smsindiahub panel. Please check the status of your template. Make sure that, your template is approved and not pending.
Thanks

paypal useraction=commit doesnt give a pay button. PHP

I have the following code in php
$postData = "USER=xxxxxx"
. "&PWD=xxxxxxxx"
. "&SIGNATURE=xxxxxxx"
. "&METHOD=SetExpressCheckout"
. "&VERSION=93"
. "&EMAIL=" . $_POST['email']
. "&PAYMENTREQUEST_0_SHIPTOPHONENUM=" . $_POST['phone']
. "&PAYMENTREQUEST_0_AMT=" . $price
. "&PAYMENTREQUEST_0_ITEMAMT=" . $price
. "&PAYMENTREQUEST_0_PAYMENTACTION=SALE"
. "&PAYMENTREQUEST_0_DESC=" . $plan['fullname']
. "&L_PAYMENTREQUEST_0_NAME0=" .$plan['fullname']
. "&L_PAYMENTREQUEST_0_AMT0=$price"
. "&L_PAYMENTREQUEST_0_QTY0=1"
. "&RETURNURL=http://localhost/bemo/casperreg"
. "&CANCELURL=http://bemoacademicconsulting.com/casperprep-internal";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // this line makes it work under https
curl_setopt($ch, CURLOPT_URL, "https://api-3t.paypal.com/nvp");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
// Ready the postData to send
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
// Send the data to Paypal and assign the repsonse
$resp = curl_exec($ch);
parse_str($resp,$arr);
redirect::to("https://www.paypal.com/checkoutnow?token=" . $arr['TOKEN'] . "&useraction=commit");
Now technically everything here works except for the fact that even though it says useraction=commit I don't get a pay now button. Can anyone help me please?
Also as a bonus question, even though I pass the phone number to paypal, I don't see it on the paypal page. Is this something I can do?
Thanks!
Nur
The correct redirect address for express checkout should be
https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=commit&token=[token number]
Source: Developer Guide
As to your bonus question
1. Try doing a field mask on number xxx-xxx-xxxx
2. Check if phone number is set to required in your merchant profile

role name in embedded sending docusign

I am using the docusign rest api. I am trying to create a template and then use embedded sending.
Here is my code:
Create a template:
$header = "<DocuSignCredentials><Username>" . $email . "</Username><Password>" . $password . "</Password><IntegratorKey>" . $integratorKey . "</IntegratorKey></DocuSignCredentials>";
$url = "https://demo.docusign.net/restapi/v2/login_information";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-DocuSign-Authentication: $header"));
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 200 ) {
$status = 'notok';
}
$response = json_decode($json_response, true);
if( (isset($response['errorCode'])) && ($response['errorCode'] == 'USER_LACKS_PERMISSIONS')) {
echo $msg = 'This user lacks sufficient permissions';
$_SESSION['msg_frm_apd'] = $msg;
}
$accountId = $response["loginAccounts"][0]["accountId"];
$baseUrl = $response["loginAccounts"][0]["baseUrl"];
curl_close($curl);
$template_name = "template_" . time();
$data = "{
\"emailBlurb\":\"String content\",
\"emailSubject\":\"String content\",
\"documents\": [{
\"documentId\": \"1\",
\"name\": \"document.pdf\"
}],
\"recipients\": {
\"signers\": [{
\"recipientId\": \"1\",
\"roleName\": \"Signer 1\"
}]
},
\"envelopeTemplateDefinition\": {
\"description\": \"Description\",
\"name\": \"$template_name\"
}
}";
$file_contents = file_get_contents("uploads/envelopes/" . $file_name);
$requestBody = "\r\n"
."\r\n"
."--myboundary\r\n"
."Content-Type: application/json\r\n"
."Content-Disposition: form-data\r\n"
."\r\n"
."$data\r\n"
."--myboundary\r\n"
."Content-Type:application/pdf\r\n"
."Content-Disposition: file; filename=\ā€¯document.pdf\"; documentid=1 \r\n"
."\r\n"
."$file_contents\r\n"
."--myboundary--\r\n"
."\r\n";
$url = "https://demo.docusign.net/restapi/v2/accounts/376082/templates";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data;boundary=myboundary',
'Content-Length: ' . strlen($requestBody),
"X-DocuSign-Authentication: $header" )
);
$json_response = curl_exec($curl);
$response = json_decode($json_response, true);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
print_r($json_response); echo "\n";
exit(-1);
}
$response = json_decode($json_response, true);
Embedded Sending:
$templateId = $response['templateId']; // provide a valid templateId of a template in your account
$clientUserId = "1234";
$templateRoleName = "Signer 1";
$data = array("accountId" => $accountId,
"emailSubject" => "DocuSign API - Embedded Sending Example",
"templateId" => $templateId,
"templateRoles" => array(
array( "roleName" => $templateRoleName, "email" => $recipient_email, "name" => $recipient_name, "clientUserId" => $clientUserId )),
"status" => "created");
$data_string = json_encode($data);
$curl = curl_init($baseUrl . "/envelopes" );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
"X-DocuSign-Authentication: $header" ) docusign
);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
print_r($json_response); echo "\n";
exit(-1);
}
$response = json_decode($json_response, true);
$envelopeId = $response["envelopeId"];
curl_close($curl);
//--- display results
// echo "Envelope created! Envelope ID: " . $envelopeId . "\n";
/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 3 - Get the Embedded Sending View (aka the "tag-and-send" view)
/////////////////////////////////////////////////////////////////////////////////////////////////
/*$data = array("returnUrl" => "http://www.docusign.com/devcenter");*/
$returnUrl = $SITE_URL . "/docusign_return.php";
$data = array("returnUrl" => $returnUrl);
$data_string = json_encode($data);
$curl = curl_init($baseUrl . "/envelopes/$envelopeId/views/sender" );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
"X-DocuSign-Authentication: $header" )
);
$json_response = curl_exec($curl);
$response = json_decode($json_response, true);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
print_r($json_response); echo "\n";
exit(-1);
}
$url = urlencode($response["url"]);
When i click on the above url, i am getting a page where i can tag and send the document. There is an email address
field in this page which is automatically populated with the $recipient_email. But the problem is that the mail is not going to the recipient email address. Thanks.(also i am not getting any error messages).
There is no problem with your code and it is working perfectly as expected. I can tell that you are using DocuSign's sample code from their API Walkthroughs.
If you're saying everything is working ok and no errors, but that after navigating to the embedded sending URL and sending the document for signature the recipient is not receiving the email I would check for things like security software, spam/junk mail filters, firewalls, etc on your side. The DocuSign service is so widely used that if the emails weren't going out in signature requests there would be a big stink about it pretty quickly, and on top of that I just did a test and my emails are going out just fine.
So as mentioned, if you're sure the email address is correct and you are hitting the SEND button through the UI, I'd check security software, spam/junk mail filters, firewalls, and anything else that might stop the email from arriving on your side.
Another suggestion for troubleshooting issues with email delivery: use DocuSign Connect. If your account is configured properly, you could enable Connect and use the Connect logs to detect if the recipient email is undeliverable (i.e., if DocuSign's receiving a 'bounce-back' when sending to that address).
Create a Custom Connect Configuration in DocuSign, as described in this guide: http://www.docusign.com/sites/default/files/DocuSign_Connect_Service_Guide.pdf.
In the Connect Configuration that you create, you can specify any URL as the URL to publish to -- all you're trying to do for this test is to make Connect send the notification for the "Recipient Sent" event and "Recipient Delivery Failed" event (and create Log entries for the notifications it sends) -- it doesn't matter that the endpoint you specify won't be equipped to receive/process the Connect message.
In the Connect Configuration that you create, be sure to select the options I've highlighted here (as previously stated, you can use any URL -- I just happened to have chosen google.com):
Once you've created and saved the Connect Configuration as described above, go through the process you describe in your question to create/send the Envelope.
Once you've sent the Envelope, login to the DocuSign web console (as Administrator), and view the Connect log entries. (i.e., navigate to Preferences >> Connect >> Logs
You should see log entries listed for each "Recipient Sent" and/or "Recipient Delivery Failed" event that's occurred since the time that you created/enabled the Connect Configuration.
Examining the contents of the log entries should allow you to determine if DocuSign is successfully sending the Recipient email(s), and also whether the Recipient email(s) are bouncing back as undeliverable.
If the log entries indicate that DocuSign is sending the Recipient email(s) and do NOT indicate any bounce-backs, then it's likely an issue with the Email client that's preventing the recipient from receiving the email(s) (as Ergin suggested in his answer).

CURL - No response and no error

I'm hoping someone could help me as I'm getting no response, no error, and no indication of why my CURL is not working here. I've searched around and added a clause to ignore the SSL (SSLVerify is set to false) and tried numerous ways to get a response.
May someone please point me in the right direction?
Thanks!
<?php
function sendContactInfo() {
//Process a new form submission in HubSpot in order to create a new Contact.
$hubspotutk = $_COOKIE['hubspotutk']; //grab the cookie from the visitors browser.
$ip_addr = $_SERVER['REMOTE_ADDR']; //IP address too.
$hs_context = array(
'hutk' => $hubspotutk,
'ipAddress' => $ip_addr,
'pageUrl' => 'https://www.myfoodstorage.com/onestepcheckout/',
'pageTitle' => 'MyFoodStorage.com Cart Checkout'
);
$hs_context_json = json_encode($hs_context);
//Need to populate these varilables with values from the form.
$str_post = "firstname=" . urlencode($firstname)
. "&lastname=" . urlencode($lastname)
. "&email=" . urlencode($email)
. "&phone=" . urlencode($telephone)
. "&address=" . urlencode($street)
. "&city=" . urlencode($city)
. "&state=" . urlencode($region)
. "&country=" . urlencode($country)
. "&hs_context=" . urlencode($hs_context_json); //Leave this one be :)
//replace the values in this URL with your portal ID and your form GUID
$endpoint = 'https://forms.hubspot.com/uploads/form/v2/234423/4a282b6b-2ae2-4908-bc82-b89874f4e8ed';
$ch = #curl_init();
#curl_setopt($ch, CURLOPT_POST, true);
#curl_setopt($ch, CURLOPT_POSTFIELDS, $str_post);
#curl_setopt($ch, CURLOPT_URL, $endpoint);
#curl_setopt($ch, CURLOPT_HTTPHEADER, array('application/x-www-form-urlencoded'));
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
#curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = #curl_exec($ch);
$info = #curl_getinfo($ch);
#curl_close($ch);
}
echo sendContactInfo();
echo $response;
print_r($info);
?>
1.you can't print variable value defined inside a function outside of function, like this:
function sendContactInfo() {
$response = #curl_exec($ch);
$info = #curl_getinfo($ch);
}
echo $response;
print_r($info);
but you can print value so:
function sendContactInfo() {
$response = #curl_exec($ch);
$info = #curl_getinfo($ch);
echo $response;
print_r($info);
}
sendContactInfo();
2.when you want to run function and get after value, use "return", like this:
function sendContactInfo() {
$response = #curl_exec($ch);
$info = #curl_getinfo($ch);
return $response;
//or return $info;, when you want to get array values from #curl_getinfo
}
print_r(sendContactInfo());
sendContactInfo is a function but its not being used like one.
You can't access variables that are used inside the function. Also it needs to return something
Change:
$response = #curl_exec($ch);
to
return #curl_exec($ch);

Categories