Fatal error: Class 'Stripe\Stripe_Charge' not found in - php

This is the code I'm using.
<?php
require_once('init.php');
if ($_POST) {
\Stripe\Stripe::setApiKey("xxxxxxxxxxxxx");
$error = '';
$success = '';
try {
if (!isset($_POST['stripeToken']))
throw new Exception("The Stripe Token was not generated correctly");
$charge = \Stripe\Stripe_Charge::create(array("amount" => 100, //995
"currency" => "eur",
"card" => $_POST['stripeToken']));
$success = 'Your payment was successful.';
}
catch (Exception $e) {
$error = $e->getMessage();
}
}
?>
Why am I getting the error
"Fatal error: Class 'Stripe\Stripe_Charge' not found in"

I was pulling my hair out on this one as well, but finally found the solution.
From what I found out older versions of PHP (<5.3) did not use namespaces. To get around this PSR-0* converted underscores to directory names. This created a messy interface for Composer. Now with PHP 5.3 and higher namespaces have been added. Composer now uses PSR-4 which supports namespaces but does not convert underscores to directories. Therefore, if you are using the newer preferred PSR-4, you need to use the new method. Therefore, your line:
$charge = \Stripe\Stripe_Charge::create(...);
needs to become:
$charge = \Stripe\Charge::create(...);
You can double check to see what version of PSR your Stripe module is using by opening the /vendor/stripe/stripe-php/composer.json file. Mine shows:
"autoload": {
"psr-4": { "Stripe\\" : "lib/" }
},
which clearly states that I am using PSR-4.
Here is some good reading on the subject: https://mattstauffer.co/blog/a-brief-introduction-to-php-namespacing
*PSR stands for PHP Standards Recommendation

I discovered that neither UsageRecord.php nor UsageRecordSummary.php were available in my stripe-php/lib directory and likewise not initiated in my stripe-php/init.php. I went and grabbed them from https://github.com/stripe/stripe-php/blob/master/lib/
Using Composer to install. adam-paterson/oauth2-stripe. Not sure if this is missing there.

Related

Model\User not found

I am trying to retrieve information on Users using the Microsoft Graph Library for PHP.
I can see that in GraphResponse.php, my $result is filled with correct data but the code does not find the Model\User-class.
$user = $graph->createRequest("GET", "/users")
->setReturnType(Model\User::class)
->execute();
Since I am not familiar with this autoload-thing, I am stuck. Any help appreciated.
Is there any better documentation for this subject???
It appears you're experiencing some troubles with installing msgraph-sdk-php package and that's the reason why Model\User type could not be resolved in your case. If you are following the official Getting Started documentation, there is a typo regarding package name in Installation section:
{
"require": {
"Microsoft/Graph": "^1.0"
}
}
and while installing you should get the error like this
The requested package microsoft/graph could not be found in any
version, there may be a typo in the package name.
Instead of Microsoft/Graph the valid name should be microsoft/microsoft-graph, for example:
{
"require": {
"microsoft/microsoft-graph": "^1.6"
}
}
Once the package is installed successfully, the minimal example for retrieving users could look like this:
require_once './vendor/autoload.php';
use \Microsoft\Graph\Graph;
$accessToken = "--YOUR-ACCESS-TOKEN-GOES-HERE--";
$graph = new Graph();
$graph->setAccessToken($accessToken);
$user = $graph->createRequest("GET", "/users")
->setReturnType(\Microsoft\Graph\Model\User::class)
->execute();

Call to undefined method Maatwebsite\Excel\Excel::create() - laravel 5.6

