Welcome Guest, Not a member yet? Register   Sign In
Openpay LIBRARY
#1

(This post was last modified: 03-12-2020, 09:55 AM by jreklund.)

Hi! Im trying to use Openpay library in CI4 but I don't have success, I don't know if im doing right,
Im new using this framewwork, can any one help me?  Huh

This is my code

//controller
PHP Code:
<?php namespace App\Controllers;

use 
Codeigniter\Controller;
use 
App\Libraries\LibreriaPago;

class 
Demo extends BaseController
{
public function 
index(){


$api = new LibreriaPago;
}


//library
PHP Code:
<?php namespace App\Libraries;

require_once 
APPPATH.'/libraries/openpay/Openpay.php';




class 
LibreriaPago extends Openpay
{
public function 
__construct()
    {
        parent::__construct();
    }


// and content of library looks like

PHP Code:
<?php 


if (!function_exists('curl_init')) {
throw new 
Exception('CURL PHP extension is required to run Openpay client.');
}
if (!
function_exists('json_decode')) {
throw new 
Exception('JSON PHP extension is required to run Openpay client.');
}
if (!
function_exists('mb_detect_encoding')) {
throw new 
Exception('Multibyte String PHP extension is required to run Openpay client.');
}

require(
dirname(__FILE__) . '/data/OpenpayApiError.php');
require(
dirname(__FILE__) . '/data/OpenpayApiConsole.php');
require(
dirname(__FILE__) . '/data/OpenpayApiResourceBase.php');
require(
dirname(__FILE__) . '/data/OpenpayApiConnector.php');
require(
dirname(__FILE__) . '/data/OpenpayApiDerivedResource.php');
require(
dirname(__FILE__) . '/data/OpenpayApi.php');

require(
dirname(__FILE__) . '/resources/OpenpayBankAccount.php');
require(
dirname(__FILE__) . '/resources/OpenpayCapture.php');
require(
dirname(__FILE__) . '/resources/OpenpayCard.php');
require(
dirname(__FILE__) . '/resources/OpenpayCharge.php');
require(
dirname(__FILE__) . '/resources/OpenpayCustomer.php');
require(
dirname(__FILE__) . '/resources/OpenpayFee.php');
require(
dirname(__FILE__) . '/resources/OpenpayPayout.php');
require(
dirname(__FILE__) . '/resources/OpenpayPlan.php');
require(
dirname(__FILE__) . '/resources/OpenpayRefund.php');
require(
dirname(__FILE__) . '/resources/OpenpaySubscription.php');
require(
dirname(__FILE__) . '/resources/OpenpayTransfer.php');
require(
dirname(__FILE__) . '/resources/OpenpayWebhook.php');
require(
dirname(__FILE__) . '/resources/OpenpayToken.php');
require(
dirname(__FILE__) . '/resources/OpenpayPse.php');
?>
Reply
#2

What is not working? Do you have an error message?
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(This post was last modified: 08-25-2020, 11:01 PM by jreklund.)

Hi, I'm new in CI, I'm using version 4.0 and I'm dealing with the same problem, I don't know how to integrate Openpay library in order to use it as a payment gateway.
Here's where Openpay repo is for PHP: https://github.com/open-pay/openpay-php

I created a folder under app/Libraries/Openpay
Inside has the Openpay.php file, data and resources folders

In my cart controller in app/Controllers I added the following line:

use App\Libraries\Openpay;

Adding this line where I need it under the controller:
Openpay::setId('moiep6umtcnanql3jrxp');
I got the error that "Openpay" was not found.

I also tried following the above example and still no luck.
I also I added with composer : composer require openpay/sdk
But I don't know how to include the autoloader

Any guide/help/advice is more than welcome, thanks in advance.
Reply
#4

Hi, while I can't guarantee this will solve your problem, in both cases above (with my limited knowledge of the subject) I can see no mention of configuring the autoload. So CI doesn't probably know where to look.
Someone please correct me if I'm wrong here but the Openpay library is not namespaced so you will have to namespace it yourself in order to use it in CI. Also, it seems there are files int here with multiple classes defined. Not sure if that's gona play nice with CI.

So, once you namespace it, in app/Config/Autoload.php you need to modify the __construct function. Where the $psr4 array is defined, add something like this to the array definition:
'Openpay' => APPPATH . 'ThirdParty/Openpay'
Reply
#5

(08-26-2020, 04:44 AM)Jan Zelenka Wrote: Hi, while I can't guarantee this will solve your problem, in both cases above (with my limited knowledge of the subject) I can see no mention of configuring the autoload. So CI doesn't probably know where to look.
Someone please correct me if I'm wrong here but the Openpay library is not namespaced so you will have to namespace it yourself in order to use it in CI. Also, it seems there are files int here with multiple classes defined. Not sure if that's gona play nice with CI.

So, once you namespace it, in app/Config/Autoload.php you need to modify the __construct function. Where the $psr4 array is defined, add something like this to the array definition:
'Openpay' => APPPATH . 'ThirdParty/Openpay'

Thanks for your quick response Jan, I will start with your suggestions.
Reply
#6

It's hard to be sure, as there are two questions here, but I believe that with CI4 you are better off using the namespaces rather than the older method of directly loading the php files with the require_once function.

The simplest way, is to insert a USE statement at the top of the file, with the path to the library file, such as:

PHP Code:
use path/to/library 

If you are using something like VS Code, then you can have the system insert that line for you when you find an unknown class.

I am not using OpenPay, so cannot give you direct help, but that should point you in the right direction.
Reply
#7

(This post was last modified: 08-30-2020, 02:46 PM by jreklund.)

Hi, I namespaced all the files, I moved all files to one folder and if a file has more than one class I create a separate file for the other classes in order to have 1 class per file.
I have the files in the following folder:
app/Libraries/Openpay
I added the following line at the top of each file under the folder Openpay

PHP Code:
namespace App\Libraries\Openpay;

In app/Config/Autoload.php file

$psr4 
= [
 
'App'        => APPPATH,                // To ensure filters, etc still found,
 
APP_NAMESPACE => APPPATH,                // For custom namespace
 
'Config'      => APPPATH 'Config',
 
'Openpay'        => APPPATH 'Libraries\Openpay'
 
]; 

In my controller I added the following
use App\Libraries\Openpay\Openpay;

And in the function where I need to use it I added the following:
Openpay::setId('mysecretid');
Openpay::setApiKey('mysecretkey');

The problem that I have now it's when I call any of the class/method like:

$customer = $openpay->customers->add($customerData);
$charge = $openpay->charges->create($chargeData);

I get the following error:
Call to undefined method stdClass::create()
Call to undefined method stdClass::add()

I don't know why I'm getting this error and how to fix it.
Do you have an idea what I'm doing wrong or what I need to change?
Thanks for your help.
Reply
#8

(08-30-2020, 02:34 PM)cgowez Wrote: The problem that I have now it's when I call any of the class/method like:

$customer = $openpay->customers->add($customerData);
$charge = $openpay->charges->create($chargeData);

I get the following error:
Call to undefined method stdClass::create()
Call to undefined method stdClass::add()

I don't know why I'm getting this error and how to fix it.
Do you have an idea what I'm doing wrong or what I need to change?
Thanks for your help.
Looks like the $openpay variable is not initialized to an openpay class.
Have you created the $openpay object? From the docs:
PHP Code:
$openpay Openpay::getInstance('MERCHANT_ID''PRIVATE_KEY''COUNTRY_CODE'); 
Reply
#9

I tried both options:
Openpay:ConfusedetId('moiep6umtcnanql3jrxp');
Openpay:ConfusedetApiKey('sk_3433941e467c4875b178ce26348b0fac');
Or
$openpay = Openpay::getInstance('MERCHANT_ID', 'PRIVATE_KEY', 'COUNTRY_CODE');

With same result.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB