Get the cost of SMS after SMS has delivered using Twilio - php

I am working on a project where under certain events it will send an SMS message.
What I was planning was when the SMS was delivered, I would keep a record, both for debugging purposes if required, and to keep a history of what's happening with my service. One of the things I was planning on logging was the cost of the message being delivered. It looked like from the Twilio documentation that this is possible, except when I send the message, the response I get back is status queued and price is null.
I then looked at using the Twilio webhook call back so that I receive an update on the status of the SMS delivery, which also works fine exception I don't get the price I only receive the following:
(
[SmsSid] => redacted
[SmsStatus] => delivered
[MessageStatus] => delivered
[To] => redacted
[MessageSid] => redacted
[AccountSid] => redacted
[From] => redacted
[ApiVersion] => 2010-04-01
)
For reference below is how I am sending the SMS
$twilio = new \Twilio\Rest\Client($sid, $token);
$message = $twilio->messages->create("redacted", array(
"body" => $smsContent,
"from" => "redacted",
"statusCallback" => $callback
));
How can I get the price of the SMS delivery from Twilio?

I've figured a way but it seems a little inefficient as I have to send another request to Twilio to get the full status of the SMS but this is how I've done.
I receive a Twilio request via webhook and I then fetch the message the message using the SmsId that is posted like the following:
$smssid = $_POST["SmsSid"];
$twilio = new \Twilio\Rest\Client($sid, $token);
$message = $twilio->messages($smssid)->fetch();
$twilioResponse = new TwilioResponse($message);
FYI TwilioResponse is my own class that has the $message passed in a constructor and I then use it to build and parse my own version for my needs.

Related

When using twilio's php notify API, how do you set your callback URL?

I have the following code and it sends SMS notifications to my phone:
$notification = $twilio->notify->services($serviceSid)
->notifications->create([
'toBinding' => $batch,
'body' => $txt,
'statusCallback' => 'http://postb.in/b/jarblegarble' // <-- this doesn't work
]);
However, even though the sending works, I can't seem to figure out their callbacks.
I'm scouring through their docs and I can't find how to set the callback URL. I see some of their resources use "url" while others use "statusCallback" (heck, one seems to use "redirect"). That being said, I can't seem to post to postb.in using them -- there must be a way to check the status of my notification.
So it turns out I was wrong on two fronts.
1) The callback URL needs to be passed to your messaging service this way:
$notification = $twilio->notify->services($serviceSid)
->notifications->create([
'toBinding' => $bindings,
'body' => $txt,
'sms' => ['status_callback' => 'http://your_callback_url' ]
]);
2) postb.in wasn't working! I was testing the code above, after being assured by twilio support that it was valid, I decided to try and post to my own server and just capture the POSTed content. Sure enough, it was working as they suggested.
Edit: It wasn't clear to me at the time but the callback URL will be called for each SMS sent out for each status update. So that means queued, sent, and delivered. I initially thought that I'd just get a status update for the batch itself as I don't necessarily care for the status of up to 10,000 txt messages.
Your example passes the statusCallback parameter of the individual SMS service API to the universal notify API. This mixing won't work. The individual SMS service sets up a callback for that one particular message, which isn't efficient for batch sends. The universal notify API, in contrast, relies on web hooks, which are globally configured per service.
The simplest thing to do, in your case, is to use the individual SMS service API:
$message = $twilio->messages->create('+15551234567', [ 'body' => 'Hi',
'from' => '+15559876543',
'statusCallback' => 'http://postb.in/b/jarblegarble' ]);
To use the universal notify API, you'll need to set the PostWebhookUrl to the target URL when creating the notification service, and arrange for the code at that URL to handle onMessageSent messages. More at the "web hooks" URL above.
Caveat emptor: haven't tried any of this, and I haven't used Twilio in literally eight years, but the above is my theoretical understanding.

PayPal IPN listener script never stops executing

I have a PHP web page which uses the creates an invoice and allows the user to pay via PayPal. The script identifies the IPN listening page via https and that gets called once the user has completed their transaction. Inside the IPN script that is called, I am updating the invoice status to "paid" and other relevant information upon a successful payment. The good news is that all functions perfectly. However, when I add a single payment inside the routine, to a vendor that gets a percent of the sale, the script executes over and over and over and never stops. The part of the script that seems to be causing the trouble is
case "Completed":
$vBody = "PayPal has identifed this transaction as Completed. Your account will be credited.";
// BEGIN MassPay API Call
// Create PayPal object.
$PayPalConfig = array(
'Sandbox' => $sandbox,
'APIUsername' => $api_username,
'APIPassword' => $api_password,
'APISignature' => $api_signature,
'PrintHeaders' => $print_headers,
'LogResults' => $log_results,
'LogPath' => $log_path,
);
$PayPal = new angelleye\PayPal\PayPal($PayPalConfig);
// Prepare request arrays
$MPFields = array(
'emailsubject' => 'Notification of Payment', // The subject line of the email that PayPal sends when the transaction is completed. Same for all recipients. 255 char max.
'currencycode' => 'USD', // Three-letter currency code.
'receivertype' => 'EmailAddress' // Indicates how you identify the recipients of payments in this call to MassPay. Must be EmailAddress or UserID
);
// Build MPItems array.
$Item1 = array(
'l_email' => 'recipient#gmail.com', // Required. Email address of recipient. You must specify either L_EMAIL or L_RECEIVERID but you must not mix the two.
'l_receiverid' => '', // Required. ReceiverID of recipient. Must specify this or email address, but not both.
'l_amt' => '3.00', // Required. Payment amount.
'l_uniqueid' => $Post["invoice"], // Transaction-specific ID number for tracking in an accounting system.
'l_note' => 'Payment per PayPal transaction processing fee.' // Custom note for each recipient.
);
$MPItems = array($Item1);
//$MPItems = $Item1; // ($Item1, $Item2, etc
$PayPalRequestData = array('MPFields'=>$MPFields, 'MPItems' => $MPItems);
// Pass data into class for processing with PayPal and load the response array into $PayPalResult
$PayPalResult = $PayPal->MassPay($PayPalRequestData);
// END MassPay API Call
//Update database on succcessful payment
The script executes fine and the mass payment to the vendor in the Sandbox works, but it NEVER STOPS. If I complete a transaction using it, the senders PayPal account would be drained of all funds. I have read that there may be an issue with the Sandbox, but not sure if that is accurate and there is no way I can put this into production. Can anyone give me an idea of how to solve or identify the problem?
Try using:
break;
Or:
exit();
Which ill make the script stop executing after executing the first time

