Integrate API

Integrate API - Header

Friday, August 30, 2013

How to integrate CM.NL SMS API

http://www.cmtelecom.com is one of the largest worldwide SMS provider company who provide two different services named BUSINESS and ELITE. Depending upon your SMS volume choose right package for you. Here is how those two services you will integrate with your own application.

From another view, CM.NL provide two different types of Test Account(may be same for Live)
1. SMPP: http://docs.cm.nl/SMPP.pdf
2. HTTP: http://docs.cm.nl/http_MT.pdf
3. CM Direct: http://docs.cm.nl/Technische-Documentatie-CMDirect.pdf (Detail)

CM Direct:
First of all go to: here and create an account with your email address, company name and phone number. Once you have successfully create your account they will send you a verification email link. after verify your email(they use mobile number verification as well) please log in to system using email address and phone number. every time when you attempt to log in they will send you a verification code to your phone number you have used during registration. After log in, you can see your current credit middle top right corner like Current credit: 74 Messages (Updated to Fri 30 Aug 02:03). Usually cmtelecom provide 100 free SMS credit for test.
Now here is the straight forward and easy PHP coding to integrate CM.NL with your application.
<?php 
class CMSMS 

  function CreateMessage($ProductToken, $Sender, $Recipient, $Tariff, $Body) 
  { 
$XMLSMS = new SimpleXMLElement('<MESSAGES/>'); 
$XMLSMS->addChild('AUTHENTICATION'); 
$XMLSMS->AUTHENTICATION->addChild('PRODUCTTOKEN', $ProductToken); 
$XMLSMS->addChild('TARIFF'); 
$XMLSMS->TARIFF = $Tariff; 
$XMLSMS->addChild('MSG'); 
$XMLSMS->MSG->addChild('FROM'); 
$XMLSMS->MSG->FROM = $Sender; 
$XMLSMS->MSG->addChild('BODY'); 
$XMLSMS->MSG->BODY = $Body; 
$XMLSMS->MSG->addChild('TO'); 
$XMLSMS->MSG->TO = $Recipient; 
return $XMLSMS->asXML(); 
  }
  function SendMessage($URL, $Message) 
  { 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $URL); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml', 'Content-length: ' . strlen($Message))); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $Message); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$return = curl_exec($ch); 
curl_close($ch);
return $return; 
  } 

//Send SMS
$SMS = new CMSMS; 
$ProductToken=$_POST['product_token'];//token you get in email
$Tariff=0; $Sender="123456789"; //your no
$Recipient="123456789"; $Body="Test SMS using CM.NL direct";
$XMLtoSend = $SMS->CreateMessage($ProductToken, $Sender, $Recipient, $Tariff, $Body); 
$return=$SMS->SendMessage('http://gateway.cmdirect.nl/cmdirect/gateway.ashx', $XMLtoSend); 
echo $return;
?>


HTTP:
send-sms-start.php:-
<form method="post" action="send_sms.php"> You can use BUSINESS or ELITE Credential to Send SMS.<br /> Customer ID: <input name="customer_id" type="text" value="1000"><br/> Username: <input name="username" type="text" value="your-user-id"><br/> Password: <input name="password" type="text" value="pass"><br/> <br /> Sender No: <input name="sender_no" type="text" value=""><br/> Recipient No: <input type="text" name="recipient_no" value=""> <br/> SMS Text: <input type="text" name="sms_body"> <input type="submit" value="Send SMS"> </form>

send_sms.php
// Report all PHP errors (see changelog) error_reporting(E_ALL); include("cmSMS.php"); //Create SMS Object $SMS = new CMSMS; $CustomerID=$_POST['customer_id']; $Login=$_POST['username']; $Password=$_POST['password']; $Tariff=0; $SenderName=$_POST['sender_no']; $Body=$_POST['sms_body']; $MSISDN=$_POST['recipient_no']; //Prepare SMS $Reference="member_id=420&Time=".time(); //Any custom Reference/Text. Helpful to identify SMS $XMLtoSend = $SMS->CreateMessage($CustomerID, $Login, $Password, $Tariff, $SenderName, $Body, $MSISDN, $Reference); //Send SMS $respnose=$SMS->SendMessage('http://smsgateway02.cm.nl/cm/gateway.ashx',$XMLtoSend); if($respnose=="") echo "SMS Sent Successfully"; else echo $respnose;

