failure iyzico payment method in laravel 8 - php

I have installed Iyzico payment method by Composer and I followed each step in the documentation but
I Am Trying To Load Default Form Of Iyzico Payment
But It's Show Me That Failure Status When i am reloading the page it is just Dispaly the
odeme sayfa and Failure
so how can i do it please
here is my code in controller
public function payment()
{
$iyzico = new Iyzico();
$payment = $iyzico->setForm([
'conversationID' => '123456789',
'price' => '120.0',
])
->paymentForm();
$paymentContent = $payment->getCheckoutFormContent();
$paymentStatus = $payment->getStatus();
return view(
'iyzico',
compact('paymentContent', 'paymentStatus')
);
}
here is iyzico class code
<?php
namespace App\Libraries;
use PhpParser\Node\Expr\Cast\Array_;
class Iyzico
{
protected $options;
protected $request;
protected $basketItems;
public function __construct()
{
$this->options = new \Iyzipay\Options();
$this->options->setApiKey("your api key");
$this->options->setSecretKey("your secret key");
$this->options->setBaseUrl("https://sandbox-api.iyzipay.com");
$this->basketItems = [];
}
public function setForm(array $params)
{
$this->request = new \Iyzipay\Request\CreateCheckoutFormInitializeRequest();
$this->request->setLocale(\Iyzipay\Model\Locale::TR);
$this->request->setConversationId($params['conversationID']);
$this->request->setPrice($params['price']);
return $this;
}
public function setBuyer(array $params)
{
$buyer = new \Iyzipay\Model\Buyer();
$buyer->setId($params['id']);
$buyer->setName($params['name']);
return $this;
}
public function setShipping(array $params)
{
$shippingAddress = new \Iyzipay\Model\Address();
$shippingAddress->setContactName($params['name']);
$this->request->setShippingAddress($shippingAddress);
return $this;
}
public function setBilling(array $params)
{
$billingAddress = new \Iyzipay\Model\Address();
$billingAddress->setContactName($params['name']);
$this->request->setBillingAddress($billingAddress);
return $this;
}
public function setItem(array $items)
{
foreach ($items as $key => $value) {
$basketItem = new \Iyzipay\Model\BasketItem();
$basketItem->setId($value['id']);
$basketItem->setName($value['name']);
array_push($this->basketItems, $basketItem);
}
$this->request->setBasketItems($this->basketItems);
return $this;
}
public function paymentForm()
{
$form = \Iyzipay\Model\CheckoutFormInitialize::create($this->request, $this->options);
return $form;
}
}
here is my iyzico.blade.php page
<article>
<h1>odeme sayfa</h1>
<div>
{{ $paymentContent }}
{{ $paymentStatus }}
<div id="iyzipay-checkout-form" class="responsive"></div>
</div>
</article>

Related

Invalid Value provided for RegionId field in Magento 2

I am getting this error while the controller run -'Invalid value of "491" provided for the regionId field.'. I want to transfer the telephone number from the billing address to the customer mobile number field and for that I have built this controller
My controller code is :
<?php
namespace [Vendor]\[Extension]\Controller\Adminhtml\Index;
use [Vendor]\[Extension]\Helper\Data;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
class Update extends Action
{
protected $_customerRepoInterface;
protected $_customerFactory;
public function __construct(
Context $context,
Data $helper,
JsonFactory $resultJsonFactory,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepoInterface,
\Magento\Customer\Model\CustomerFactory $customerFactory
)
{
$this->resultJsonFactory = $resultJsonFactory;
$this->helper = $helper;
$this->_customerRepoInterface = $customerRepoInterface;
$this->_customerFactory = $customerFactory;
parent::__construct($context);
}
public function execute()
{
$result = $this->resultJsonFactory->create();
$customerCollectoin = $this->_customerFactory->create()->getCollection()
->addAttributeToSelect("*")
->load();
foreach ($customerCollectoin as $customer){
if($customer->getPrimaryBillingAddress()) {
if ($customer->getPrimaryBillingAddress()->getTelephone()) {
$telephone = $customer->getPrimaryBillingAddress()->getTelephone();
$customerObj = $this->_customerRepoInterface->getById($customer->getId());
$customerObj->setCustomAttribute('mobilenumber', $telephone);
$this->_customerRepoInterface->save($customerObj);
}
}
}
return $result->setData( count($customerCollectoin));
}
protected function _isAllowed()
{
return true;
}
}
~~Thank you in advance!
enter code here

How can I get the variable in the model for the controller?

I have a $send variable in my model. I want to use it on the controller. But I get an error. How can I call it to the controller?
Model:
public function register_user($send)
{
if($this->emailVerify()) {
$send = array(
'tipo' => 'Error.'
);
return true;
} else {
return false;
}
Controller:
public function __construct()
{
parent::__construct();
$this->send;
}
public function register()
{
$this->load->model('users');
$this->users->register_user();
$this->load->view('signup', $this->send);
}
You can declare a private variable, say send in your model and make getter and setter in your model class in an encapsulated way to get the value in the controller, like below:
Snippet:
Model:
<?php
class Yourmodel extends CI_Model{
private $send;
function __construct() {
parent::__construct();
$this->send = [];
}
public function register_user($send){
if($this->emailVerify()) {
$this->send = array(
'tipo' => 'Error.'
);
return true;
}
return false;
}
public function setSendValue($value){
$this->send = $value;
}
public function getSendValue(){
return $this->send;
}
}
Controller:
<?php
class Controller extends CI_Controller{
private $send;
public function __construct(){
parent::__construct();
$this->send = [];
}
public function register(){
$this->load->model('users');
if($this->users->register_user()){
$this->send = $this->users->getSendValue();
}
$this->load->view('signup', $this->send);
}
}
Replace your code modal and controller as follows:
You need not to declare $send in modal definition, as you are not passing any value while calling the same modal function.
modal positive return can be array $send itself
Catch value of modal function
Modal :
public function register_user()
{
if($this->emailVerify()) {
$send = array(
'tipo' => 'Error.'
);
return $send;
} else {
return false;
}
Controller:
public function __construct()
{
parent::__construct();
$this->send;
}
public function register()
{
$this->load->model('users');
$send = $this->users->register_user();
//print_r($send); // You will get data here
$this->load->view('signup', $this->send);
}
Model
public function register_user($send = "")
{
if($this->emailVerify()) {
$send = array(
'tipo' => 'Error.'
);
return $send;
} else {
return false;
}
Controller
public function __construct()
{
parent::__construct();
$this->send;
}
public function register()
{
$this->load->model('users');
$sendRes = $this->users->register_user(); //now you can use this $send response variable
$this->load->view('signup', $this->send);
}

Call to a member function createData() on null using middleware on Laravel Transformers

So i created a controller for authentication with 2 methods (token() / native)_). Im using fractal transformer to return response. The token method works fine for me, but the loginAndroid() returns
"Call to a member function createData() on null" error.
Any help? Thank you.
class AuthController extends RestController
{
protected $transformer = UserTransformers::Class;
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login', 'loginAndroid']]);
}
public function login(Request $request)
{
$credentials = $request->only(['username', 'password']);
if (!$token = auth()->attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token);
}
public function loginAndroid(Request $request)
{
$credentials = $request->only(['username', 'password']);
if (Auth::attempt($credentials)) {
//$user = Auth::user()->with(['employees']);
$userdata = User::with(['employees', 'employees.role', 'employees.branch'])->find(Auth::id());
//$success['token'] = $user->createToken('MyApp')->accessToken;
//return response()->json($userdata, 200);
//return $userdata;
$response = $this->generateItem($userdata);
return $this->sendResponse($response, 201);
} else {
return response()->json('gagal', 401);
}
}
}
this is my restcontroller
abstract class RestController extends Controller
{
protected $manager;
protected $transformer;
public function __construct()
{
$this->manager = new Manager();
}
protected function generateItem($model, $transformer = null)
{
if (!is_null($transformer)) {
return new Item($model, new $transformer);
}
return new Item($model, new $this->transformer);
}
protected function generateCollection($model, $transformer = null)
{
if (!is_null($transformer)) {
return new Collection($model, new $transformer);
}
return new Collection($model, new $this->transformer);
}
protected function sendResponse(ResourceInterface $data, $status = 200)
{
return response()->json(
$this->manager->createData($data)->toArray(),
$status
);
}
protected function sendNotFoundResponse($status)
{
return response()->json($status, 404);
}
protected function sendIseResponse($status)
{
return response()->json($status, 500);
}
}
It looks like your sendResponse() method depends on $this->manager. However, $this->manager gets set in RestController::__construct() and you've overridden the __construct() method in your AuthController::__construct(). So, in order to have $this->manager available, you should call the parent constructor from your AuthController, like this:
class AuthController extends RestController
{
protected $transformer = UserTransformers::Class;
public function __construct()
{
parent::__construct(); // call the parent constructor where
// $this->manager gets initialized
$this->middleware('auth:api', ['except' => ['login', 'loginAndroid']]);
}
... etc

Yii2 save() creating DB row with default values

I am trying to implement a login method using OpenID, and the $_SESSION var's are posting correctly - and through those I am simply trying to register users in MySQL. For some reason, when passing through the 'login' action in my controller ::
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
include ('../views/user-record/steamauth/userInfo.php');
$steamid = $_SESSION['steam_steamid'];
$username = $_SESSION['steam_personaname'];
$profileurl = $_SESSION['steam_profileurl'];
$avatar = $_SESSION['steam_avatar'];
$avatarmedium = $_SESSION['steam_avatarmedium'];
$avatarfull = $_SESSION['steam_avatarfull'];
$user = UserRecord::findBySteamId($steamid);
if ($user === null)
{
$user = new UserRecord();
$user->steamid = $steamid;
$user->username = $username;
$user->profileurl = $profileurl;
$user->avatar = $avatar;
$user->avatarmedium = $avatarmedium;
$user->avatarfull = $avatarfull;
$user->verified = 0;
$user->banned = 0;
$user->save();
}
Yii::$app->user->login($user, 604800);
return $this->redirect(Yii::$app->user->returnUrl);
}
EDIT: Here is the UserRecord class, forgot to add it in.
<?php
namespace app\models;
class UserRecord extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
public $id;
public $steamid;
public $username;
public $profileurl;
public $avatar;
public $avatarmedium;
public $avatarfull;
public $verified;
public $banned;
public $rank;
public $authKey;
// public $password;
// public $accessToken;
public static function tableName()
{
return 'users';
}
public function getAuthKey()
{
return $this->authKey;
}
public function getId()
{
return $this->id;
}
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
public static function findIdentity($id)
{
return self::findOne($id);
}
public function validateSteamID($steamid)
{
return $this->steamid === $steamid;
}
public static function findIdentityByAccessToken($token, $type = null)
{
throw new \yii\base\NotSupportedException;
}
public static function findBySteamId($steamid)
{
return self::findOne(['steamid' => $steamid]);
}
}
The result is simply a posted row, with none of the data entered.
Any help would be greatly appreciated, thank you.
If you have redefine the same columns name using public vars these override the vars for activeRecord and then are saved only empty value ..
if this you must remove the (redifined) public vars in your model
otherwise if you have no rules then
Try adding safe at the attribute in you model rules
public function rules()
{
return [
[['steamid', 'username', 'profileurl', 'avatar', 'avatarmedium',
'avatarfull', 'verified', 'banned', 'rank', 'authKey',], 'safe'],
];
}
Declaring 'public' variables made the save() ignore the data being posted. Thanks.

Slim3 right way to set errors and check is user logged in

I'm a new user of Slim framework, I've a simple Slim 3 application, with sign in and sign up validation. But I'm not really sure if this is the right/best way to set errors and check if user is logged in -In order to redirect it to his account if session user.id exists.
I used a middleware: AuthMiddleware which includes:
class AuthMiddleware
{
protected $container;
public function __construct($container)
{
$this->container = $container;
}
public function __invoke($request, $response, $next)
{
if (isset($_SESSION['user.id']) && !empty($_SESSION['user.id'])) {
return $response->withRedirect($this->container->router->pathFor('user.index'));
}
$twig = $this->container->view->getEnvironment();
if (isset($_SESSION['validation'])) {
$twig->addGlobal('errors', $_SESSION['validation']['errors']);
$twig->addGlobal('values', $_SESSION['validation']['values']);
unset($_SESSION['validation']);
}
if (isset($_SESSION['auth.signup.success'])) {
$twig->addGlobal('auth_signup_success', $_SESSION['auth.signup.success']);
unset($_SESSION['auth.signup.success']);
}
if (isset($_SESSION['auth.signin.failed'])) {
$twig->addGlobal('auth_signin_failed', $_SESSION['auth.signin.failed']);
unset($_SESSION['auth.signin.failed']);
}
$response = $next($request, $response);
return $response;
}
}
And I used Twig for my views.
Session validation assigned in the validator.php which includes:
class Validator
{
protected $errors = [];
protected $values = [];
public function validate($request, $rules)
{
foreach ($rules as $field => $rule) {
$this->values[$field] = $request->getParam($field);
try {
$rule->setName(ucfirst($field))->assert($request->getParam($field));
} catch (NestedValidationException $e) {
$this->errors[$field] = $e->getMessages()[0];
}
}
if ($this->failed()) {
$_SESSION['validation'] = [
'errors' => $this->errors,
'values' => $this->values,
];
}
return $this;
}
public function failed()
{
return !empty($this->errors);
}
}
Using Respect\Validation. Also, is this the right use of Middlewares?
Thanks in advance.
try creating a separate file for the methods, and calling it from the middleware:
<?php
class AuthMiddleware extends Middleware {
public function __invoke($request, $response, $next) {
if (!$this->container->auth->check()) {
$this->container->flash->addMessage('danger', 'Please sign in to continue.');
return $response->withRedirect($this->container->router->pathFor('auth.signin'));
}
$response = $next($request, $response);
return $response;
}
}
while the Auth class would have those methods to check:
<?php
public function check () {
return isset($_SESSION['user']);
}
public function user() {
if (isset($_SESSION['user'])) {
return User::find($_SESSION['user'])->first();
} else {
return false;
}
}
Don't forget to include the Auth Class within your $app:
<?php
$container['auth'] = function ($container) {
return new \App\Auth\Auth();
};

Categories