I dont ask many questions as I like to research for myself but this has me stumped.
I have an existing Codeigniter2(CI) application and am trying to integrate an existing API for a payment system (MangoPay). I have added it as a library and also preloaded it in autoload.php, it is being included with no errors.
My question is about setting up the class structure and addressing the class from my application.
Now, if you were to get this working from a plain old PHP file, the code would look like this (and btw it works on my machine with no issue from a plain php file)
<?php
require_once('../vendor/autoload.php');
$mangoPayApi = new MangoPay\MangoPayApi();
$mangoPayApi->Config->ClientId = 'user_id';
$mangoPayApi->Config->ClientPassword = 'password_here';
$mangoPayApi->Config->TemporaryFolder = 'c:\\wamp\\tmp/';
$User = new MangoPay\UserNatural();
$User->Email = "test_natural#testmangopay.com";
$User->FirstName = "Bob";
$User->LastName = "Briant";
$User->Birthday = 121271;
$User->Nationality = "FR";
$User->CountryOfResidence = "ZA";
$result = $mangoPayApi->Users->Create($User);
var_dump($result);
?>
So, I have created a new class in the libraries folder and if i was to var_dump() the contents of mangoPayApi as below, it throws all kinds of stuff which proves that it is working (ie no PHP errors).
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once('/vendor/autoload.php');
class MangoPayService {
private $mangoPayApi;
private $user;
public function __construct()
{
$this->mangoPayApi = new MangoPay\MangoPayApi();
$this->mangoPayApi->Config->ClientId = 'user_id_here';
$this->mangoPayApi->Config->ClientPassword = 'password_here';
$this->mangoPayApi->Config->TemporaryFolder = 'c:\\wamp\\tmp/';
//var_dump($mangoPayApi);
}
I thought I could just write a method in the class like this
function add_user(){
//CREATE NATURAL USER
$this->user = new user();
$user->Email = 'test_natural#testmangopay.com';
$user->FirstName = "John";
$user->LastName = "Smith";
$user->Birthday = 121271;
$user->Nationality = "FR";
$user->CountryOfResidence = "ZA";
$add_userResult = $this->mangoPayApi->Users->Create($user);
var_dump($add_userResult);
}
and acces it in my application like
<?php echo $this->mangopayservice->add_user() ?>
But i get errors Fatal error: Class 'user' not found in C:\wamp\www\mpapp\application\libraries\MangoPayService.php on line 25 (which is this->user = new user(); this line)
Can anyone explain how to correctly set up this scenario and how to integrate correctly with the API.
if I can get something to create a user simply when a page is opened, I think I can work it from there using the solution as a roadmap.
I will be writing all the integration code once I understand how to make this work.
Thank in advance
MangoPay requires a NaturalUser class. You try to instantiate a user class.
Simply replace your first line of the add_user function with :
$user = new MangoPay\UserNatural();
Related
I'm developing Telegram Bot in PHP. At All of my projects i need check user register status and if not registered, store user info. for each project i wrote this step again and again.
Is there any ways to write a user class that usable ( whit some bit change ) in other projects and avoid this repeat?
This is my code.
// File: index.php
$user = User::isRegistered($this->telegram->UserID());
if ($user === false) {
$this->user = User::register($this->telegram);
} else
$this->user = $user;
And this my User class
// File: User.php
class User
{
public static function isRegistered($telegram_id)
{
$user = SlmUsers::find()->where(['telegram_id' => $telegram_id])->one();
if ($user)
return $user;
return FALSE;
}
public static function register(\Telegram $telegram)
{
$nickname = $telegram->FirstName() . ' ' . $telegram->LastName();
$nickname = mb_substr($nickname,0,128);
$user = new SlmUsers();
$user->telegram_id = $telegram->UserID();
$user->nickname = $nickname;
$user->telegram_username = $telegram->Username();
$user->message_id = 0;
$user->updated_at = date('U');
$user->last_request = 'new';
$user->save();
return $user;
}
}
but users table filed are different in each project so for store new user in other projects i should change all field of User::register. how i can avoid this? i want write a class do this works whit the lowest change in the origin class (User.php).
create one class file and define the function which you are going to use repeatedly in this classs file.
Import this file in the file where you want to use this function and call that function using the object of this class file
Im trying to figure out how to call functions based on what a user clicks on a form. But im not sure if im doing it right.
I have a number of classes, lets say 3 for different ways to connect to a site, the user clicks on which one they would like.
FTP
SFTP
SSH
Which i have named 'service' in my code.
I don't want to run a whole bunch of IF statements, i would rather try and build the call dynamically.
What i have at the moment is as follows
$ftp_backup = new FTPBackup;
$sftp_backup = new SFTPBackup;
$ssh_backup = new SSHBackup;
$service = $request->input('service') . '_backup';
$service->testConn($request);
Im getting the following error
Call to a member function testConn() on string
Im not sure im doing this right.
Any help would be greatly appreciated.
Thanks
First of all $service is a string on which You cannot call method, because it is not an object (class instance).
I think it is a great example of where You can use Strategy Pattern which look like that:
class BackupStrategy {
private $strategy = null;
public function __construct($service_name)
{
switch ($service_name) {
case "ftp":
$this->strategy = new FTPBackup();
break;
case "sftp":
$this->strategy = new SFTPBackup();
break;
case "ssh":
$this->strategy = new SSHBackup();
break;
}
}
public function testConn()
{
return $this->strategy->testConn();
}
}
And then in place where You want to call it You call it by:
$service = new BackupStrategy($request->input('service'));
$service->testConn($request);
I suggest You to read about Design Patterns in OOP - it will help You a lot in the future.
How about this:
$ftp_backup = new FTPBackup;
$sftp_backup = new SFTPBackup;
$ssh_backup = new SSHBackup;
$service = $request->input('service') . '_backup';
${$service}->testConn($request);
This is called "Variables variable": http://php.net/manual/en/language.variables.variable.php
// Create class name
$className = $request->get('service') . '_backup';
// Create class instance
$service = new $className();
// Use it as you want
$service->testConn($request);
Before explaining the problem. Let me show the controller function:
public function storePost(IdeaRequest $request)
{
$idea = new Idea();
$idea->idea_title = $request->input('idea_title');
$idea->user_id = $request->input('user_id');
$idea->idea_image = $request->file('idea_image')->move('publicPages\images')->getClientOriginalName();
$idea->idea_info = $request->input('idea_info');
$idea->selection = $request->input('selection');
$idea->idea_location = $request->input('idea_location');
$idea->idea_goal = $request->input('idea_goal');
$idea->idea_description = $request->input('idea_description');
$idea->save();
session()->flash('flash_message', 'Your idea has been submitted for Review');
return back();
}
It stores the image as .tmp file. Things I have tried out
guessExtension(), It just returns the extension name and does not
even store the image.
getClientOriginalName(), it throws an error;
getClientOriginalName method is not defined. I have searched the
method and it is in there. used its namespace
Symfony\Component\HttpFoundation\File\UploadedFile. it did not work
either.
Tried different things out from stackoverflow, Nothing has worked
for me till now. In other words, i have invested a lot of time to
solve this problem but nothing worked. Any help would highly be
appreciated.
Here you go:
public function storePost(IdeaRequest $request)
{
$request->file('idea_image')->move('publicPages\images');
$filename = $request->file('idea_image')->getClientOriginalName();
$idea = new Idea();
$idea->idea_title = $request->input('idea_title');
$idea->user_id = $request->input('user_id');
$idea->idea_image = $filename;
$idea->idea_info = $request->input('idea_info');
$idea->selection = $request->input('selection');
$idea->idea_location = $request->input('idea_location');
$idea->idea_goal = $request->input('idea_goal');
$idea->idea_description = $request->input('idea_description');
$idea->save();
session()->flash('flash_message', 'Your idea has been submitted for Review');
return back();
}
Observe closely. You will have to do this in 2 separate lines:
$request->file('idea_image')->move('publicPages\images');
$filename = $request->file('idea_image')->getClientOriginalName();
Why?
move() returns a File object representing the new file.
The File class doesn't have a getClientOriginalName() method. That method belongs to UploadedFile.
When you chain them, you are trying to access getClientOriginalName() from File, which doesn't exist. Have a look at the docs here.
However...
File extends the PHP native SplFileInfo class which has a getFilename() method. So I guess you could also do:
$request->file('idea_image')->move('publicPages\images')->getFilename();
I have been looking for the help to configure SDK for my Allocator.
I have already followed the steps given in the documentation.
https://github.com/MyAllocator/myallocator-ota-php
From the above documentation I have installed myAllocator using composer and I can see MyAllocator directory inside vendor.
Moving further I tried to copy the code from MaReceiver.php to my controller but this does not work out.
I have also checked how to configure the facebook sdk but this also does not help me to get a good idea to work out with MyAllocator SDK.
Again now tried to created a separate module in zf2 but I did not get any success.
It would be really helpful if anyone can guide me of give me any reference for SDK configuration in zend framework 2.
Please find the code
$request = $this->getRequest();
if ($request->isPost()) {
$data = $request->getPost('id');
$propertyId = $request->getPost('pand_id');
$password = $data['pand_password'];
$guid = empty($data['guid'])? '' : $data['guid'];
$verb = empty($data['verb'])? 'SetupProperty' : $data['verb'];
$booking_id = empty($data['booking_id'])? '' : $data['booking_id'];
$myallocator_pid = empty($data['pid'])? '' : $data['pid'];
// Instantiate backend interface that implements MaInboundInterface
$interface = new MaInboundInterfaceStub();
$interface->mya_property_id = $myallocator_pid;
$interface->ota_property_id = $propertyId;
$interface->verb = $verb;
$interface->guid = $guid;
$interface->shared_secret = 'xxxx';
$interface->ota_regcode = $password;
$interface->booking_id = $booking_id;
$interface->mya_property_id = 'M not in else ';
$router = new \MyAllocator\phpsdkota\src\Api\Inbound\MaRouter($interface);
// Process request
$post_body = file_get_contents('php://input');
$post_body = json_encode($interface);
$response = $router->processRequest($post_body);
header('Content-Type: application/json');
echo json_encode($response);exit;
}
With the above code am successfully able to call the function and work perfectly fine with the value on interface object
But now the problem is that am not able to get the request parameter from MyAllocator
I found a problem that I not sure if is a bug of the php or on my code (probably mine) so let me show you what is happening:
<?php namespace MyApp\Conciliation;
use SimpleExcel\SimpleExcel;
use ForceUTF8\Encoding;
use MyApp\Conciliation\Gol;
class Conciliation {
protected function equalizeFile($file, $providerName)
{
$type = false;
$nfile = 'public'.$file;
// TEST 1: the ideal aproach. not working (see error#1 bellow)
$provider = new $providerName();
// TEST 2: working, getting the correct response
$provider = new Gol();
// TEST 3: working, getting the correct response
$provider = new MyApp\Conciliation\Gol();
$provider->equalize($nfile);
}
Note, the $providerName = 'Gol';
error1
Class 'Gol' not found
http://inft.ly/N8Q6F4B
So, there is any way that I could keeping using variables to instantiate aliases similar as above?
Edit, Problem solved: working example
<?php namespace MyApp\Conciliation;
use SimpleExcel\SimpleExcel;
use ForceUTF8\Encoding;
class Conciliation {
protected function equalizeFile($file, $providerName)
{
$type = false;
$nfile = 'public'.$file;
$providerName = "MyApp\\Conciliation\\".$providerName;
$provider = new $providerName();
$provider->equalize($nfile);
}
http://php.net/manual/en/language.namespaces.dynamic.php
If you are calling the class dynamically, you have to use the full path to the class.
So, your call to equalizeFile should be something like:
equalizeFile("myFile", "MyApp\\Conciliation\\Gol");