|
- <?php
-
-
-
- namespace producer\controllers;
-
- use common\models\CreditHistory ;
- use producer\models\CreditForm ;
- use common\models\User ;
-
- class CreditController extends ProducerBaseController
- {
-
-
-
- public function behaviors()
- {
- return [];
- }
-
- public function actions()
- {
- return [
- 'captcha' => [
- 'class' => 'yii\captcha\CaptchaAction',
- 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
- ],
- ];
- }
-
-
-
- public function actionHistory($returnPayment = '')
- {
- $searchModel = new CreditHistorySearch() ;
- $searchModel->id_user = User::getCurrentId() ;
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
-
- $userProducer = UserProducer::searchOne([
- 'id_producer' => $this->getProducer()->id,
- 'id_user' => User::getCurrentId()
- ]) ;
-
- if(strlen($returnPayment)) {
- if($returnPayment == 'success') {
- Yii::$app->getSession()->setFlash('success', "Paiement accepté : votre compte vient d'être crédité.");
- }
- if($returnPayment == 'cancel') {
- Yii::$app->getSession()->setFlash('error', 'Paiement annulé');
- }
- }
-
- return $this->render('history',[
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- 'creditUser' => $userProducer->credit
- ]) ;
- }
-
- public function actionAdd()
- {
- $producer = $this->getProducer() ;
-
- if($producer->online_payment) {
- \Payplug\Payplug::setSecretKey($producer->getSecretKeyPayplug());
-
- $creditForm = new CreditForm ;
-
- if ($creditForm->load(Yii::$app->request->post()) && $creditForm->validate()) {
- $user = User::getCurrent() ;
-
- $payment = \Payplug\Payment::create(array(
- 'amount' => (float) $creditForm->amount * 100,
- 'currency' => 'EUR',
- 'save_card' => false,
- 'customer' => array(
- 'email' => $user->email,
- 'first_name' => $user->name,
- 'last_name' => $user->lastname
- ),
- 'hosted_payment' => array(
- 'return_url' => Yii::$app->urlManagerProducer->createAbsoluteUrl(['credit/history', 'slug_producer' => $producer->slug, 'returnPayment' => 'success']),
- 'cancel_url' => Yii::$app->urlManagerProducer->createAbsoluteUrl(['credit/history', 'slug_producer' => $producer->slug, 'returnPayment' => 'cancel']),
- ),
- 'notification_url' => Yii::$app->urlManagerProducer->createAbsoluteUrl(['credit/payment-notifications', 'idUser' => $user->id, 'slug_producer' => $producer->slug]),
- 'metadata' => array(
- 'id_user' => $user->id
- )
- ));
-
- if($payment) {
- $this->redirect($payment->hosted_payment->payment_url) ;
- }
- }
-
- return $this->render('add', [
- 'creditForm' => $creditForm
- ]) ;
- }
- else {
- throw new \yii\base\UserException('Cette option est désactivée chez ce producteur.');
- }
- }
-
-
-
- public function actionPaymentNotifications($idUser)
- {
- $producer = $this->getProducer() ;
- \Payplug\Payplug::setSecretKey($producer->getSecretKeyPayplug());
-
- $input = file_get_contents('php://input');
- try {
- $resource = \Payplug\Notification::treat($input);
-
- if ($resource instanceof \Payplug\Resource\Payment
- && $resource->is_paid
- ) {
-
- $creditHistory = new CreditHistory;
- $creditHistory->id_user = $idUser;
- $creditHistory->id_user_action = $idUser;
- $creditHistory->id_producer = $producer->id ;
- $creditHistory->type = CreditHistory::TYPE_CREDIT ;
- $creditHistory->comment = '' ;
- $creditHistory->amount = $resource->amount ;
- $creditHistory->mean_payment = MeanPayment::CREDIT_CARD ;
- $creditHistory->save();
-
- } else if ($resource instanceof \Payplug\Resource\Refund) {
- echo 'refund' ;
- }
- }
- catch (\Payplug\Exception\PayplugException $exception) {
- echo 'error' ;
- }
- }
-
- }
|