[ 'class' => VerbFilter::className(), 'actions' => [ 'stripe-verification' => ['post'], ], ] ]; } public function actions() { return [ 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, ], ]; } public function beforeAction($action) { $this->enableCsrfValidation = false; return parent::beforeAction($action); } /** * Affiche l'historique du crédit client. */ public function actionHistory(string $returnPayment = '') { $producer = $this->getProducerCurrent(); if (\Yii::$app->user->isGuest) { return $this->redirect($this->getUrlManagerFrontend()->createAbsoluteUrl(['site/producer', 'id' => $producer->id])); } $searchModel = new CreditHistorySearch(); $searchModel->id_user = GlobalParam::getCurrentUserId(); $dataProvider = $searchModel->search(\Yii::$app->request->queryParams); $userProducer = $this->getUserProducerManager()->findOneUserProducer($this->getUserCurrent()); if (strlen($returnPayment)) { if ($returnPayment == 'success') { $this->setFlash('success', "Paiement accepté : votre compte vient d'être crédité."); } if ($returnPayment == 'cancel') { $this->setFlash('error', 'Paiement annulé.'); } } return $this->render('history', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'creditUser' => $userProducer->credit ]); } public function actionAdd() { $producer = $this->getProducerCurrent(); if (\Yii::$app->user->isGuest) { return $this->redirect($this->getUrlManagerFrontend()->createAbsoluteUrl(['site/producer', 'id' => $producer->id])); } if ($producer->online_payment || $producer->option_stripe_mode_test) { $creditForm = new CreditForm; if ($creditForm->load(\Yii::$app->request->post()) && $creditForm->validate()) { $user = GlobalParam::getCurrentUser(); \Stripe\Stripe::setApiKey( $this->getProducerManager()->getPrivateKeyApiStripe($producer) ); $checkout_session = \Stripe\Checkout\Session::create([ 'line_items' => [ [ 'price_data' => [ 'currency' => 'eur', 'product_data' => [ 'name' => 'Alimentation crédit', ], 'unit_amount' => (float) $creditForm->amount * 100, ], 'quantity' => 1, ] ], 'payment_method_types' => ['card'], 'mode' => 'payment', 'customer_email' => $user->email, 'client_reference_id' => $user->id, 'payment_intent_data' => [ 'metadata' => [ 'user_id' => $user->id, 'producer_id' => $producer->id ], ], 'success_url' => $this->getUrlManagerProducer()->createAbsoluteUrl( [ 'credit/history', 'returnPayment' => 'success' ] ), 'cancel_url' => $this->getUrlManagerProducer()->createAbsoluteUrl( [ 'credit/history', 'returnPayment' => 'cancel' ] ), ]); header("HTTP/1.1 303 See Other"); header("Location: " . $checkout_session->url); die(); } return $this->render('add', [ 'creditForm' => $creditForm ]); } else { throw new \yii\base\UserException('Cette option est désactivée chez ce producteur.'); } } public function actionStripeVerification() { $orderManager = $this->getOrderManager(); $paymentManager = $this->getPaymentManager(); $producerManager = $this->getProducerManager(); $userManager = $this->getUserManager(); $producer = $this->getProducerCurrent(); $contactProducer = $producerManager->getMainContact($producer); $payload = @file_get_contents('php://input'); $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE']; try { $event = \Stripe\Webhook::constructEvent( $payload, $sig_header, $producerManager->getPrivateKeyEndpointStripe($producer) ); } catch (\UnexpectedValueException $e) { // Invalid payload http_response_code(400); exit(); } catch (\Stripe\Exception\SignatureVerificationException $e) { // Invalid signature http_response_code(400); exit(); } $paymentIntent = $event->data->object; $paymentIntentMetadata = $paymentIntent->metadata; $amount = $paymentIntent->amount / 100; $idUser = $paymentIntentMetadata->user_id; if($idUser) { $user = $userManager->findOneUserById($idUser); if (isset($paymentIntentMetadata->order_id)) { $order = $this->getOrderManager()->findOneOrderById($paymentIntentMetadata->order_id); $orderManager->initOrder($order); $pointSale = $this->getPointSaleManager()->findOnePointSaleById($order->id_point_sale); $distribution = $this->getDistributionManager()->findOneDistributionById($order->id_distribution); } // Handle the event switch ($event->type) { case 'charge.succeeded': $paymentExist = Payment::searchOne([ 'id_user' => $idUser, 'amount' => $amount, ], [ 'conditions' => [ 'date > DATE_SUB(NOW(), INTERVAL 1 MINUTE)' ] ]); if (!$paymentExist) { $paymentManager->creditUser($user, $amount, MeanPayment::CREDIT_CARD, $user); if (isset($order) && $order) { $paymentManager->payOrder($order, $user, true); // client : envoi d'un email de confirmation de paiement /*\Yii::$app->mailerService->sendFromProducer( 'Confirmation de commande', 'paymentOrderConfirm', [ 'amount' => $amount, 'user' => $user, 'producer' => $producer, ], $user->email, $producer );*/ // producteur : mail de confirmation \Yii::$app->mailerService->sendFromSite( 'Confirmation de commande', 'orderConfirmProducer', [ 'order' => $order, 'pointSale' => $pointSale, 'distribution' => $distribution, 'user' => $user, 'producer' => $producer ], $contactProducer->email ); } else { $userProducer = $this->getUserProducerManager()->findOneUserProducer($user); \Yii::$app->mailerService->sendFromProducer( 'Alimentation de votre crédit', 'creditConfirm', [ 'user' => $user, 'userProducer' => $userProducer, 'producer' => $producer, 'amount' => $amount, ], $user->email, $producer ); } } break; case 'charge.failed': // client \Yii::$app->mailerService->sendFromProducer( 'Erreur de paiement', 'paymentError', [ 'amount' => $amount, 'user' => $user, 'producer' => $producer, ], $user->email, $producer ); // producteur if (isset($order) && $order) { \Yii::$app->mailerService->sendFromSite( 'Erreur de paiement', 'paymentErrorProducer', [ 'amount' => $amount, 'user' => $user, 'producer' => $producer, 'order' => $order, 'distribution' => $distribution ], $contactProducer->email, ); } break; // handle other event types default: echo 'Received unknown event type ' . $event->type; } http_response_code(200); } else { http_response_code(400); } die(); } }