Monday, 6 July 2015

Android push notification - Send notification to google when message received


Android push notification api in PHP


Question: What is Push Notification in Android?
Push notifications let your application notify a user of new messages or events even when the user is Offline.


Question: How to get to know Push Notification in Android?
In Android devices, when a device receives a push notification, your application's icon and a message appear in the status bar.


Question: What are basic steps to implements push notifiction in Android Application?
  1. Registering for the Push Service
  2. Application must have the permissions to receive pushes and show notifications.
  3. Choose Your Push Icon
  4. Add your Parse API keys Parse.initialize(this, "YOUR_APP_ID", "YOUR_CLIENT_KEY");
  5. Enable Push Notifications in Mobile


PHP script for send notification to google services when event/message received so that notification start showing on user device.
 // Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$registrationIDs = array('Mobile Device Id1','Mobile Device Id3','Mobile Device Id3');
$message='message_received';
$apiKey='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$fields = array(
    'registration_ids' => $registrationIDs,
    'data' => array( "message" => $message ),
);
$headers = array(
    'Authorization: key=' . $apiKey,
    'Content-Type: application/json'
);

// Open connection
$ch = curl_init();

// Set the URL, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);


Friday, 3 July 2015

Braintree hosted fields javascript php send nonce in ajax

Hosted Fields of Braintree are small, transparent iframes that replace the sensitive credit card inputs in your checkout flow, It helps you meet the latest data security requirements.

In this you need not to take the PCI Compliance as credit card details are in Braintree and process by Braintree. User will feel he is adding credit card details in your website and in actual its NOT.

Braintree hosted fields javascript php send nonce in ajax


After doing couples of days hard work, I am able to integrate the braintree hosted fields.
Following are HTML Form
<form action="/braintree/submitform" id="my-payment-form" method="GET">
<div class="braintreeinput">
<label for="card-number">Card Number</label>
      <br />
<div id="card-number">
</div>
</div>
<div class="braintreeinput">
<label for="cvv">CVV</label>
      <br />
<div id="cvv">
</div>
</div>
<div class="braintreeinput">
<label for="expiration-month">Expiration Month</label>
      <br />
<div id="expiration-month">
</div>
</div>
<div class="braintreeinput">
<label for="expiration-year">Expiration Year</label>
      <br />
<div id="expiration-year">
</div>
</div>
<div class="payBtnCont">
<input id="mainSubmitBtn" type="submit" value="Send Money" />
    </div>
</form>



Add Following javascript in web page
http://js.braintreegateway.com/js/beta/braintree-hosted-fields-beta.17.js



Get the Client Token from Server and set in javascript variable as below:
var clientToken = 'CLIENT_TOKEN_FROM_SERVER';



Following are javascript code:
      braintree.setup(clientToken, "custom", {
        id: "my-payment-form",
        hostedFields: {
          number: {
            selector: "#card-number"
          },
          cvv: {
            selector: "#cvv"
          },
          expirationMonth: {
            selector: "#expiration-month"
          },
          expirationYear: {
            selector: "#expiration-year"
          },
          styles:{
            ".braintreeinput": {
            "font-size": "16pt",
            "color": "#3A3A3A",
            "line-height" : "40px"
          }  
          }
        },onPaymentMethodReceived:function(nonce){
             console.log(JSON.stringify(response));
            /*Response:
{"nonce":"e1db0653-7a83-4022-8213-7e73504e7f88","details":{"lastTwo":"11","cardType":"Visa"},"type":"CreditCard"}*/


            $.ajax({
                url:'/braintree/submitform',
                data:'payment_method_nonce='+response.nonce,
                type:'POST',
                dataType:'json',
                success: function(response) {
                    //Ajax call results comes here
                }                
            });

            return false;
          
      }
        }
      
              ); 



Once you get the nonce in API call "/braintree/submitform", you can charge from customer using below code.
$result = Braintree_Transaction::sale([
  'amount' => '10.00',
  'paymentMethodNonce' => nonceFromTheClient,
  'options' => [
    'submitForSettlement' => True
  ]
]);