I am using Laravel Cashier but this time I am calling the StripeClient.
I am trying to display the CardException errors when a user entered expired card number. But it is just returning a response of paymentMethods->create instead CardException errors.
Here is the code
try{
$stripe = new \Stripe\StripeClient(config('app.stripe_secret'));
echo $stripe->paymentMethods->create([
'type' => 'card',
'billing_details' => [
'name' => '123',
'email' => '123#gmail.com',
],
'card' => [
'number' => '4000000000000002',
'exp_month' => 11,
'exp_year' => 2023,
'cvc' => '314',
],
]);
} catch(\Stripe\Exception\CardException $e){
echo 'Status is:' . $e->getHttpStatus() . '\n';
echo 'Type is:' . $e->getError()->type . '\n';
echo 'Code is:' . $e->getError()->code . '\n';
echo 'Param is:' . $e->getError()->param . '\n';
echo 'Message is:' . $e->getError()->message . '\n';
}
inside try
first check stripe token create or not
$token = $stripe->tokens()->create(
[
'card' => [
'number' => $request->get('card_no'),
'exp_month' => $request->get('ccExpiryMonth'),
'exp_year' => $request->get('ccExpiryYear'),
'cvc' => $request->get('cvvNumber'),
],
]
);
if (!isset($token['id'])) {
// Session::flash('error', 'The Stripe Token was not generated correctly');
// return Redirect::back();
$json['type'] = 'error';
$json['message'] = 'The Stripe Token was not generated correctly';
return $json;
}
Related
Facing cURL Error: Operation timed out after 15001 milliseconds with 0 bytes received issues with Woocomerce API to create products.
I am using the Laravel package i.e https://github.com/Codexshaper/laravel-woocommerce
It was working fine and creating products but suddenly it stopped working and start throwing PHP errors.
Below are the method that I am using to create a book on Woocomerce from laravel Controller:
public function addProductToWC(Request $request)
{
set_time_limit(0);
$response = '';
if ($request->isMethod('post')){
if(!empty($request->get('book_id'))){
$book = Book::find($request->get('book_id'));
$coverImgPath = base_path('public/customize_book/'.Session::get('cover_image'));
if (file_exists($coverImgPath)) {
$imageurl = url('/public/customize_book/'.Session::get('cover_image'));
} else {
$imageurl = url('/images/'.$book->bookimage);
}
if(!empty($book->id)){
$data = [
'name' => $book->title,
'type' => 'simple',
'regular_price' => number_format($request->get('book_price')),
'description' => (!empty($book->description) ? $book->description :''),
'short_description' => 'Simple product short description.',
'categories' => [
[
'id' => 1
]
],
'images' => [
[
'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg'
],
[
'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg'
]
]
];
$product = Product::create($data);
if($product['id']){
$response = array('error' => false,'code' => '200', 'data' => array('product_id' => $product['id'], 'message' => 'Product created successfully.'));
}else{
$response = array('error' => true,'code' => '401', 'data' => array('product_id' => $product['id'], 'message' => 'Product syncing failed please try again later.'));
}
}else{
$response = array('error' => true,'code' => '401','message' => 'Invalid book detail please try again.');
}
}else{
$response = array('error' => true,'code' => '401','message' => 'Invalid book detail please try again.');
}
}else{
$response = array('error' => true,'code' => '401','message' => 'Invalid method please try again.');
}
// return response
return response()->json($response);
}
Looking at the composer.json at https://github.com/Codexshaper/laravel-woocommerce/blob/master/composer.json, I can see that they are using the woocommerce client "automattic/woocommerce": "^3.0" This defaults to a request timeout of 15 seconds, hence why set_time_limit(0); didn't fix the issue.
When using it directly you'd set the timeout in the options
$woocommerce = new Client(
env('MGF_WOOCOMMERCE_API_URL'), // Your store URL
env('MGF_WOOCOMMERCE_API_KEY'), // Your consumer key
env('MGF_WOOCOMMERCE_API_SECRET'), // Your consumer secret
[
'timeout' => 120, // SET TIMOUT HERE
'wp_api' => true, // Enable the WP REST API integration
'version' => 'wc/v3' // WooCommerce WP REST API version
]
);
Looking at the library source https://github.com/Codexshaper/laravel-woocommerce/blob/master/src/WooCommerceApi.php
$this->client = new Client(
config('woocommerce.store_url'),
config('woocommerce.consumer_key'),
config('woocommerce.consumer_secret'),
[
'version' => 'wc/'.config('woocommerce.api_version'),
'wp_api' => config('woocommerce.wp_api_integration'),
'verify_ssl' => config('woocommerce.verify_ssl'),
'query_string_auth' => config('woocommerce.query_string_auth'),
'timeout' => config('woocommerce.timeout'),
]
);
It looks like the timeout is coming from woocommerce.timeout in your Laravel config file.
How do I get the filename of failed uploaded file and pass it on validation error message? For example: "The file.0 must be 1024 kilobytes." be like "The failed-file-sample.jpg must be 1024 kilobytes."
Here's the sample code arrangement:
$messages = [
'mimes' => 'File(s) must be of type: :values.',
'size' => 'The ' . $request->file('file_field')->getClientOriginalName() . ' must be :size kilobytes.'
];
$this->validate(
request(),
['file_field' => 'bail|required|mimes:gif,jpg,jpeg,png|size:1024'],
$messages
);
Use:
$validationArray = [];
foreach ($request->file('file_field') as $key => $file) {
$validationArray['file_field.'.$key.'.size'] => 'The ' . $file->getClientOriginalName() . ' must be 1024 kilobytes.';
}
$messages = [
'file_field.*.mimes' => 'File(s) must be of type: :values.',
$validationArray
];
$this->validate(
$request,
[
'file_field' => 'bail|required|array',
'file_field.*' => 'mimes:gif,jpg,jpeg,png|size:1024'
],
$messages);
I'm trying to send a message with a phone number as a link. Is there a way to acomplish that? I'm using the Laravel Telegram Bot API
public function start( $telegram, $chatid ) {
$keyboard = [
['Continue'],
];
$reply_markup = Telegram::replyKeyboardMarkup([
'keyboard' => $keyboard,
'resize_keyboard' => true,
'one_time_keyboard' => true
]);
$text = "<b>¡Hola!</b> " . config('constants.emojis.waving-hand') .
" Hey I'm Bot. " . config('constants.emojis.hug-face') .
"\nPlease chose some option" .
config('constants.emojis.glass') . "\nOr contact us at <a href='tel:018099999999'>HEREÍ</a> to talk with an specialist.";
$this->sendMessage( $telegram, $chatid, $text, $reply_markup );
}
Also here is my send message function
public function sendMessage( $telegram, $chatid, $text, $reply_markup ) {
if ( !is_null( $reply_markup ) ) {
$response = $telegram->sendMessage([
'chat_id' => $chatid,
'text' => $text,
'parse_mode' => 'HTML',
'reply_markup' => $reply_markup
]);
} else {
$response = $telegram->sendMessage([
'chat_id' => $chatid,
'text' => $text,
'parse_mode' => 'HTML',
]);
}
}
Thanks in advance.
you can use the phone number with plus and country code:
+989123456789
if you use that format, telegram automatically link the number!
I'm on Laravel 5.3 and I was working on a form I POST submit to the server. What I want to point out right at the beginning is that I have been working on this form for days, with no issues what so ever. Yesterday I was working in the store method of my controller, when all of a sudden the form won't submit at all anymore.
The opening form:
{!! Form::model($addon = new \App\Addon, ['name' => 'FinalForm', 'route' => 'addons.store', 'enctype' => 'multipart/form-data']) !!}
My route:
Route::resource('addons', 'AddonController');
php artisan route:list:
Method: POST
URI: addons
Name: addons.store
Action: App\Http\Controllers\AddonController#store
Middleware: web,auth
So what happens now is, the form gets posted, but then, for some reason, get's redirected. This is what chrome says:
POST request to /addons
And after that, for some reason, GET request to /addons/
As you can see I'm getting a 403 Forbidden response. Why is the request being redicted to /addons/ ??
As I said before, I changed NOTHING, I was working with the inner logic of the store method on the controller when this popped up. I even restarted the homestead box (and my whole machine), I don't know what is causing it.
EDIT (controller):
/**
* Store the addon.
*
* #return Response
**/
public function store(CreateAddonRequest $request, ImageHandler $imageHandler)
{
$input = $request->only('title', 'body', 'author', 'slogan', 'version', 'revision', 'published_at', '_img_data');
$session = $request->session()->all();
$slug = SlugService::createSlug(Addon::class, 'title', $input['title'], ['unique' => true]);
$imageHandler->move($slug, $input['body'], 'description');
$body = $imageHandler->body;
$addon = Auth::user()->addons()->create([
'body' => $body,
'title' => $input['title'],
'slogan' => $input['slogan'],
'author' => $input['author'],
'locales' => $session['locales'],
'published_at' => $input['published_at']
]);
$addon->categories()->attach($request->input('categories'));
Storage::makeDirectory('addons/' . $slug . '/files/');
$extension = Dir::extension('temp/' . $session['file_name']);
if ($input['revision'] != "") {
$file = $addon->slug . '-v' . $input['version'] . '-r' . $input['revision'] . '.' . $extension;
} else {
$file = $addon->slug . '-v' . $input['version'] . '.' . $extension;
}
Storage::disk('local')->move('temp/' . $session['file_name'], 'public/addons/' . $addon->slug . '/files/' . $file);
// With 5.4 we don't need that check anymore
if ($input['revision'] == "") {
$input['revision'] = null;
}
$addon->files()->create([
'file_name' => $file,
'hash' => $session['hash'],
'version' => $input['version'],
'revision' => $input['revision'],
'game_version' => $session['interface'],
'virustotal' => $session['virustotal']
]);
dd('done');
// if ($request->hasFile('images'))
// {
// Storage::makeDirectory('addons/' . $slug . '/images/');
// foreach ($request->file('images') as $image)
// {
// $imageName = uniqid() . '.' . $image->getClientOriginalExtension();
// //$image->storeAs('addons/' . $slug . '/images/', $imageName);
// $addon->images()->create(array('image_name' => $imageName));
// }
// }
// flash('The addon has been uploaded.');
// return redirect('addons');
dd('done');
return response()->json([
'success' => true,
'message' => 'Images checked',
]);
}
I've been working on upgrading from v 1 to 1.1 for a few days now. I have 100s of users that can't see their followers in my dashboard.
I followed all the steps by changing /1/ to /1.1/ still not working.
Getting error 404 (Not Found)
Controller:
function twitter_auth()
{
if ( !$this->tweet->logged_in() )
{
die('some how you are not logged in');
}
$tokens = $this->tweet->get_tokens();
$this->tweet->enable_debug(TRUE);
$user = $this->tweet->call('get', 'account/verify_credentials');
var_dump($user);
}
$options = array(
'count' => 10,
'page' => 2,
'include_entities' => 1
);
$timeline = $this->tweet->call('get', 'statuses/home_timeline');
var_dump($timeline);
}
Jobs:
function get_twitter_subscribers($args)
{
$rate = #file_get_contents('http://api.twitter.com/1.1/application/rate_limit_status.json');
$rate = #json_decode($rate);
if($rate->remaining_hits == 0):
$status = 'retask';
else:
$new_data = #file_get_contents('http://api.twitter.com/1.1/users/show.json? screen_name='.$args['account_id'].'&include_entities=true');
$new_data = #json_decode($new_data);
if(isset($new_data->followers_count)){
$insert_args = array(
'account_id' => $args['account_id'],
'metric' => 'subscribers',
'value' => $new_data->followers_count,
'platform' => 'twitter'
);
$insert = $this->CI->metrics_model->insert_metric($insert_args);
if (isset($insert)) { $status = 'complete'; }
}else{
$this->send_error_email("Function: get_twitter_subscribers - new_data->followers_count not set for account id: " . $args['account_id'] . "\r\n\r\n" . "args: " . http_build_query($args) . "\r\n\r\n" . 'http://api.twitter.com/1.1/users/show.json?screen_name='.$args['account_id'].'&include_entities=true');
}
endif;
if(!isset($status)){
$this->send_error_email("Function: get_twitter_subscribers - status not set." . "\r\n\r\n" . "args: " . http_build_query($args) . "\r\n\r\n" . 'http://api.twitter.com/1.1/users/show.json? screen_name='.$args['account_id'].'&include_entities=true');
$status = 'error';
}
return $status;
controllers:
function auth($platform)
{
if($platform == 'facebook'):
echo 'auth facebook';
elseif($platform == 'twitter'):
$this->load->library('tweet');
// current key in php page
$tokens = array(
'oauth_token' => 'xxxxxxxxxxxxx',
'oauth_token_secret' => 'xxxxxxxxxxxx'
);
$this->tweet->set_tokens($tokens);
if ( !$this->tweet->logged_in() ) :
$this->tweet->set_callback(site_url('manage/auth/twitter'));
$this->tweet->login();
else:
echo 'Twitter logged in. Return to manager';
endif;
endif;
}
What am I doing wrong?
This issue has been resolved. I created my own cURL library.
function get_twitter_subscribers($args){
$request = curl_init();
$bearer = "AAAAAAAAAAAAAAAAAAAAAJ%2xxxxxxxxxxxxxxx0000x0x0x0x0";
curl_setopt($request, CURLOPT_SSLVERSION, 1);
curl_setopt($request, CURLOPT_URL, 'https://api.twitter.com/1.1/users/show.json?screen_name='.$args['account_id'].'&include_entities=true');
curl_setopt($request, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$bearer));
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$new_data = json_decode($file_get_contents = curl_exec($request));
curl_close($request);
if(isset($new_data->followers_count)){
$insert_args = array(
'account_id' => $args['account_id'],
'metric' => 'subscribers',
'value' => $new_data->followers_count,
'platform' => 'twitter'
);
$insert = $this->CI->metrics_model->insert_metric($insert_args);
if (isset($insert)) { $status = 'complete'; }
}else{
$this->send_error_email("Function: get_twitter_subscribers - new_data->followers_count not set for account id: " . $args['account_id'] . "\r\n\r\n" . "args: " . http_build_query($args) . "\r\n\r\n" . 'https://api.twitter.com/1.1/users/show.json?screen_name='.$args['account_id'].'&include_entities=true');
}
if(!isset($status)){
$this->send_error_email("Function: get_twitter_subscribers - status not set." . "\r\n\r\n" . "args: " . http_build_query($args) . "\r\n\r\n" . 'http://api.twitter.com/1.1/users/show.json?screen_name='.$args['account_id'].'&include_entities=true');
$status = 'error';
}
return $status;
}