All the things working perfect on android but ios not receiving any message from php backend api.
CASE 1 : successfully receiving PN on mobile app when testing from here https://www.pushtry.com/ when app in background mode.
CASE 2: Below php script not sending PN not even in background mode app.
Here is php api code script.
use Kreait\Firebase\Factory;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Notification;
public function send_push($target_user_id)
{
$fcm_token = get_user_meta($target_user_id, 'fcm_token', true);
if (!empty($fcm_token)) {
$title = "Your Invitation Was Accepted";
$body = 'Click here to start game';
try {
$message = CloudMessage::withTarget('token', $fcm_token)
->withNotification(Notification::create($title, $body))
->withData([
'notification_type' => 'invitation_accepted',
'title' => $title,
'body' => "Your partner's accepted your invitation",
]);
$this->messaging->send($message);
} catch (\Throwable $e) {
// for PHP 7.x
$this->on_error($e);
} catch (\Exception $e) {
// for PHP 5.x
$this->on_error($e);
}
}
}
Can someone please help me to guide for ios :)
Related
I have a weird issue with microsoft graph api v1.
I am trying to setup a subscription, so that I get webhook notification after a new outlook email arrives.
$body = [
"changeType" => "created,updated",
"notificationUrl" => route('api.outlook.push'),
"resource" => "me/messages",
"expirationDateTime" => now()->addMinutes(self::SUBSCRIPTION_EXPIRATION_MINUTES),
];
$response = null;
try {
$response = $this->service->createRequest('POST', '/subscriptions')
->attachBody(json_encode($body))
->setReturnType(Subscription::class)
->execute();
} catch (Exception $exception) {
logger()->info('outlook subscription exception', ['message' => $exception->getMessage()]);
}
This seems to be working correctly as microsoft is sending a notification to specified url with verification code, that I'm returning and the message says that subscription was created. The issue is that even though it was set up properly - I am not getting a single one webhook. Did any of you guys had this issue and manage to fix it?
I am setting up a small Slim 3 application which will be the middleman for processing Web based Stripe payment form. The Slim application will handle the business logic of reaching out to Stripe and conducting the payment, as well as sending the payment data off to some third-party CRM / Applications.
My issue during testing is that when i make a test payment through the StripeJS payment form the slim application is actually logging 3 duplicate requests 1-second apart. Here's what the logging looks like:
test#gmail.com Attempting to make payment 2020-04-27 05:34:39
test#gmail.com Attempting to make payment 2020-04-27 05:34:40
test#gmail.com Attempting to make payment 2020-04-27 05:34:41
test#gmail.com Payment success 2020-04-27 05:34:44
test#gmail.com Payment success 2020-04-27 05:34:45
test#gmail.com Payment success 2020-04-27 05:34:47
This is all from an individual "payment" using the Stripe Test credentials.
The Slim layout is fairly straightforward in terms of the route passing the request directly to my StripeController class. The StripeController class ( in development ) will take the values of the request and pass it to the Stripe API to conduct the payment.
The route layout:
$this->post('/stripe-payment', 'StripeController:stripePayment');
The controller (stripped to essentials for privacy ):
class StripeController extends Controller
{
private $privateKey;
private $httpClient;
public function __construct() {
$this->privateKey = getenv('STRIPE_KEY');
$this->httpClient = new Client();
\Stripe\Stripe::setApiKey($this->privateKey);
}
//
public function stripePayment($request, $response)
{
// Get the request body
$r = (object)$request->getParsedBody();
// Initial Logging
$log = new LogController($r->email_address, 'Attempting to make payment');
$log->saveLog();
// Attempt to find an existing user
$user = User::where('email', $r->email_address)->first();
if($user !== null) {
// If user exists, use their StripeID
$invoice = $this->stripeInvoice($r, $user->stripe_id);
// Log the transaction
$this->savePayment( $user->id, 'stripe-invoice', $invoice->id, $invoice->amount_paid );
// Third party application push
$this->pushUser($r, $invoice->amount_paid);
// Return with success
return $response->withRedirect('example.com/payment-success/');
} else {
// Same as above, just create a user then push.
}
}
public function stripeInvoice(object $r, string $stripe_id)
{
// Try to create the invoice in stripe
try {
\Stripe\InvoiceItem::create([
'customer' => $stripe_id,
'amount' => (int)$r->product,
'currency' => 'USD',
'description' => $plan_desc
]);
} catch ( Exception $e ) {
error_log('STRIPE_INVOICE_EXCEPTION: '.$e->getMessage());
}
// Try to charge the customer the invoice we just created
try {
$invoice = \Stripe\Invoice::create([
'customer' => $stripe_id
]);
$_get_invoice = \Stripe\Invoice::retrieve($invoice->id);
$_get_invoice->pay();
$success = 1;
// Use Stripe's library to make requests...
} catch(\Stripe\Exception\CardException $e) {
$error = $e->getMessage();
// Since it's a decline, \Stripe\Exception\CardException will be caught
} catch (\Stripe\Exception\RateLimitException $e) {
$error = $e->getMessage();
// Too many requests made to the API too quickly
} catch (\Stripe\Exception\InvalidRequestException $e) {
$error = $e->getMessage();
// Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Exception\AuthenticationException $e) {
$error = $e->getMessage();
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
} catch (\Stripe\Exception\ApiConnectionException $e) {
$error = $e->getMessage();
// Network communication with Stripe failed
} catch (\Stripe\Exception\ApiErrorException $e) {
$error = $e->getMessage();
// Display a very generic error to the user, and maybe send
// yourself an email
} catch (Exception $e) {
// Something else happened, completely unrelated to Stripe
$error = $e->getMessage();
}
if($success != 1) {
return $response->withRedirect('example.com&error='.$error);
} else {
return $_get_invoice;
}
}
}
Is there anything that Slim is doing in the background, like an origin check, that could be causing the duplication issue ?
Need assistance with Stripe error response.
Everything seems to work according to stripe dashboard logs when creating a Customer and then enrolling them into a Subscription therefore generating a Charge, in the event where there are multiple requests made with idempotency keys in place.
How ever in this event I get this stripe error response (capture of the exception $error6) which opens up as a page (charge.php) where the code runs, instead of sending to success page.
charge.php
\Stripe\Stripe::setApiKey('sk_live_xxxxxxxxxxx');
$POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);
$email = $POST['email'];
$token = $POST['stripeToken'];
$membership_type = $POST['membership_type'];
$user_id = $POST['user_id'];
$success = 0;
try {
// Create customer in Stripe
$customer = \Stripe\Customer::create([
"email" => $email,
"source" => $token,
],[
"idempotency_key" => $_SESSION['sid2'],
]);
$success = 1;
} catch(Stripe_CardError $e) {
$error1 = $e->getMessage();
} catch (Stripe_InvalidRequestError $e) {
// Invalid parameters were supplied to Stripe's API
$error2 = $e->getMessage();
} catch (Stripe_AuthenticationError $e) {
// Authentication with Stripe's API failed
$error3 = $e->getMessage();
} catch (Stripe_ApiConnectionError $e) {
// Network communication with Stripe failed
$error4 = $e->getMessage();
} catch (Stripe_Error $e) {
// Display a very generic error to the user, and maybe send
// yourself an email
$error5 = $e->getMessage();
} catch (Exception $e) {
// Something else happened, completely unrelated to Stripe
$error6 = $e->getMessage();
}
if ($success!=1)
{
$_SESSION['error1'] = $error1;
$_SESSION['error2'] = $error2;
$_SESSION['error3'] = $error3;
$_SESSION['error4'] = $error4;
$_SESSION['error5'] = $error5;
$_SESSION['error6'] = $error6;
print_r($_SESSION);
}
// Add Customer to a Subscription in Stripe
$subscription = \Stripe\Subscription::create([
'customer' => $customer->id,
'items' => [['plan' => $membership_type]]
],[
"idempotency_key" => $_SESSION['sid'],
]);
//adding all relevent info into data base...
//send user to success page
header('Location: ../success.php?id='.$user_id.'&product='.$subscription->plan->nickname);
Could this be because each time the Stripe JS $token parameter changes? is this normal or am I doing something wrong? (I got a similar error when running idempotency only on Subscribing a Customer but then stripe creates multiple customers with the same email and payment cards but different customer->id)
Can anyone kindly suggest how can I resolve this error page?
Could this be because each time the Stripe JS $token parameter changes?
Yes, I suspect what's happening here is that you are reusing $_SESSION['sid2'] for two separate requests with different source parameters to create a Customer. And this error in response is the expected behavior!
You should be able to see this in your Dashboard logs: Assuming this is a test mode request, https://dashboard.stripe.com/test/logs/iar_IgylJRGpbLyVb6 should tell you where the same key was originally used.
best option i could find is disabling the button after the Stripe JS event listener
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
$('.button').attr("disabled", true);
stripe.createToken(card).then(function(result) {...........
Our code sends twilio sms messages by doing the following:
// send the text message to the member's mobile phone
try {
// attempt to send the message through Twilio
$tw_msg = $twilio_client->messages->create(
"+1".$recipient['address'],
array (
'From' => "+1".$org['twilio_number'],
'Body' => $txtmsg,
'StatusCallback' => CALLBACK_LINK.'/text_message_handler.php'
)
);
// else trap the error since the message could not be sent and the callback routine is not called
} catch (Exception $e) {
// process the text message error
process_text_msg_error($db, $e, $org, $msg, $recipient);
}
In the v4 library we would get the error code by doing the following:
// get the twilio error components
$twilio_error_status = $e->getStatus();
$twilio_error_code = $e->getCode();
$twilio_error_msg = $e->getMessage();
This is not giving us what we expected using the V5 library. How do we get the error status and code using the V5 lib?
Twilio developer evangelist here.
It looks to me like you need to update one of the methods you call on the exception to get the status code. The exception is now a RestException and has the method getStatusCode(). You should update to:
// get the twilio error components
$twilio_error_status = $e->getStatusCode();
$twilio_error_code = $e->getCode();
$twilio_error_msg = $e->getMessage();
Let me know if that helps at all.
I am using Twilio sdk to send SMS in my PHP based application,I have an array of phone numbers and sending an SMS to each number in this array in a loop.....the problem is while the loop is running if an invalid number comes twilio API returns 500 internal server error and it stops the loop without trying sending sms for rest of the numbers in the array.
This is the code I am using for sending sms :
public function sendSmsAction($userphones)
{
foreach($userphones as $user_phone)
{
$message = 'Thanks for coming';
$this->twiliosms($user_phone,$message);
}
}
private function twiliosms($phone_num,$message)
{
require Mage::getBaseDir()."/twilio-php-master/Services/Twilio.php";
$AccountSid = "XXXXXXX";
$AuthToken = "XXXXXXX";
$client = new Services_Twilio($AccountSid, $AuthToken);
try {
$message = $client->account->messages->create(array(
"From" => "+1XXXXXXXXXX",
"To" => $phone_num,
"Body" => $message,
));
} catch (Services_Twilio_RestException $e) {
echo $e->getMessage();
}
}
And this is the error I am getting :
Status: 500 Internal Server Error
The 'To' number +1XXXXXXXXXX is not a valid phone number or shortcode.
please help me out in handling this error.
Thanks in advance.
Finally, I got my solution....by just adding another catch() made it working fine...
catch(Exception $e)
{
}