cmSMS.php: 
class CMSMS 

  function CreateMessage($CustomerID, $Login, $Password, $Tariff, $SenderName, $Body, $MSISDN, $Reference="")
  {  
    $MSISDN=str_replace("+","",$MSISDN);//remove begining plus sign
    if(substr($MSISDN,0,2)!="00")
    $MSISDN="00".$MSISDN;
    $XMLSMS = new SimpleXMLElement('<MESSAGES/>'); 
    $XMLSMS->addAttribute('PID',25); 
     
    $XMLSMS->addChild('CUSTOMER'); 
    $XMLSMS->CUSTOMER->addAttribute('ID',$CustomerID); 
     
    $XMLSMS->addChild('USER'); 
    $XMLSMS->USER->addAttribute('LOGIN',$Login); 
    $XMLSMS->USER->addAttribute('PASSWORD',$Password); 

//Helpful for assign cusotm SMS ID
if($Reference!="")
{
      $XMLSMS->addChild('REFERENCE');
      $XMLSMS->REFERENCE = $Reference;
}
     
    $XMLSMS->addChild('TARIFF'); 
    $XMLSMS->TARIFF = $Tariff; 
     
    $XMLSMS->addChild('MSG'); 
     
    $XMLSMS->MSG->addChild('FROM'); 
    $XMLSMS->MSG->FROM = $SenderName; 
     
    $XMLSMS->MSG->addChild('BODY'); 
    $XMLSMS->MSG->BODY = $Body; 
     
    $XMLSMS->MSG->addChild('TO'); 
    $XMLSMS->MSG->TO = $MSISDN; 

//Multipart SMS.Recipient will get more then 160 char at once
$no_of_sms_credit_required=ceil(strlen($Body)/160.00);
if($no_of_sms_credit_required>1)
    {
      $XMLSMS->MSG->addChild('MINIMUMNUMBEROFMESSAGEPARTS');
      $XMLSMS->MSG->MINIMUMNUMBEROFMESSAGEPARTS = 1;

      $XMLSMS->MSG->addChild('MAXIMUMNUMBEROFMESSAGEPARTS'); 
      $XMLSMS->MSG->MAXIMUMNUMBEROFMESSAGEPARTS = $no_of_sms_credit_required;

      $XMLSMS->MSG->addChild('TARIFFPERMESSAGEPART');
      $XMLSMS->MSG->TARIFFPERMESSAGEPART = $Tariff;
}

    return $XMLSMS->asXML(); 
  } 
   
  function SendMessage($URL, $Message) 
  { 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $URL); 
    #curL_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml', 'Content-length: '.strlen($Message))); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $Message); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $return = curl_exec($ch); 
    curl_close($ch);     
    return $return; 
  } 
}

Concatenated SMS:
A single SMS contain maximum 160 character but sometimes your SMS might longer then that and your recipient love to receive whole SMS at once(in a single SMS). In order to do it, you have to use additional parameter MINIMUMNUMBEROFMESSAGEPARTS,  MAXIMUMNUMBEROFMESSAGEPARTS & TARIFFPERMESSAGEPART in SMS XML preparation.

FAQ:
Where from shall i get ProductToken of CM Direct?
=> You will receive a Product Token by mail after creating an account.

For HTTP method where from  shall i get Customer ID, username and password?
=> CM.NL will create these for you.

What response do i receive for HTTP?
=> Every request will get an HTTP response with status 200 (OK), even if the request is malformed. If the request was correct, the response will be empty. If the request was malformed, the response will start with “ERROR”. So it's easy for you to check a SMS sent or not by $response=="" (success)

How to pass a custom text(specially needed for SMS ID) which will back to me while Delivery Receipt?
=> You can pass custom text using REFERENCE field.

For any further enquiry please feel free to post your comments.

Monday, August 12, 2013

Integrate Plivo.com SMS API

