0

Using yii2 trying make success callback for payments. Callback work, but i need make changes for order. In my common/congig/main.php:

'successCallback' => function($invoice) {
    $order = \common\models\Checkout::findOne($invoice->order_id);
    $order->payment_status = 1;
    $order->update();
}

$invoice->order_id; receives current order id, i need change payment status for checkout model.

Update:

can I somehow run it recursively? for example, if I have several records with one ID?

3
  • What's not working? your code seems to be ok Commented May 12, 2022 at 14:15
  • error Creating default object from empty value in line $order->payment_status = 1; Commented May 12, 2022 at 14:48
  • Can you tell me what's the Checkout's ID column in the database so I can give you an answer? Commented May 12, 2022 at 14:53

1 Answer 1

2

The problem in your code is that findOne() requires the name of the column to compare the value, in this case, the Checkout's ID column.

Assuming it's order_id like in invoice table.

The code will be like this:

'successCallback' => function($invoice) {
    $order = \common\models\Checkout::findOne(['order_id' => $invoice->order_id]);
    $order->payment_status = 1;
    $order->update();
}

Replace 'order_id' with the checkout's id column if it has a different name.

Update To update multiple records you could do something like this:

'successCallback' => function($invoice) {
    \common\models\Checkout::updateAll(['payment_status'=>1],['order_id' => $invoice->order_id]);
}

Make a DB backup before testing this code.

Sign up to request clarification or add additional context in comments.

5 Comments

Ur solution give error too Creating default object from empty value
What's the name of the checkout column? How are you using the success callback? what does invoice get as value?
I think I understood, I had a wrong selection from the database, can I somehow run it recursively? for example, if I have several records with one ID?
It if helps, remember to mark the answer. If you have multiple records you can use updateAll, i can update the answer by having the column name and if you update the question
updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.