IOS Push notifications with AWS SNS

I have a flash based ios app that I am currently testing on phone/ipod.
I have added push notification to the app and I am receiving device tokens from apple.
I have added those tokens to AWS SNS APN SandBox
The platform Application ARN looks like
arn:aws:sns:us-east-1:7860818154325:app/APNS_SANDBOX/gamename
I have added tokens to the PAA and they look like:
token: f63d05538cd7d9b86c76188dbfeb42edf26a1e23b47334a02a205e0fb54a812a
endpoint ARN: arn:aws:sns:us-east-1:7860818154325:endpoint/APNS_SANDBOX/gamename/7412740a-79e4-3b71-8e7f-56e127bc4cg1
Sending out the notification via PHP:
require 'vendor/autoload.php';
$sns = Aws\Sns\SnsClient::factory(array(
'key' => 'mykey',
'secret' => 'mysecret',
'region' => 'us-east-1'
));
$snsmessage = $sns->publish(array(
'TargetArn' => "$endpoint",
'Message' => 'A game is waiting for you to play'
));
(where $endpoint = "arn:aws:sns:us-east-1:7860818154325:endpoint/APNS_SANDBOX/gamename/7412740a-79e4-3b71-8e7f-56e127bc4cg1")
$snsmessage returns a message ID, so it looks like it is sending the message.
However no message is ever received on the phone.
I have tried manually sending a message from the SNS portal and the same issue. IT sends with no error message but it never arrives.
I am building the app as "device testing" and I am using provisionprofile that has push notifcation enabled.
I am not sure where to start looking to find the issue.
Any help would be appreciated.

Credit Card processing using Pin Payments - pin.net.au - using omnipay gateway, card_token method (pin.js)

I am trying to integrate Pin.net.au CC processing into my site. I am using Omnipay library to make the calls.
To not store CC details in my server, I am using the Pin.js token method.
On form submit page (after user fills in personal and CC details) javascript does a 'prevent default' and sends the data from forms (browser) straight to pin.net.au servers. Server sends a card_token in response and resubmits the form to my server.
This token is recieved successfully and I can output it in my tests.
I get into trouble when I take that token and send a purchase request to pin.net.au. According to the API docs, I need not send user and card details when I send the token (the entire point of the token, really). I send this token along with other compulsory bits like email, amount, description etc.
This works when I cURL on my terminal and I get a charge success.
However, sending this purchase/charge request using the Omnipay library, each time I get a 422 (invalid resource) that asks for the user details and CC information. It should have populated this stuff from the token I sent.
I have scoured the API docs of both Omnipay and Pin.net.au. I don't seem to be doing anything wrong. What am I missing?
Here's my charge request:
$gateway = GatewayFactory::create('Pin');
$gateway->setSecretKey('MY_SECRET_KEY');
$response = $gateway->purchase([
'email' => 'user#email.com',
'description' => 'Package',
'amount' => '99',
'currency' => 'AUD',
'card_token' => Input::get('card_token'),
'ip_address' => Input::get('ip_address')
])->send();
Finally, it shouldn't really matter but if you'd like to know, I'm using Laravel 4.
Your example request has an amount of 99, the minimum amount for a Pin Payments charge is $1 (amount = 100).
I don't think this is the problem you are referring to though, it looks like Omnipay does not support using the card_token gear. If you go look over here - https://github.com/adrianmacneil/omnipay/blob/master/src/Omnipay/Pin/Message/PurchaseRequest.php#L34 - you can see Omnipay isn't sending the card_token field with it's request, it only tries to send card details, which obviously aren't present from your example!
Perhaps you could get in touch with the Omnipay developers or write a pull request yourself!
This is fixed in Omnipay v1.0.4 - you should be able to use the token like this:
$gateway = GatewayFactory::create('Pin');
$gateway->setSecretKey('MY_SECRET_KEY');
$response = $gateway->purchase([
'description' => 'Package',
'amount' => '99.00',
'currency' => 'AUD',
'token' => Input::get('token'),
'ip_address' => Input::get('ip_address'),
'card' => ['email' => 'user#email.com'],
])->send();

Want to send some Information with message which will be return to server on successfull delivery

I am using Google GCM service for push notification , it is working properly and receiving request from my server and generating response like below:
{"multicast_id":65162440918454631,"success":1,"failure":0,"canonical_ids":0,
"results":[{"message_id":"0:137098281085815%98234feef459fd7ecd"}]}
But this is the response of accepting request to Google GCM server not of delivery of Notification to device.
What i want to do is send some message_id with request to Google GCM server and when that notification delivered to device it will return that message_id ,so that i will update status of respective message_id in database for reporting purpose.
Code for Request:
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => array("msg" => $message,"msgid"=>12345),
"collapse_key" => "message_alert",
"time_to_live" => 1800,
"delay_while_idle" => true
);
Want a syntax or way by which message_id is send to server.
Is there some other way by which delivery status of notification will be get?

Categories