chartmogul / chartmogul-php Goto Github PK
View Code? Open in Web Editor NEWChartMogul API PHP Client
License: MIT License
ChartMogul API PHP Client
License: MIT License
Working example
curl -X POST "https://api.chartmogul.com/v1/data_sources" -v --http1.1 \
-u xxxxxx:xxxxxx \
-H "Content-Type: application/json" \
-d '{
"name": "In-house billing"
}'
Not working example (if we assume that you are using latest OSx version or latest linux distro):
curl -X POST "https://api.chartmogul.com/v1/data_sources" -v \
-u xxxxxx:xxxxxx \
-H "Content-Type: application/json" \
-d '{
"name": "In-house billing"
}'
Same with this lib, it tries to use http2 connection instead of http1.1 and every POST request fails. GET requests works as expected.
Any tips on how to include this in a Laravel project, doesn't seem to be picking it up, it's getting the namespace's confused Class 'App\Http\Controllers\ChartMogul\Configuration' not found
The ChartMogul\Enrichment\Customer::update
method as described by the documentation is missing.
Package zendframework/zend-diactoros has been deprecated.
Instead of
ChartMogul\Configuration::getDefaultConfiguration()->setApiKey('<YOUR_API_KEY>');
It should be:
ChartMogul\Configuration::getDefaultConfiguration()->setAccountToken('<YOUR_API_KEY>');
ChartMogul\Configuration::getDefaultConfiguration()->setSecretKey('<YOUR_API_KEY>');
Since the repo does require to use guzzle 6, it's hard to upgrade.
Better rely on the psr client interface for http clients and let the people plug in guzzle 6 or 7 or symonfy http client
thanks so much
philipp
When using ChartMogul\Customer::get("UUID") I am getting the following error:
FatalThrowableError in GetTrait.php line 19:
Using $this when not in object context
I was able to add customers but unable to List Customers, Search by email or Find by External_Id, nor can I find plans by external_id they just don't work.
I get a response code 200, but with no result data. Dies silently.
Because of strict return types, version 2.2 is not compatible with 2.1, therefore all projects requiring it as ^2.1
will potentially have problems (I did).
Was just trying this out within the Laravel framework and I can't seem to get it to work as expected, it seems to find the Configuration class fine, but it's failing on almost any other, and I am getting:
Fatal error: Class 'ChartMogul\DataSource' not found
or if I try Ping:
Fatal error: Class 'ChartMogul\Ping' not found
PHP Strict standards: Static function ChartMogul\Resource\EntryTrait::getEntryClass() should not be abstract in /var/www/smartlook/application/vendor/chartmogul/chartmogul-php/src/Resource/EntryTrait.php:12
chartmogul-php/src/Metrics.php
Line 5 in 325f9a0
chartmogul-php/src/Metrics.php
Line 11 in 325f9a0
Requesting a customer with findByExternalId throws a Fatal error.
$external_id = "123";
$customer = ChartMogul\Customer::findByExternalId([
"external_id" => $external_id
]);
Fatal error: Call to a member function first() on null in /Users/Bill/c-php-test/vendor/chartmogul/chartmogul-php/src/Customer.php on line 109
https://github.com/chartmogul/chartmogul-php/blob/master/src/Customer.php#L109
It would be great if google play payload submission could be part of the api.
https://dev.chartmogul.com/docs/importing-google-play-purchase-payloads-into-chartmogul
Thanks!
After 100-200 requests to chartmogul php fails with exception " cURL error 6: getaddrinfo() thread failed to start (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)" Retrying same request works as expected, but it fails again after 100-200 requests.
Sometimes it fails with "Http\Client\Exception\NetworkException : cURL error 6: Could not resolve host: api.chartmogul.com (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)" error too.
The readme suggests using php-http/guzzle6-adapter
, but following that advice leads to a warning
Package php-http/guzzle6-adapter is abandoned, you should avoid using it. Use guzzlehttp/guzzle or php-http/guzzle7-adapter instead.
Probably, we should update that (the package suggested by the warning seems to work just fine)
I'm experiencing an issue after upgrading my project to PHP 8.2 and Symfony 6.3. The error message I'm getting is:
Undefined constant "ChartMogul\DEFAULT_MAX_RETRIES"
I managed to work around the issue by moving the constant inside the Configuration class like this:
<?php
namespace ChartMogul;
/**
*
* @codeCoverageIgnore
*/
class Configuration
{
const DEFAULT_MAX_RETRIES = 20;
/**
* @var null|Configuration
*/
private static $defaultConfiguration = null;
/**
* @var string
*/
private $accountToken = '';
/**
* @var string
*/
private $secretKey = '';
/**
* @var int
* maximum retry attempts
*/
private $retries = self::DEFAULT_MAX_RETRIES;
/**
* Creates new config object from accountToken and secretKey
* @param string $accountToken
* @param string $secretKey
*/
public function __construct($accountToken = '', $secretKey = '', $retries = self::DEFAULT_MAX_RETRIES)
{
$this->accountToken = $accountToken;
$this->secretKey = $secretKey;
$this->retries = $retries;
}
Related with PR #112
Currently, it is very easy to call the static methods.
$c1 = Customer::findByExternalId($extId1);
$c2 = Customer::findByExternalId($extId2);
But since we can inject own http client, it becomes cumbersome to call the static api of this lib.
$client = createCustomClient();
$c1 = Customer::findByExternalId($extId1, $client);
$c2 = Customer::findByExternalId($extId2, $client);
note: hehe actually looking at the code, findByExternalId
currently does not even support passing the client as second argument which even makes an inconsistent and partially inaccessible api.
Let me even explain why I believe passing a custom client instance (even if the default is passed) is better and should be the prefered if not the only option.
See the first snippet, see how the client is never passed to the static methods? When it is not passed it undergoes the entire client building process again and again on every call to the static api, which is of course very ineffective.
Let's see how this can be implemented obeying the single responsibility and dependency inversion principles:
$client = createCustomClient();
$customers = new CustomersService($client); // or pass no client and default may be built, but personally i would always inject
$c1 = $customers->findByExternalId($extId1);
$c2 = $customers->findByExternalId($extId2);
See how we removed the client argument from all methods that were formerly static and part of a class that should only be responsible for holding a single customer data.
Of course this makes a completley new api and lot has to be refactored in both the lib and its consumers. However there is quite a smooth migration path:
First create the service classes and delegate to the static api while also deprecating the static api:
class CustomersService
{
public function __construct(private ClientInterface $client) {}
public function findByExternalId(string $externalId): ?Cursomer
{
return Customer::findByExternalId($externalId, $this->client);
}
}
class Customer
{
/** @deprecated use CustomersService */
public function findByExternalId(string $externalId, ?ClientInterface $client = null): self
{
if ($client === null) {
trigger_user_error(E_USER_DEPRECATED, '...');
}
// previous implementation
}
}
Then it can be released allowing consumers to change their code.
And then you can continue working on moving the logic to the services themselves while keeping the deprecated method but adding user errors
class Customer
{
/** @deprecated use CustomersService */
public function findByExternalId(string $externalId, ?ClientInterface $client = null): self
{
trigger_user_error(E_USER_DEPRECATED, '...');
return (new CustomersService($client))->findByExternalId($externalId);
}
}
This all can even be done method by method, service by sevice, not at all needed to do it in one go and it will be minor releases all the time.
But when it is all finished, all logic is in services and all data classes delegate to temporary created service instance, then in one go remove all the deprecated methods and release a new major version. As the minor versions were all emitting deprecations and new way to achieve same results was always possible, consumers have had quite a lot of time for transition and the transition itself is quite straight forward.
As a cherry on top, I propose to also remove any nullable constructors parameters in the services and the client and remove http client discovery so that the end code must look minimally like this:
$psrHttpClient = psrClient(); // always custom
$config = new Configuration(); // this is the only one that has defaults and only scalars
$client = new Client($config, $psrHttpClient, $psrRequestFactory);
for simplification you could keep the client autodiscovery but this should be optional (require-dev in composer) and in a factory class:
class ClientFactory
{
public static function create(
Configuration $config = null,
HttpClient $client = null,
RequestFactoryInterface $requestFactory = null
): ClientInterface {
if (!\class_exists(HttpClientDiscovery::class) || !\class_exists(Psr17FactoryDiscovery::class)) {
throw new \Exception('Please install optional dependency php-http/discovery or pass you own psr client and request factory instances');
}
$config ?: new Configuration();
$client = $client ?: HttpClientDiscovery::find();
$requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory();
return new Client($config, $client, $requestFactory);
}
}
$cus = new ChartMogul\Customer([
"uuid" => $user->uuid
]);
$subscriptions = $cus->subscriptions();
results in:
PHP Fatal error: Uncaught exception 'ChartMogul\Exceptions\ChartMogulException' with message 'Subscription request error has occurred. Response:
<title>We're sorry, but something went wrong (500)</title> <style> body { background-color: #EFEFEF; color: #2E2F30; text-align: center; font-family: arial, sans-serif; margin: 0; }div.dialog {
width: 95%;
max-width: 33em;
margin: 4em auto 0;
}
div.dialog > div {
border: 1px solid #CCC;
border-right-color: #999;
border-left-color: #999;
border-bottom-color: #BBB;
border-top: #B00100 solid 4px;
border-top-left-radius: 9px;
border-top-right-radius: 9px;
background-color: white;
padding: 7px 12% 0;
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
}
h1 {
font-size: 100%;
color: #730E15;
line-height: 1.5em;
}
div.dialog > p {
margin: 0 0 1em;
padding: 1em;
background-colo in /home/bpzadmin/public_html/wp-content/mu-plugins/bookpagez-chart-mogul/chart-mogul/vendor/chartmogul/chartmogul-php/src/Http/Client.php on line 222
The following code trows a 404 if the specified customer doesn't have any subscriptions, if it is just a lead.
$cus = new ChartMogul\Customer([
"uuid" => $user->cm_uuid
]);
$subscriptions = $cus->subscriptions();
I believe AbstractResource::all() should return self[]
as well. It would help with autocomplete and warnings in IDEs.
Updating dependencies
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Root composer.json requires chartmogul/chartmogul-php ^4.0.0 -> satisfiable by chartmogul/chartmogul-php[4.0.0].
- chartmogul/chartmogul-php 4.0.0 requires php ^7.1 -> your php version (8.0.8) does not satisfy that requirement.
It would be useful to be able to cancel a subscription with the subscription UUID rather than having to do it via the Customer class.
From what I can tell this is possible via:
https://dev.chartmogul.com/v1.0/reference#cancel-a-customers-subscription
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.