balanced / balanced-php Goto Github PK
View Code? Open in Web Editor NEWBalanced API library in php.
License: MIT License
Balanced API library in php.
License: MIT License
Hi there,
I am trying to associate the credit card to an already created customer account. With the below code but get an error as below, can you help me with the right set of files to be downloaded from github or what needs to be done to make this code work. I am using the code sample as presented here:- https://github.com/balanced/balanced-php
“Fatal error: Class 'Balanced\Customer' not found in /opt/lampp/htdocs/Developmentphp/lyfesource/GIT_HUB/balancepayment/addcard.php on line 12”
The code is as below :-
<?php
require(__DIR__ . '/vendor/autoload.php');
Httpful\Bootstrap::init();
RESTful\Bootstrap::init();
Balanced\Bootstrap::init();
Balanced\Settings::$api_key = "bcf65636e9f111e2a74c026ba7cac9da";
$customer = \Balanced\Customer::get("/v1/customers/AC7JHRgcsSiIaGNyRGSdAOWm");
$customer->addCard("v1/marketplaces/TEST-MP6dYW4Jk4VStaXPZCwJ0i5x/cards/CC27i1Rw4LQMfQ59QGYldpk4");
print_r($account);
?>
php example/debit-example.php
Create a card
Fatal error: Uncaught exception 'Balanced\Errors\Error' in /Users/marshall/code/balanced/balanced-php/src/Balanced/Errors/Error.php:42
Stack trace:
#0 /Users/marshall/code/balanced/balanced-php/src/Balanced/Resource.php(24): Balanced\Errors\Error::createFromResponse(Object(Httpful\Response))
#1 [internal function]: Balanced\Resource::convertError(Object(Httpful\Response))
#2 /Users/marshall/code/balanced/balanced-php/vendor/matthewfl/restful/src/RESTful/Client.php(70): call_user_func('Balanced\Resour...', Object(Httpful\Response))
#3 /Users/marshall/code/balanced/balanced-php/vendor/matthewfl/restful/src/RESTful/Client.php(34): RESTful\Client->_op(Object(Httpful\Request))
#4 /Users/marshall/code/balanced/balanced-php/vendor/matthewfl/restful/src/RESTful/Collection.php(36): RESTful\Client->post('/cards', Array)
#5 /Users/marshall/code/balanced/balanced-php/example/debit-example.php(19): RESTful\Collection->create(Array)
#6 {main}
thrown in /Users/marshall/code/balanced/balanced-php/src/Balanced/Errors/Error.php on line 42
I really like the Balanced business and would like to integrate it with my current software. Having a custom library specifically designed for CodeIgniter use would be really great.
The file containing Error class should be moved to Balanced\Errors\Error.php
This file is currently under Balanced folder and the file is called Errors.php
This is not a bug. But it makes it difficult for autoloaders to load the class using the namespace as path and class name as file name.
From balanced/balanced-api#443 by @jpxavier
We have a new Customer resource.
With a customer you should be able to debit
, credit
, as well as add_card
and add_bank_account
. A customer should be able to tell you their active_card
and active_bank_account
.
Create an example which shows:
php example/events-and-callbacks.php
Create a callback
Notice: Undefined property via __get(): uri in /Users/marshall/code/balanced/balanced-php/example/events-and-callbacks.php on line 37 in /Users/marshall/code/balanced/balanced-php/vendor/matthewfl/restful/src/RESTful/Resource.php on line 80
The callback:
Notice: Undefined property via __get(): uri in /Users/marshall/code/balanced/balanced-php/example/events-and-callbacks.php on line 45 in /Users/marshall/code/balanced/balanced-php/vendor/matthewfl/restful/src/RESTful/Resource.php on line 80
The customer:
Create a Card
Fatal error: Uncaught exception 'Balanced\Errors\Error' in /Users/marshall/code/balanced/balanced-php/src/Balanced/Errors/Error.php:42
Stack trace:
#0 /Users/marshall/code/balanced/balanced-php/src/Balanced/Resource.php(24): Balanced\Errors\Error::createFromResponse(Object(Httpful\Response))
#1 [internal function]: Balanced\Resource::convertError(Object(Httpful\Response))
#2 /Users/marshall/code/balanced/balanced-php/vendor/matthewfl/restful/src/RESTful/Client.php(70): call_user_func('Balanced\Resour...', Object(Httpful\Response))
#3 /Users/marshall/code/balanced/balanced-php/vendor/matthewfl/restful/src/RESTful/Client.php(34): RESTful\Client->_op(Object(Httpful\Request))
#4 /Users/marshall/code/balanced/balanced-php/vendor/matthewfl/restful/src/RESTful/Collection.php(36): RESTful\Client->post('/cards', Array)
#5 /Users/marshall/code/balanced/balanced-php/example/events-and-callbacks.php(53): RESTful\Collection->create(Array)
#6 {main}
thrown in /Users/marshall/code/balanced/balanced-php/src/Balanced/Errors/Error.php on line 42
When a card is declined (for example, by attempting a debit on 4444444444444448), a card-declined error is returned. However, the exception that is thrown is an HTTPError, rather than something that might make more sense (such as, Declined).
You get an uncaught exception when you pass in 0 for debit amount, not a huge deal but the error was referencing the HTTPError class and it took me a while to find it, here is the stack trace:
Location: /vagrant/bundles/balanced/src/Balanced/Resource.php on line 27</pre>
Stack Trace:
#0 [internal function]: Balanced\Resource::convertError(Object(Httpful\Response))
#1 /vagrant/bundles/restful/src/RESTful/Client.php(64): call_user_func('Balanced\Resour...', Object(Httpful\Response))
#2 /vagrant/bundles/restful/src/RESTful/Client.php(32): RESTful\Client->_op(Object(Httpful\Request))
#3 /vagrant/bundles/restful/src/RESTful/Collection.php(35): RESTful\Client->post('/v1/marketplace...', Array)
#4 /vagrant/bundles/balanced/src/Balanced/Account.php(129): RESTful\Collection->create(Array)
#5 /vagrant/application/controllers/contribution.php(79): Balanced\Account->debit(0, '[Removed]', '[Removed]', NULL, '/v1/marketplace...')
#6 [internal function]: Contribution_Controller->action_create()
#7 /vagrant/laravel/routing/controller.php(325): call_user_func_array(Array, Array)
#8 /vagrant/laravel/routing/controller.php(285): Laravel\Routing\Controller->response('create', Array)
#9 /vagrant/laravel/routing/controller.php(165): Laravel\Routing\Controller->execute('create', Array)
#10 /vagrant/laravel/routing/route.php(153): Laravel\Routing\Controller::call('contribution@cr...', Array)
#11 /vagrant/laravel/routing/route.php(124): Laravel\Routing\Route->response()
#12 /vagrant/laravel/laravel.php(167): Laravel\Routing\Route->call()
#13 /vagrant/public/index.php(34): require('/vagrant/larave...')
#14 {main}
I realize that there is a "0" clearly in the debit call and that's how I found it in the end but I didn't notice that till I had thoroughly looked over the Resource class for issues and the HTTPError class.
The line in question was (Line 27 of Resource)
$error = new HTTPError($response);
It's unclear that to run examples they need to be moved to a directory above where composer installs everything ("vendor"). Some instructions on running the examples would clear this up.
The API allows for the deletion of Customers "so long as there has been no activity associated with it such as creating a hold, credit, or debit"
This ability needs to be added.
Seems like balanced is now on version 0.7.4 however, it's not showing up in https://packagist.org/packages/balanced/balanced yet. So perhaps it was forgotten about.
Composer is only required to install the HTTP library.
Are any of these possible:
Line 123 of Balanced/Account.php and then again on line 128
Shouldn't be any negative side effects but just pointing it out so it's not missed.
Right now the method signature is:
public static function configure($url_root, $api_key)
{
self::$url_root= $url_root;
self::$api_key = $api_key;
}
This is not intuitive. The more intuitive method signature should be
public static function configure($api_key, $url_root)
{
self::$url_root= $url_root;
self::$api_key = $api_key;
}
It turns out that Balanced\Settings::configure
isn't used anywhere in the code, so it's a non-breaking change.
The only thing I’m doing is \Balanced\Debit::get($debit_uri);
. Everything works perfectly on my local machine through MAMP.
An error occurred in /var/app/current/extensions/balanced/lib/balanced/balanced/src/Balanced/Resource.php around line 27
22 {
23 if (property_exists($response->body, 'category_code') &&
24 array_key_exists($response->body->category_code, Error::$codes))
25 $error = new Error::$codes[$response->body->category_code]($response);
26 else
27 $error = new HTTPError($response);
28 return $error;
29 }
30
31 public static function getClient()
Backtrace
[:0]
Balanced\Resource::convertError();
[/var/app/current/extensions/balanced/lib/bninja/restful/src/RESTful/Client.php:69]
call_user_func();
[/var/app/current/extensions/balanced/lib/bninja/restful/src/RESTful/Client.php:24]
RESTful\Client->_op();
[/var/app/current/extensions/balanced/lib/bninja/restful/src/RESTful/Resource.php:158]
RESTful\Client->get();
[/var/app/current/workspace/events/event.reservations_cron_expired.php:145]
RESTful\Resource::get();
Sort of a continuation of #7 but a little different.
The solution imposed by mjallday works, however I am wondering if it's possible to do it from the account side.
I can get cards from an account's card URI:
// Get a single record based on the e-mail provided
$buyer = $mp->accounts->query()->filter(Balanced\Account::$f->email_address->eq($email))->one();
// Get all of the buyer's information based on their URI
$info = Balanced\Account::get($buyer->uri);
// Load up their CC info
$cards = Balanced\Card::get($info->cards->uri);
However, this only provides a limit of 10 by default. While this is substantial for production environments, when it comes to developing it can be a little bit of a pain because you can have 15 invalid cards out of 20, and not be able to get the 5 good ones because it only returns 10.
I know there's a limit() option, but I haven't found the proper way to call that yet.
While what mjallday provided does work, the extra bandwidth and stress of having to cycle through every credit card in the marketplace, not just for a single buyer, really cuts down on performance.
All debit list with pagination (number pagination with previous or next )
debits->query()->all(); echo ''; foreach($debits as $debit) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "Name | Acount ID | Statement | Ammount | Transaction Number | |
---|---|---|---|---|---|
".$debit->account->email_address." | ".$debit->source->name." | ".$debit->account->id." | ".$debit->id." |
we want Pagination like this:
Previous 1 2 3 4 5 6 Next
An example of how to handle a failed charge would be great.
e.g.
try {
# new debit
$debit = $account->debit($amount);
}
catch (Balanced\Exceptions\HTTPError $e) {
// document all errors that can occur here.
if ($e->category_code == 'some-sort-of-error') {
}
}
It seems updating customer information is not well documented. Also, it was not implemented in the PHP client library. One user asked this on IRC
if you run debit or hold with a description of more than 22 characters, it fails. I'd think that this might just ignore the extra characters, or possibly be longer?
given the dynamic nature of the descriptions marketplace users will be submitting (merchant names, descriptions, phone numbers of various lengths), this could cause problems for unsuspecting marketplaces in production.
If you look at the source, the createBuyer
method does not accept a name parameter.
The workaround is to call the createBuyer
method, update the name parameter (account->name = "blah"
) and then call account->save()
to update this subsequently but it would be better if it accepted a name parameter in the original method.
Given the payload
{
"debits": [
{
"amount": 5000,
"appears_on_statement_as": "BAL*Statement text",
"created_at": "2014-02-27T20:03:06.176697Z",
"currency": "USD",
"description": "Some descriptive text for the debit in the dashboard",
"failure_reason": null,
"failure_reason_code": null,
"href": "/debits/WD1l5vYA21ZP24LMQMqHizea",
"id": "WD1l5vYA21ZP24LMQMqHizea",
"links": {
"customer": null,
"dispute": null,
"order": null,
"source": "CC1diaA1Lunr4RlelwKQ8C4v"
},
"meta": {},
"status": "succeeded",
"transaction_number": "W347-910-8642",
"updated_at": "2014-02-27T20:03:07.629780Z"
}
],
"foos": [
{
}
],
"links": {
"debits.customer": "/customers/{debits.customer}",
"debits.dispute": "/disputes/{debits.dispute}",
"debits.events": "/debits/{debits.id}/events",
"debits.order": "/orders/{debits.order}",
"debits.refunds": "/debits/{debits.id}/refunds",
"debits.source": "/resources/{debits.source}"
}
}
Make sure that this client library does not choke and return the wrong object.
The api now supports setting the default card and bank account by PUTing to the source_uri and destination_uri. Then the default source will be represented by the value of source_uri and destination_uri.
https://github.com/balanced/balanced-php/blob/master/src/Balanced/Customer.php#L74
When I try to run the examples I get the following:
php example/example.php
Create a new API key
Our secret is ak-test-1MnAgpuLB2ytTp4IrBVVDUEVnBc0l9ygT
Configure with API key secret ak-test-1MnAgpuLB2ytTp4IrBVVDUEVnBc0l9ygT
Create a marketplace for the new API key secret
Notice: Undefined property via __get(): uri in /Users/marshall/code/balanced/balanced-php/example/example.php on line 25 in /Users/marshall/code/balanced/balanced-php/vendor/matthewfl/restful/src/RESTful/Resource.php on line 80
Balanced\Marketplace::mine():
Marketplace name: Test Marketplace
Changing marketplace name to TestFooey
Marketplace name is now TestFooey
Create a card
Fatal error: Uncaught exception 'Balanced\Errors\Error' in /Users/marshall/code/balanced/balanced-php/src/Balanced/Errors/Error.php:42
Stack trace:
#0 /Users/marshall/code/balanced/balanced-php/src/Balanced/Resource.php(24): Balanced\Errors\Error::createFromResponse(Object(Httpful\Response))
#1 [internal function]: Balanced\Resource::convertError(Object(Httpful\Response))
#2 /Users/marshall/code/balanced/balanced-php/vendor/matthewfl/restful/src/RESTful/Client.php(70): call_user_func('Balanced\Resour...', Object(Httpful\Response))
#3 /Users/marshall/code/balanced/balanced-php/vendor/matthewfl/restful/src/RESTful/Client.php(34): RESTful\Client->_op(Object(Httpful\Request))
#4 /Users/marshall/code/balanced/balanced-php/vendor/matthewfl/restful/src/RESTful/Collection.php(36): RESTful\Client->post('/cards', Array)
#5 /Users/marshall/code/balanced/balanced-php/example/example.php(48): RESTful\Collection->create(Array)
#6 {main}
thrown in /Users/marshall/code/balanced/balanced-php/src/Balanced/Errors/Error.php on line 42
I followed the install instructions https://github.com/balanced/balanced-php#composer
source_uri is not accessible via customer. Appears to be a bug upstream in RESTFul.
The meta and dscription params should be switched in the debit function in Customer.php. In all other functions and the example, https://docs.balancedpayments.com/current/api.html?language=php#create-a-new-debit 'description' is before the meta.
public function debit(
$amount = null,
$appears_on_statement_as = null,
$meta = null,
$description = null,
$source = null,
$on_behalf_of = null)
{
When trying to charge a card using $buyer->debit, if you pass the card URI instead of the account URI an error is generated instead of an exception.
Sometimes, one might want to wrap some statements in a try/catch and have a catch all. This should be possible by catching from a base exception.
raised by: https://support.balancedpayments.com/tickets/1238.
$debit = $card->debit(123);
vs
$debit = $card->account->debit(123)
This method has a weird return -
https://github.com/balanced/balanced-php/blob/master/src/Balanced/Account.php#L194
Also, this method is not fully covered by tests
I am trying to create a customer and I cannot figure out how to create a customer. The method on the docs, https://docs.balancedpayments.com/current/api.html?language=php#creating-a-customer, is not defined.
Method defined in docs:
$customer = Balanced\Customer::mine()->createCustomer();
Method in the customers class also does not work
$customer = new Balanced\Customer(array());
/**
* Represent a buyer or merchant within your marketplace.
*
* You create these using new Balanced\Customer.
*
* <code>
* $customer = new Customer(array(
* "name" => "John Lee Hooker",
* "twitter" => "@balanced",
* "phone" => "(904) 555-1796",
* "meta" => array(
* "meta can store" => "any flat key/value data you like",
* "github" => "https://github.com/balanced",
* "more_additional_data" => 54.8
* ),
* "facebook" => "https://facebook.com/balanced",
* "address" => array(
* "city" => "San Francisco",
* "state" => "CA",
* "postal_code" => "94103",
* "line1" => "965 Mission St",
* "country_code" => "US"
* ),
* "business_name" => "Balanced",
* "ssn_last4" => "3209",
* "email" => $email_address,
* "ein" => "123456789"));
* </code>
*/
I have obtained the latest balanced php version.
I am getting the following error whenever I try to use the balanced php api.
Unexpected exception of type [RESTful\Exceptions\HTTPError] with message [] in balanced/balanced/src/Balanced/Resource.php line 27]
My code has worked in the past so I am not sure what is happening. I just re downloaded the code from Github to see if that would fix the issue but it did not. I am using a test marketplace api key (I also obtained a new test key) so my code is not in production (just a test environment). So far my test cases (with the test bank account and card data provided by balanced) have been to create a bank account and to store a credit card.
Balanced now support ACH Debits. Let PHP folk use them!
The following example does not iterate through the 3 refunds associated with the debit. How do you iterate through a collection in balanced-php?
<?
require('vendor/autoload.php');
Httpful\Bootstrap::init();
Balanced\Bootstrap::init();
$key = new Balanced\APIKey();
$key->save();
Balanced\Settings::$api_key = $key->secret
$marketplace = new Balanced\Marketplace();
$marketplace->save();
#$marketplace = Balanced\Marketplace::mine
$card = $marketplace->cards->create(array(
"card_number" => "5105105105105100",
"expiration_month" => "12",
"expiration_year" => "2015"
));
$buyer = $marketplace->createBuyer("buyer@
$debit = $buyer->debit(1500);
$debit->refund(100);
$debit->refund(100);
$debit->refund(100);
echo $debit->refunds->total() . " refunds
$total = 0;
foreach ($debit->refunds as $r) {
$total += $r->amount;
echo "refund = " . $r->amount;
}
echo $total;
?>
There is no example in balanced-php of how to paginate through a large collection.
If I already have the bank account token (id returned by balanced.js), do I need to do a get to credit the account?
The documentation at:
https://docs.balancedpayments.com/current/api.html?language=php#credit-an-existing-bank-account
says: To credit an existing bank account, you simply pass the amount to the nested credit endpoint of a bank account. The credits_uri is a convenient uri provided so that you can simply issue a POST with the amount and a credit shall be created.
But the example given below it, does a get on bank account first and then a credit.
How do I post to the credits_uri with the amount and other parameters without first doing a get?
The error subclasses need to be in one file per class as per PSR-0.
I know there is a work around in the spl loader to map the sub classes to the parent file but this breaks 3rd party PSR-0 loaders which is how i'v come across the issue
this is weird, can we pass named params instead of all those nulls?
$card = Balanced\Marketplace::mine()->createCard(
null, null, null, null, null,
"5105105105105100",
"123",
"12",
"2020"
);
php example/bank-account-debits.php
Notice: Undefined property via __get(): uri in /Users/marshall/code/balanced/balanced-php/example/bank-account-debits.php on line 27 in /Users/marshall/code/balanced/balanced-php/vendor/matthewfl/restful/src/RESTful/Resource.php on line 80
The BankAccount:
Fatal error: Call to undefined method Balanced\Customer::addBankAccount() in /Users/marshall/code/balanced/balanced-php/example/bank-account-debits.php on line 37
Would it be possible for you Balanced to provide a complete implementation of the API? I am confused how to connect certain parts so that they work together...
It would be great to have more docs on this step.
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.