I am using Maatwebsite/Excel in my application and when i get the error
Call to undefined method Maatwebsite\Excel\Excel::create()
from one of my controllers. I am using laravel 5.6 and i have followed the documentation strictly and other very few related discussions online to solve this, but i still get the error.
How do i solve this error please
app.php
'provider' => 'Maatwebsite\Excel\ExcelServiceProvider',
'alias' => 'Excel'=> 'Maatwebsite\Excel\Facades\Excel',
Controller
$cl = ClassModel::Select('name')->where('code',$input->class)->first();
$input->class=$cl->name;
$fileName=$input->class.'-'.$input->section.'-'.$input->session.'-'.$input->exam;
// return $students;
Excel::create($fileName, function($excel) use($input,$subjects,$students) {
$excel->sheet('New sheet', function($sheet) use ($input,$subjects,$students) {
$sheet->loadView('app.excel',compact('subjects','input','students'));
});
})->download('xlsx');
You are using 2.* syntax while using 3.* package. Please refer to the correct documentation over here: https://laravel-excel.maatwebsite.nl/docs/3.0/export/basics
Try to decrease the version using :
composer require "maatwebsite/excel=2.1.0"
There have been many changes in the new version of the package.
In your composer.json file inside the require array replace your package with this:
"maatwebsite/excel": "~2.1.0",
and then run composer update
This should work fine.
Please switch to version 2*
Version 3.0 of that package doesn't handle imports yet. Release date for this feature is unknown. See this post for more details: maatwebsite
I have worked it around but I know it's not a perfect solution. It will really help you if you only have concerns with the uploading not adjusting Crudbooster features.
I removed the extra features from the importing screen of the Crudbooster by applying the following CSS in the crudbooster-controller.
$this->style_css = "ul.nav li:not(:first-child) {
display: none;
}";
I copied the getImportData() method from Crudbooster CBController and overridden it in the crudbooster-controller by the following code.
//PHP
//By the way, you can still create your own method in here... :)
public function getImportData()
{
$this->cbLoader();
$data['page_menu'] = Route::getCurrentRoute()->getActionName();
$data['page_title'] = 'Import Data';
if (request('file') && ! request('import')) {
$file = base64_decode(request('file'));
$file = storage_path('app/'.$file);
$data = Excel::import(new ProductImport, $file);
CRUDBooster::redirect('/admin/products', cbLang("alert_add_data_success"), 'success');
}
return view('crudbooster::import', $data);
}
Importing is working fine now

Using AdWords BatchJob in PHP

I'm trying to get new BatchJob (https://developers.google.com/adwords/api/docs/guides/batch-jobs) up and running, however missing one part.
Docs says:
The good news is that your client library of choice will have a
utility that handles constructing and sending the request for you. The
example below uses the BatchJobHelper utility from the Java client
library.
However PHP library is missing that Helper and any method that should do that...
Anyone had any luck sending request to API using BatchJob? I can't find any working example anywhere.
Thanks!
In the branch experimental they are rewriting the API. It seems that the BatchJobHelper is missing still (current day of write this), see my issue in github requesting it.
For get BatchJobs you should use the BatchJobService class, which is instantiated from the adword service. This is a example snippet:
$batch_job_service = $adWordsServices->get($session, 'BatchJobService', 'v201605', 'cm');
try
{
/** #var BatchJobReturnValue $result */
$result = $batch_job_service->mutate($operations);
}
catch(ApiException $e)
{
echo $e->getMessage() . PHP_EOL;
}
if(!empty($result) && $result instanceof Google\AdsApi\AdWords\v201605\cm\BatchJobReturnValue)
{
$batch_job = reset($result->getValue());
}
else
{
echo 'Result is empty or no valid';
}
If you are using composer to load the new v201603 version of adwords you will need to also adjust your composer file to map the utils since they are duplicated across the other versions for some reason.. Not sure why they did this. you should be able to find the class you need with the following path. Hope this helps.
{
"require": {
"googleads/googleads-php-lib": "8.3.0"
},
"autoload": {
"classmap": [
"vendor/googleads/googleads-php-lib/src/Google/Api/Ads/AdWords/Util/v201601"
]
}
}

Gitonomy cannot access my remote repository

I am testing out the Gitonomy Library within PHP and something I just cannot put my finger on. I have created a small application found here with a class that uses the Repository class from the library to list available branches on GITHUB Remote REposictory. Below is the class:
namespace LuSoft\Library{
use Gitonomy\Git\Repository as RemoteRepositroy;
class Repository
{
public function getBranches($repository)
{
if(!is_string($repository)){
throw new Exception("Reposotory must be in a form of a string.");
}
$rr = new RemoteRepositroy($repository);
$branches = [];
foreach($rr->getReferences()->getBranches() as $branch){
$branches[] = $branch->getName();
}
$rr->run('fetch', array('--all'));
return $branches;
}
}
}
I have followed the example from here. Below is how I call this class in my index.php page.
require_once "vendor/autoload.php";
use LuSoft\Library\Repository;
$repository = new Repository();
try{
var_dump($repository->getBranches(__DIR__));
}catch(\Exception $e){
echo "Exception :" . $e->getMessage();
}
Below is the error that I get:
Exception :Error while getting list of references: '"git"' is not
recognized as an internal or external command, operable program or
batch file.
I am using the application I am testing with to get the branches and if things went on correctly I am suppose to be seeing master.
Can someone please explain to me how does the library really work in simplest form?
NB: My index.php is on the same folder level of the application where git has been initialized.

