L
L
lllyx2019-05-09 23:00:05
Yii
lllyx, 2019-05-09 23:00:05

How to print an object in JSON?

An object

class Required3DS
{
    /**
     * @var integer
     */
    protected $transactionId;
    /**
     * @var string
     */
    protected $token;
    /**
     * @var string
     */
    protected $url;
    /**
     * @return integer
     */
/**
     * @param $params
     * @return Required3DS
     */
    public static function fromArray($params)
    {
        $model = new Required3DS();
        $model->setTransactionId($params['TransactionId']);
        $model->setToken($params['PaReq']);
        $model->setUrl($params['AcsUrl']);
        return $model;
    }
}

Calling a Function and Passing an Array
public function chargeCard($amount, $currency, $ipAddress, $cardHolderName, $cryptogram, $params = [], $requireConfirmation = false)
    {
        $endpoint = $requireConfirmation ? '/payments/cards/auth' : '/payments/cards/charge';
        $defaultParams = [
            'Amount' => $amount,
            'Currency' => $currency,
            'IpAddress' => $ipAddress,
            'Name' => $cardHolderName,
            'CardCryptogramPacket' => $cryptogram
        ];
        $response = $this->sendRequest($endpoint, array_merge($defaultParams, $params));
        if ($response['Success']) {
            return Transaction::fromArray($response['Model']);
        }
        if ($response['Message']) {
            throw new RequestException($response);
        }
        if (isset($response['Model']['ReasonCode']) && $response['Model']['ReasonCode'] !== 0) {
            throw new PaymentException($response);
        }

        return Required3DS::fromArray($response['Model']);

    }

The controller itself
if (Yii::$app->request->post('CardCryptogramPacket')){
            $transaction = $client->chargeCard(230, 'RUB', Yii::$app->request->userIP, Yii::$app->request->post('Name'), Yii::$app->request->post('CardCryptogramPacket'), ['Email'=>'[email protected]', 'phone'=>'89178950879'], false);
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            //$transaction = ArrayHelper::toArray($transaction);
            //$transaction = Json::decode($transaction);
            return $transaction;

        }

What emptiness comes to,
vardump issues
object(app\modules\main\module\CloudPayments\Model\Required3DS)#107 (3) { ["transactionId":protected]=> int(143396325) ["token":protected]=> string(234) "+/eyJNZXJjaGFudE5hbWUiOm51bGwsIkZpcnN0U2l4IjoiNDI0MjQyIiwiTGFzdEZvdXIiOiI0MjQyIiwiQW1vdW50IjoyMzAuMCwiQ3VycmVuY3lDb2RlIjoiUlVCIiwiRGF0ZSI6IjIwMTktMDUtMDlUMDA6MDA6MDArMDM6MDAiLCJDdXN0b21lck5hbWUiOm51bGwsIkN1bHR1cmVOYW1lIjoicnUtUlUifQ==" ["url":protected]=> string(33) "https://demo.cloudpayments.ru/acs" }

Please tell me how to output in JSON correctly

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lllyx, 2019-05-10
@lllyx

To print an object, in my case the Required3DS class, you need to convert it to an array, and then to JSON
Convert to JSON and output

Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$json1 = Json::encode($json);
return $json1;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question