Plivo.com have little bit more restriction and a bit complex to integrate rather then other similar sites. Moreover, PHP PEAR library must be installed on your server. Once everything is ready you need to download plivo.php file from their site(http://plivo.com/docs/helpers/php/). Now in short and quick example, use the following code to send SMS.

<?php
require_once 'HTTP/Request2.php';
require_once 'plivo.php';

$params = array(
'src' => 1202123456', //sender id, which must bought from plivo.com
'dst' => '190909090',
'text' => 'test SMS from plivo',
'type' => 'sms',
);
$response = $p->send_message($params);
print_r($response);
echo "Script End";
?>

Most important factor here is, you need to 

For developer documentation here is the link: http://plivo.com/blog/get-started-with-your-free-developer-account/

Thursday, August 1, 2013

How to integrate Paypal API in your site

Paypal have changed their site features in recent days. They have now more usable online payment solutions. As though their outlook and functionality have been changed, i am going to write a short summery of  how to integrate paypal in your site.

First of all you need a paypal developer account(for test transaction) and live account(for real transaction).
  1. Go to http://developer.paypal.com and register here.
  2. Now log in developer.paypal.com
  3. After log in click "Dashboard" tab. Here you will get 02 options (Test and Live).
  4. Click Test->REST API transactions. you get "Test REST API transactions and responses". Click on this link "Create your first PayPal app and receive API credentials."
  5. You are now at "My appsREST API apps where from you can Create, edit and manage multiple PayPal apps. In each app, select the specific PayPal capabilities you want to offer to your customers. Every app gets a unique set of live and test API credentials. 
  6. You get there a button labeled "Create Application" click on it. Give Application name and select integration type(web/mobile app). Finally click "Create Application"
  7. Your application is immediately approved and ready for Accepts PayPal payments. From this page you can get application credentials by clicking "REST API CREDENTIALS". You get credentials like: 
Test credentials
Test account: yourname-facilitator@gmail.com
Endpoint: api.sandbox.paypal.com
Client ID: AWXUjxAgAxTmMjjv9yhNXZERa_zAxz4kWlB1CRMgTWUYtaVop5pQyi9uCy
Secret: EK9ZzRD2ciHJhk_GBLdaR7BP7PVSEX-tU0nyaVMJxIGDeylvVjP0uRamDvyp
Live credentials
Endpoint: api.paypal.com
Client ID: Aey2xBfKiEZWrk8Fgahd6Cw4KXL0Mu7eEsC6Pdxj45oaNhOPsbFLMSFo_tu
Secret: ENE0iBCsfgeWaUCK2ghzUCeK7afuIslW1XoGVi93gn6pjSsx-UUoCmqEwTWD
 
After create "My Application" successfully you get "Sandbox accounts" in left menu under Applications. Click on "Sandbox accounts". Here you get your business account. 
Click on "Profile" link under your account title. "Account Details" popup window will appear. From there "API Credential" tab you have to collect 3 things
API Username: yourname-facilitator_api1.gmail.com
Password: 1375222646
Signature: AFcWxV21CxE7Ev3bYYYRCpSSRl31AeHPKoPJmkyNgPh36GMZEZxToxth

Well, we have created merchant(seller) account successfully. Now we need to create some buyer account so that we can test buyer experience.
After log in developer.paypal.com click "Application" main tab on the top.
Click "Applications"->Sandbox accounts. Click "Create account" button (right middle corner).
Provide Account Detail information. For example:
Country: United States
Account Type: Personal(buyer account) or Business(seller account). i select here Personal
Email address: your-fake-email@something.com
Password: anypass
Payment Methods: Paypal balance Put here any amount say 5000USD
Bank verified account:  Yes
Select payment card: Discover
Credit card type: American Express
Lastly click "Create Account" button.

So you have created a buyer account for you. Now you can check your Applications and Sandbox Accounts by click on "Application" link on header after log in developer.paypal.com

How to activate your credit card?
=> After log in developer.paypal.com click "Application" -> "Sendbox accounts" -> Profile(under test account) -> Funding tab -> 'Paypal Payment Card' section - Card Status. Click here to activate your credit card.

How do i check whether i receive a payment from a buyer to my business account?
=> After log in developer.paypal.com click "Application" -> "Sendbox accounts" -> Click "Notifications" (under business account)-> You get 'Notification of Payment Received" with date. click on it to show detail of this transaction.

Shall i need to log in developer.paypal.com during test transactions?
=> No you don't need that. Before last upgrade of paypal it was mandatory.

How to change my Store Name in Sandbox/Test account?
=> log in sandbox.paypal.com using merchant account detail created at developer.paypal.com. Click "My Account" tab(first tab) of header menu. Click "Profile" tab which is right most tab appear in sub menu. Now Click "My Business Info" in left menu. First item her is name and click "Change" link (appear right to name). Select "Change Business Name" and press next. Enter here new business name and click "Continue". You are done.

What is developer.paypal.com?
=> Developer.paypal.com is a place where from you will be able to create test accounts(buyer & seller), get account API details, view their summery transactions. Moreover, you can create New Test Applications from here. In addition, you can even change buyer & sellers account password.

What is sandbox.paypal.com?
=> sandbox.paypal.com is purely a Test Platform where from you will be able to log in using account details(email and password) created at developer.paypal.com. Moreover, you can get detail transaction history of buyer/seller account.

Where from i get Paypal Live account API Credentials(Username, Password & Signature)?
=> Go to https://www.paypal.com/us/cgi-bin/webscr?cmd=_login-api-run and log in using your Live account userid and password. Immediately after you logged in, will get API username, Password and Signature.

Error Message: Could not resolve host: api-3t.paypal.com; Host not found
=> Check correctness of your Live account API Credentials(username, password & signature). In order to get correct credential follow former error & solution.

How to create a test account in Sandbox?
=> After login developer.paypal.com click Application. In left menu under Applications you will find out Sandbox accounts(click on it). Now click "Create Account" button on the right. Provide detail information here and click "Create Account". Keep in mind, in order to test buyer experience you have to set Account Type to Personal.

Where from i can check my Buyer Account Transactions & Payments?
=> Go to sandbox.paypal.com which is purely a Test Platform and from here you will be able to log in using test account details(email and password) created at developer.paypal.com. Moreover, you can get detail transaction as well as test account information(like first & last name, country etc) history of buyer/seller account.

What is Guest Checkout?
=> There are two ways of payment receive in paypal. First payment is not possible without having any paypal account and Second, pay using Credit Card (no need any paypal account). Guest checkout means second one.

How to enable Guest Checkout in Paypal Checkout
=> In your NVPSTR set &SOLUTIONTYPE=Sole to enable Guest Checkout. And optionally specify LANDINGPAGE=Billing for the billing page to be forced. If guest checkout is enabled then visitors will be able to pay using only their credit card (without having paypal account).
Moreover, in order to enable Guest Checkout you need to setup few parameter in your paypal merchant account as well. You must have 'PayPal Account Optional' set to 'On' with 'Website Preferences' section of PayPal account.
Profile > My selling tools (or: My selling preferences) > Website Preferences > Update > PayPal Account Optional: On.

How to turn off Paypal Shipping Address in Checkout
=> For sale digital goods and local pickup we don't need any shipping address. so we need not to display shipping address in paypal. In NVPSTR:
NOSHIPPING=1 leads buyer not asked for a shipping address in paypal.
Error & Solution:
The totals of the cart item amounts do not match order amounts
=> Please check your total amount(AMT) should be equal to sum of all item/product total amount (ITEMAMT) and Tax amount(TAXAMT) and shipping amount(SHIPPINGAMT) and subtract by shipping discount amount(SHIPDISCAMT).

Security header is not valid
=>Check your API_ENDPOINT. For sandbox it should be https://api-3t.sandbox.paypal.com/nvp and for live https://api-3t.paypal.com/nvp

Error Message: Unknown SSL protocol error in connection to api-3t.paypal.com:443
=> This error comes in localhost because of SSL not supported in localhost. In server you will not get this error.

This transaction has expired. Please return to the recipient's website to complete your transaction using their regular checkout flow.
=> Check your PAYPAL_URL. For Sandbox it's https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token= and for Live it's https://www.paypal.com/webscr&cmd=_express-checkout&token=
     If you still getting problem please check API_ENDPOINT as well. For sandbox it should be https://api-3t.sandbox.paypal.com/nvp and for live https://api-3t.paypal.com/nvp