Trouble using DOTNET from PHP.

I've been trying to call a .net assembly from PHP through com (using DOTNET()). It seems like php is finding the DLL and initializing properly, but I can't see/use the methods for some reason. Anyone know how I might be able to fix this?
Here is the php code I'm using to call the .net class. When I call it the output is "hello1 hello2". When I try to directly call the function by doing $csclass->ModelBuilder("","") I get a 500 server error specifying that it couldn't find the function.
<?php
echo "hello1";
try{
$csclass = new DOTNET("ModelBuilder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1208136d23b48dc5",
"ModelBuilder.ModelBuilder2");
$class_methods = get_class_methods($csclass);
foreach ($class_methods as $method_name) {
echo "$method_name\n";
}
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
echo "hello2";
?>
Here is the the class in the assembly I'm trying to call (built using .net 3.5, signed with a strong name, and registered with gacutil):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using OfficeOpenXml;
using System.Runtime.InteropServices;
namespace ModelBuilder
{
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class ModelBuilder2
{
[ComVisible(true)]
public Boolean BuildModel(String outputFileLoc,String excelTemplateFile)
{
try
{
//do stuff
return true;
}
catch (Exception e)
{
return false;
}
}
}
The get_class_methods() will not work with a Dotnet class object. That thing is a proxy, and it does not behave the way a normal php object does.
If you really are focused on listing the methods of the object, then you need to look into implementing IDispatch on your .NET object. If, however, your main goal is to simply use the .NET object, and your attempt to list the methods was just a side effort to diagnose why that was not working, then I have some suggestions for you.
rather than using gacutil, consider whether you can insert the required DLL into the php directory, the place where php.exe resides. If php.exe resides at c:\php5\php.exe, then
copy your assembly into c:\php5.
If you follow the above suggestion, the .NET assembly need not be strongly named. It needs to be strongly named if you plan to GAC it. As I said, you don't need to GAC it, in order to load it with php. If you copy the assembly into the PHP dir, then you can use an un-signed assembly.
For early development and exploration with this, use the php.exe program from the command-line, rather than using IIS to launch php via a web request. This lets you see the error messages directly, rather than worrying about 500 errors from IIS.
Example
Suppose this is the C# code:
using System;
namespace Ionic
{
public class MathEx
{
System.Random rnd;
public MathEx ()
{
rnd = new System.Random();
}
public int RandomEven()
{
return rnd.Next()*2;
}
}
}
Compile it from the command-line like this:
c:\net3.5\csc.exe /t:library /out:Ionic.MathEx.dll MathEx.cs
copy the assembly to the dir that holds php.exe:
copy Ionic.MathEx.dll \php
1 file(s) copied.
Then, suppose I have a php script named mathex.php with these contents:
<?php
$my_assembly = 'Ionic.MathEx'; // name of the dll without the .dll suffix
$clz = new DOTNET($my_assembly, 'Ionic.MathEx');
for ($i=0; $i<5; $i++) {
echo $i . " " . $clz->RandomEven() . "\n";
}
?>
... and if I run it from the command line this way:
\php\php.exe mathex.php
...then I get these results:
0 -1083602762
1 1535669896
2 -86761710
3 -1204365564
4 459406052
Nota Bene
I tried doing this with an assembly compiled with .NET 4.0, but it did not work, giving me an error message about a .NET runtime mismatch. Like this:
Fatal error: Uncaught exception 'com_exception' with message 'Failed to instantiate .Net object
[CreateInstance] [0x8013101b] ' in C:\dev\php\mathEx.php:6
Stack trace:
#0 C:\dev\php\mathEx.php(6): dotnet->dotnet('Ionic.MathEx', 'Ionic.MathEx')
#1 {main}
thrown in C:\dev\php\mathEx.php on line 6
The 0x8013101b indicates a runtime mismatch - the runtime version of the assembly does not match the runtime version of the app that is trying to load the assembly.
From this I conclude that php5 is compiled to support .NET 2.0 (which includes 3.0 and 3.5). The runtime version changed for .NET 4.0, so assemblies that you compile with a .NET 4.0 compiler (including VS2010) will not be usable with php5.exe. The workaround is to just compile with the .NET 2.0 or 3.5 compiler (VS2005, vs2008).

Categories