Skip to content

Instantly share code, notes, and snippets.

@wupsbr
Created September 16, 2010 20:37
Show Gist options
  • Save wupsbr/583118 to your computer and use it in GitHub Desktop.
Save wupsbr/583118 to your computer and use it in GitHub Desktop.
//imports para funcionar a integração com a PDV
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.io.DataOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import org.json.JSONException;
import org.json.JSONObject;
//classe para ser adicionada ao Activity principal
/** Send an SMS using "Plataforma de Desenvolvedores Vivo"
* If no requestor_id is provided, the MSISDN of the terminal is assumed */
public String vivoSMS(String serviceId, String spId, String spPassword, String phone, String message) {
TelephonyManager tm = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String requestor_id = tm.getLine1Number();
String token = "SEU_TOKEN_NO_CADASTRO";
return vivoSMS(serviceId, spId, spPassword, phone, message, requestor_id, token);
}
/** Envia SMS pela "Plataforma de Desenvolvedores Vivo" */
public String vivoSMS(String serviceId, String spId, String spPassword, String phone, String message, String requestor_id, String token) {
final String urlSms = "https://sdp.vivo.com.br/osg/UNICA-SMS-REST/SMS";
// Obtem timestamp
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
df.setTimeZone(TimeZone.getTimeZone("GMT-3"));
Date now = new Date();
String timestamp = df.format(now);
StringBuffer auth = new StringBuffer();
auth.append("SDPBasicAuth realm=\"SDPAPIs\", ");
auth.append("consumer_key=\"" + serviceId + "@" + spId + "\", ");
auth.append("signature_method=\"MD5\", ");
auth.append("signature=\"" + md5(spId + spPassword + timestamp) + "\", ");
auth.append("timestamp=\"" + timestamp + "\", ");
auth.append("version=\"0.1\", ");
auth.append("token=\"" + token + "\", ");
auth.append("requestor_id=\"" + requestor_id + "\", ");
auth.append("requestor_type=\"1\"");
JSONObject json = new JSONObject();
JSONObject jsonInner = new JSONObject();
JSONObject jsonPhone = new JSONObject();
try {
json.put("smsText", jsonInner);
jsonInner.put("address", jsonPhone);
jsonPhone.put("phoneNumber", phone);
jsonInner.put("message", message);
} catch (JSONException e) {
e.printStackTrace();
}
String body = json.toString();
String code = "";
try {
URL url = new URL(urlSms);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", auth.toString());
DataOutputStream os = new DataOutputStream (conn.getOutputStream());
os.writeBytes(body);
os.flush();
code = conn.getResponseCode() + " - " + conn.getResponseMessage();
String location = conn.getHeaderField("Location");
Toast.makeText(getApplicationContext(), code + " - " + location, Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.d("SMS",e.getMessage());
}
return code;
}
/** Gera um hash MD5 para a string fornecida */
public String md5(String s) {
try {
// Create MD5 Hash
MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
digest.update(s.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexString = new StringBuffer();
for (int i=0; i<messageDigest.length; i++) {
String h = Integer.toHexString(0xFF & messageDigest[i]);
while (h.length()<2) h = "0" + h;
hexString.append(h);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment