How to add additional info into php array - php

I use this php code:
public function checkout(&$order, &$response)
{
$settings = Settings::get($order->seller_id);
$readon= 'maksājums';
$readon= $order->id;
$order->custom([
'payment_details' => [
'bank_name' => [ 'label' => $this->_->_('Bank Name'), 'value' => $settings->bank_name ],
'account_owner' => [ 'label' => $this->_->_('Account Owner'), 'value' => $settings->account_owner ],
//'bic' => [ 'label' => $this->_->_('BIC/SWIFT'), 'value' => $settings->bic ],
'iban' => ['label' => $this->_->_('IBAN'), 'value' => $settings->iban ],
'reason' => [ 'label' => $this->_->_('Reason for Payment'), 'value' => $readon],
],
]);
$response = [];
$response['redirect'] = $this->meta('manual_url');
return Payment::STATUS_OK;
}
Problem is that I want to add additional text on value readon. Right now $readon show only order number, but I want that it display text like "Please make payment for Order No.$order->id"

Just concatenate the string with what you need. Like this,
$readon= "Please make payment for Order No. ".$order->id;

Related

How to make PayPalCheckout return value by post method?

I am using PayPalCheckoutSdk library following the examples, I have the following:
<?php
require __DIR__ . '/PayPalCheckout/vendor/autoload.php';
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
$clientId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$environment = new SandboxEnvironment($clientId, $clientSecret);
$client = new PayPalHttpClient($environment);
$invoiceNumber = uniqid();
$items = array();
$items[0] = [
'name' => 'HTML5',
'description' => 'Video streaming service',
'type' => 'SERVICE',
'sku' => 'sku03',
'unit_amount' =>
[
'currency_code' => 'USD',
'value' => '90.00',
],
'quantity' => '1',
'category' => 'DIGITAL_GOODS',
];
$new_item = [
'name' => 'CSS3',
'description' => 'Video streaming service',
'type' => 'SERVICE',
'sku' => 'sku02',
'unit_amount' =>
[
'currency_code' => 'USD',
'value' => '45.00',
],
'quantity' => '2',
'category' => 'DIGITAL_GOODS',
];
array_push($items , $new_item);
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
$request = new OrdersCreateRequest();
$request->prefer('return=representation');
$request->body = [
'intent' => 'CAPTURE',
'application_context' => [
'brand_name' => 'COMPANY',
'locale' => 'us-US',
'user_action' => 'PAY_NOW',
"cancel_url" => "http://localhost/PayPal/cancel.php",
"return_url" => "http://localhost/PayPal/return.php",
'landing_page' => 'BILLING',
],
'purchase_units' => [0 => [
'reference_id' => $invoiceNumber,
'amount' => [
'currency_code' => 'USD',
'value' => '160.00',
'breakdown' => [
'item_total' => [
'currency_code' => 'USD',
'value' => '180.00',
],
'shipping_discount' => [
'currency_code' => 'USD',
'value' => '20.00',
],
],
],
'items' =>
$items,
]],
];
try {
$response= $client->execute($request);
if ($response->statusCode == 201){
for ($i = 0; $i < count($response->result->links); ++$i){
$link = $response->result->links[$i];
if ($link->rel =='approve') {
header("location: $link->href");
}
}
} else {
exit(1);
}
} catch (HttpException $ex) {
echo $ex->statusCode;
print_r($ex->getMessage());
}
?>
I am receiving the data by get method print_r($_REQUEST);:
Array ( [token] => 3JX899952R0552721 [PayerID] => J95XSJRX4WXVS
And, that information is processed in the file return.php which has the following code: https://ideone.com/ncVjIt
I would like to be able to receive the information but by post method, what configurations should I make so that the data is sent by post and not by get?
As explained in comments, you can't change the redirect method back from PayPal. It will always be a GET string appended to your return_url.
However, the ideal and recommended solution is to not use any redirects. At all. Instead, use the PayPal-Checkout-SDK you have to make two routes on your server, one for 'Create Order' and one for 'Capture Order', documented here, that return only JSON data (no HTML or text). The latter one should (on success) store the payment details in your database before it does the return (particularly purchase_units[0].payments.captures[0].id, the PayPal transaction ID)
Pair these two JSON-only routes with the following approval flow that does not use any redirects, and instead keeps your site loaded in the background (lightboxed) at all times during payment approval: https://developer.paypal.com/demo/checkout/#/pattern/server

DoctrineModule\Form\Element\ObjectSelect - Selected values and auto submit on change

I have the following code of select form:
$this->add([
'name' => 'username',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => [
'object_manager' => $this->getObjectManager(),
'target_class' => 'Application\Entity\Player',
'property' => 'username',
'is_method' => true,
'empty_option' => '-- Please select Unsername --',
'find_method' => [
'name' => 'getUsername',
],
'label_generator' => function ($targetEntity) {
return $targetEntity->getId() ;
},
],
]);
My questions are:
Is it posible selected value to display as default?
(example: https://www.w3schools.com/tags/att_option_selected.asp)
Is it possible to do submit when changing position
Thank in advance for your help

TYPO3 how to add virtual column to the TCA?

how can I add an virtual column to the TCA (TYPO3 8)? I have in a 1:n table with data and I want to display the count of the data in the backend to current element.
I need something like this:
$fields = [
'counts7d' => [
'exclude' => false,
'label' => 'last 7 days',
'config' => [
'type' => 'none',
'procFunc' => '\Namespace\MyClass->MyMethod',
'readOnly' => true,
'params' => [
'period => '7d'
]
]
],
'counts30d' => [
'exclude' => false,
'label' => 'last 30 days',
'config' => [
'type' => 'none',
'procFunc' => '\Namespace\MyClass->MyMethod',
'readOnly' => true,
'params' => [
'period => '30d'
]
]
],
];
pseudo function:
public function myMethod($element, $params){
$sql = "SELECT count(*) FROM TABLE WHERE pid=$element[uid] and date > $params[period]";
return sql_count…
}
The field should only be informative for the backend users.
Does anyone have an idea?
Thanks
Oliver
The TCA field type user is exactly what you are looking for:
'counts7d' => [
'exclude' => false,
'label' => 'last 7 days',
'config' => [
'type' => 'user',
'userFunc' => \Namespace\MyClass::class . '->MyMethod',
'parameters' => [
'period => '7d',
],
],
],
The TCA field type none is exactly what you are looking for. Type none is the only type that does not necessarily need a database field. To manipulate it you can use userFunc which allow you to use custom php function.

Yii2, Set multiple Value Select2

I have load select2 data like this :
$data = ArrayHelper::map(ContactGroups::find()->where(['group_status'=>'ACTIVE'])->asArray()->all(),'group_id', 'group_name');
echo $form->field($model, 'group_id')->widget(Select2::classname(), [
'data' => $data,
'model' => $model,
'language' => 'en',
'options' => ['placeholder' => Yii::t('modules','Pilih Kelompok')],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
],
])->label('Kelompok');
$data variable returning result :
Array
(
[1] => Tanpa Kategori
[3] => Bisnis
[4] => Kawan
[5] => Bisnis Kerang
[6] => Bisnis Selang
[99] => Keluarga
)
and select2 working properly, but I can't show selected value or initial value. is I've missed something ?
you add tags property in pluginOptions for multiple selection like....
$data = ArrayHelper::map(ContactGroups::find()->where(['group_status'=>'ACTIVE'])->asArray()->all(),'group_id', 'group_name');
foreach($data as $d)
$row[]=$d;
echo $form->field($model, 'group_id')->widget(Select2::classname(), [
'language' => 'en',
'name' => 'group_id[]',
'options' => ['placeholder' => ''],
'pluginOptions' => [
'tags' => $row,
'allowClear' => true,
'multiple' => true
],
])->label('Kelompok');
You show Demo
Try using like this.. At the time of updating we need to take already selected values in 1 variable and all values in 1 variable.. and send this to select2.
$query = NewsTags::find()->where(['news_id' => $model->id])->all();
$services = array();
$services_id_list = array();
foreach ($query as $ds) {
$tag_id = $ds->tag_id;
$tag_name = Tags::findOne($tag_id)->tag_name;
$services[$ds->tag_id] = $tag_name;
array_push($services_id_list, $ds->tag_id);
}
$data= ArrayHelper::map(Tags::find()->where([])->all(),'id','tag_name');
echo Select2::widget([
'name' => 'NewsTags[tag_id][]',
'id' => 'newstags-tag_id',
'value' => $services_id_list,
'data' => $data,
'maintainOrder' => true,
'options' => [
'placeholder' => 'Select a Service ...',
'multiple' => true
],
'pluginOptions' => [
'tags' => true,
'maximumInputLength' => 10
],
]);
here NewsTags[tag_id][] is the model and its column. we are not directly calling $model->attribute here
Having a look at the code of kartik\base\InputWidget line 190 :
if ($this->hasModel()) {
$this->name = !isset($this->options['name']) ? Html::getInputName($this->model, $this->attribute) : $this->options['name'];
$this->value = !isset($this->options['value'])? Html::getAttributeValue($this->model, $this->attribute) : $this->options['value'];
}
I've found out that, when loading data with AJAX, initial multiple values should be set in options[value] like this:
<?= $form->field($model, $attribute)->widget(Select2::className(), [
'initValueText' => $initText, // array of text to show in the tag for the selected items
'options' => [
'placeholder' => 'Any text you want ...',
'multiple' => true,
'class' => 'form-control',
'value' => $initIds, // array of Id of the selected items
],
whereas setting value next to initValueText leads to an array_combine error

Change Fieldset Fields' required parameter dynamically

I have a moneyFieldset with 2 fields, amount and currency.
class MoneyFieldset ...
{
public function __construct($name = null, $options = array())
{
parent::__construct($name, $options);
$this->setHydrator(...);
$this->add(array(
'name' => 'currency',
'type' => 'select',
'options' => array(
'value_options' => \Core\Service\Money::getAvailableCurrencies(true),
),
'attributes' => array(
'value' => \Core\Service\Money::DEFAULT_CURRENCY,
),
));
$this->add(array(
'name' => 'amount',
'type' => 'text',
));
}
}
public function getInputFilterSpecification()
{
$default = [
'amount' => [
'required' => false,
'allow_empty' => true,
'filters' => [
['name' => AmountFilter::class]
],
'validators' => [
]
],
'currency' => [
'required' => false,
'allow_empty' => true,
'filters' => [
['name' => StringToUpper::class]
],
'validators' => [
]
]
];
return \Zend\Stdlib\ArrayUtils::merge($default, $this->filterSpec, true);
}
I'm using moneyFieldset in my other fieldsets like this:
// Price Field
$this->add(array(
'name' => 'price',
'type' => 'form.fieldset.moneyFieldset',
'attributes' => array(
'required' => true,
'invalidText' => 'Please type an amount'
),
'options' => array(
...
),
));
When I set filter like this:
function getInputFilterSpecification()
{
'price' => [
'required' => true,
'allow_empty' => false,
],
}
It's not working because price has 2 fields, so how can I say price[amount] and price[curreny] is required?
You can provide the input specifications for the nested fieldset (within the context of the form) using the following array structure.
public function getInputFilterSpecification()
{
return [
// ...
'price' => [
'type' => 'Zend\InputFilter\InputFilter',
'amount' => [
'required' => true,
],
'currency' => [
'required' => true,
]
],
//...
];
}
If you are dynamically modifying the values of the input filter it might be worth considering creating the validator using a service factory class and then attaching it to a form using the object API rather than arrays.
As I said in #AlexP's comment, a field, or a group of field declared as Required like this :
function getInputFilterSpecification()
{
'price' => [
'required' => true,
'allow_empty' => false,
],
}
Not means that it will be print an html like this :
<input type="text" required="required"/>
It just check when you'll do $form->isValid() if your fields are empty and required or other checks.
To achieve that, you just have to set in attributes that you want to require those fields. As you already did. Attributes can add, same as class attribute, html code to an input.
P.S : AlexP's answer is the best answer I just give more info about it.

Categories