0

I am integrating Orange Money Payment gateway with my Yii2 basic Application to be able to receive local payments on this application. In this API, when the user initiates a transaction, I receive some data after sending a curl request to the Orange money API. I store this data in my Database with a key call notif_token. The user is then redirected to orange payment portal where the payment is done. when the User completes a payment process on their portal, they send me json response to a particular url call Notifcation URL. I am suppose to receive this data, update my Database and grant access to this user to some resources.

Everything works well till the level of receiving the feedback in from them through the notification URL.

I have tried all I know to receive this information but to no avail since this action is not an api url. I have written my action as shown below but I do not know what I am missing.( might be a configuration for this action or something).

public function actionOnotification(){
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    $request = \yii::$app->request->post();
    $transaction =OrangeFeedback::findOne(['notif_token'=>$request['notif_token']]);
    $transaction->status = $request['status'];
    $transaction->txnid =  $request['txnid'];
    $transaction->save();
    //do some processing here

}

I do not know how to solve this problem as I feel I am missing a fundamental concept here( might be on how to configure a Yii2 basic application action to receive json data, might be how to convert that action into an API call URL or something which I can not yet figure out). Any help on this will be greatly appreciated as I can't find any resources online to help me.

Nges Brian
  • 479
  • 1
  • 8
  • 22
  • @kusokBanana, My other actions even the action works when I set default values and try to call the action directly. How do I follow by curl?? Here is my controllor structure . The use statements, the class , behaviors and actions – Nges Brian Sep 23 '18 at 12:41

1 Answers1

1

To receive JSON data you need to configure your request component in the config:

'components' => [
    ...
    'request' => [
        'parsers' => [
            'application/json' => 'yii\web\JsonParser',
        ]
    ],
    ...
]

See the docs for more information

Jørgen
  • 3,467
  • 6
  • 33
  • 49
  • 1
    thanks for the response. But I need this data just in one action. Not in my whole application – Nges Brian Sep 24 '18 at 10:08
  • I am just looking at the data this morning and I am wondering wether it is even json data. Because, they state "… and the HTTP notification is posted to the notif_url you sent in the web payment request." POST http://www.merchant-example2.org/notif { "status":"SUCCESS", "notif_token":"dd497bda3b250e536186fc0663f32f40", "txnid": "MP150709.1341.A00073" } . Might be makes more sense now – Nges Brian Sep 24 '18 at 10:11
  • Your other actions should not get requests with the content header `application/json`, so this should not be an issue. What does the `Content-Type` header in the request look like? – Jørgen Sep 24 '18 at 10:23
  • There seems to be no header. That is why I am confused. I am just thinking of using curl but I am limited on how to use curl with an unknown URL. When I use normal $_POST or Yii::$app->request->post(); I do not receive the data – Nges Brian Sep 24 '18 at 10:27
  • I can't seem to find the API documentation for Orange Money, but it seems strange that it does not set the content type header. ` { "status":"SUCCESS", "notif_token":"dd497bda3b250e536186fc0663f32f40", "txnid": "MP150709.1341.A00073" }` does look a lot like json data. How are you checking for headers? Have you done the request in PostMan or similar applications so you can post the entire request? – Jørgen Sep 24 '18 at 10:34
  • That is another Issue, I do not have the URL sending me the notification. that is where I consider it as a post. Unfortunately they grant access to the documentation just to people who have access to develop with their API. – Nges Brian Sep 24 '18 at 10:37
  • Could you update your question with the content of `Yii::$app->request->headers;` from `actionOnotification()`? – Jørgen Sep 24 '18 at 10:40
  • https://stackoverflow.com/questions/52477451/receiving-http-data-in-yii2-action-from-an-unknown-url follow this other question I just asked . Might be better than this one. thanks – Nges Brian Sep 24 '18 at 10:41
  • I can not receive the data. it seems as if my action is not actually executed when transaction is processed. I say so because, when I set default values and execute the action, it works. when I run a transaction which should execute this action, I do not get any updates which might means the action is not executed. – Nges Brian Sep 24 '18 at 11:03