this is my edit.phtml file
_objectManager = $objectManager; $customerSession = $objectManager->get('\Magento\Customer\Model\Session'); $urlInterface = $objectManager->get('\Magento\Framework\UrlInterface'); /*if(!$customerSession = $helper -> getCustomersession()) { $customerSession->setAfterAuthUrl($helper->getCurrentCustUrl()); $customerSession->authenticate(); }*/ if(!$customerSession->isLoggedIn()) { $customerSession->setAfterAuthUrl($urlInterface->getCurrentUrl()); $customerSession->authenticate(); } $id = $this->getRequest()->getParam('id'); \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug("myystripe_idedit :-",[$id]); $logger = $this->_objectManager->get('\Psr\Log\LoggerInterface'); $model = $this->_objectManager->get('Milople\Stripe\Model\Payment'); //$model = $helper->getCustcard(); $stripe_id=$model->isStripeCustomer(); \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug("stripe_idddd :-",[$stripe_id]); //$card_details = $block->getCustCreditcardetails($id); $card_details = $model->getCardDetails($id); \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug("myycard_details :-",[$card_details]); $logger->log(100,print_r($card_details,true)); $countryList=$block->getCountries(); $regionList=$block->getRegion(); $baseUrl = $block->getUrl(); ?> address_state ?>"/> stripe/customer/save' id="credit_card_form" method="post" autocomplete="off">last4 ; ?>
Personal Details
name ?>" class="input-text" data-validate="{required:true}" /> exp_month; ?> getCcMonths() as $k=>$v): ?> " selected="selected"> exp_year; ?> getCcYears() as $k=>$v): ?> " selected="selected">Billing Details
Billing Address: address_line1 ?>" class="input-text" data-validate="{required:true}"/> Billing Address Line 2: address_line2 ?>" class="input-text" /> Zip/Postal Code: address_zip ?>" class="input-text required-entry" />" class="input-text" type="text" /> address_state ?>" /> address_country ?>"/> metadata['regionid'] ?>"/> City: address_city ?>" class="input-text required-entry"/> " style="display:none"/> Submit { "#credit_card_form": { "validation": {} } } require(['jquery'],function($){ var carddetails = document.getElementById('card_details').value; $(document).ready(function(){ var selectedCountry= $('#selectedCountry').val(); var selectedState = $('#selectedState').val(); $('#state').val($('#selectedfromStates').val()); $(document).on('change','#country',function changing() { var param = 'country='+$('#country').val(); $.ajax({ showLoader: true, url: 'getCountryAction(); ?>', data: param, type: "GET", dataType: 'json' }).done(function (data) { $('#state').empty(); if(data.value=='') { jQuery('.field.states.required').show(); jQuery('.field.region.required').hide(); } else { $('#state').append(data.value); $('.field.states.required').hide(); $('.field.region.required').show(); $('#state').val(selectedState.slice(0,-1)); } }); }); $('#country').val(selectedCountry); if(selectedCountry!='US') { $('#country').trigger('change'); } var forming = $('#credit_card_form'); $( "#credit_card_form" ).submit(function( event ){ var selectedstate = $('.field.region.required').find(":selected").text(); var selectedcountry = $('.field.country.required').find(":selected"); $('#country').attr('name','credit_card[country]'); if(selectedstate!='') { document.getElementById('states').value=selectedstate; } }); }); });
this is my model file:
_stripepaymentcollection->getCollection()->addFieldToFilter('stripe_customer_id', $stripeId)->getData(); // if($stripe_data != '') // { $this->_logger->log(100,print_r("the stripe data",true)); $this->_logger->log(100,print_r($stripe_data,true)); $this->_logger->log(100,print_r("the stripe data end",true)); $payment_method_id = $stripe_data[0]['stripe_payment_method_id']; $this->_logger->log(100,print_r($payment_method_id,true)); // $stripeId=$stripe_data['stripe_customer_id']; $charge=$this->useSavedcard($payment_method_id,$stripeId,$order,$amount,$iscapture); if(isset($charge->id) && $charge->id != "") { elseif($sessionstripeid!=null) { \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug('vaidehi25'); $charge=$this->useSavedcard($sessionstripeid,$stripeId,$order,$amount,$iscapture); } else { \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug('vaidehi26'); $this->_logger->log(100,print_r("***Else condition***",true)); $this->_logger->log(100,print_r($data,true)); $this->_logger->log(100,print_r($paymentInfo,true)); if(array_key_exists('lastfour_admin',$data)) { \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug('vaidehi35'); if($data['lastfour_admin'] == 'new') { $charge=$this->chargeNormally($order,$payment,$paymentInfo,$amount,$iscapture); } } else { \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug('vaidehi34'); // $milople_expy=$this->session->getCusexpy(); // $milople_expm=$this->session->getCusexpm(); // $milople_cnum=$this->session->getCusnum(); // $milople_ccid= $this->session->getCuscid(); $this->_logger->log(100,print_r("***exp_year Responce***",true)); $this->_logger->log(100,print_r(substr( $this->session->getCusexpy() , -2),true)); //$this->_logger->log(100,print_r(substr( $this->session->getCusexpy() , -2)),true)); //$this->_logger->log(100,print_r(substr( $this->session->getCusexpy() , -2)),true)); $stripe_customer_id = $this->session->getCusId(); $stripe = new \Stripe\StripeClient($this->configProvider->getSecretKey()); $payment_method = $stripe->paymentMethods->create([ 'type' => 'card', 'card' => [ 'number' =>$this->session->getCusnum(), 'exp_month' => $this->session->getCusexpm(), 'exp_year' => substr( $this->session->getCusexpy() , -2), 'cvc' => $this->session->getCuscid() ], ]); $this->_logger->log(100,print_r("***payment Responce***",true)); $this->_logger->log(100,print_r($payment_method,true)); $this->_logger->log(100,print_r($stripe,true)); $payment_method_id = $payment_method->id; $this->_logger->log(100,print_r($payment_method_id,true)); $this->_logger->log(100,print_r("***Payment Method ID***",true)); $this->_logger->log(100,print_r($this->session->getCusId(),true)); $this->_logger->log(100,print_r($this->session->getCusexpy(),true)); $this->_logger->log(100,print_r($this->session->getCusexpm(),true)); $this->_logger->log(100,print_r($this->session->getCusnum(),true)); $this->_logger->log(100,print_r($this->session->getCuscid(),true)); $this->_logger->log(100,print_r("***Stripe Customer ID***",true)); $this->_logger->log(100,print_r($stripe_customer_id,true)); $response = $stripe->paymentMethods->attach( $payment_method_id, ['customer' => $this->session->getCusId()] ); $this->_logger->log(100,print_r("***Responce***",true)); $this->_logger->log(100,print_r($response,true)); try { \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug('vaidehi36'); $charge = \Stripe\PaymentIntent::create([ 'amount' => $amount*100, 'currency' => $order->getBaseCurrencyCode(), 'customer' => $this->session->getCusId(), "description" =>'Payment for order #'.$order->getIncrementId(), 'payment_method' => $payment_method_id, 'off_session' => true, 'confirm' => true ]); //$this->_logger->log(100,print_r($charge,true)); } catch (\Stripe\Exception\CardException $e) { $this->_logger->log(100,print_r("***Exception***",true)); $this->_logger->log(100,print_r($e->getError()->code,true)); echo 'Error code is:' . $e->getError()->code; $payment_intent_id = $e->getError()->payment_intent->id; $payment_intent = \Stripe\PaymentIntent::retrieve($payment_intent_id); } $this->_logger->log(100,print_r($this->customerSession->isLoggedIn(),true)); if($this->customerSession->isLoggedIn()) { $customer_id = $payment->getOrder()->getCustomerId(); }else{ $customer_id = "0"; } $this->_logger->log(100,print_r($customer_id,true)); $stripe_cust_model->addData([ "customer_id" => $customer_id, "order_id" => $payment->getOrder()->getIncrementId(), "stripe_customer_id" => $this->session->getCusId(), "email" => $payment->getOrder()->getCustomerEmail() ]); $saveData = $stripe_cust_model->save(); $this->_logger->log(100,print_r($payment_method_id,true)); $stripe_pay_model->addData([ "order_id" =>$payment->getOrder()->getIncrementId(), "stripe_payment_method_id" => $payment_method_id, "stripe_customer_id" => $stripe_customer_id, ]); $saveData = $stripe_pay_model->save(); if($this->customerSession->isLoggedIn()) { //$customer = $customerRepository->getById($payment->getOrder()->getCustomerId()); \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug('vaidehi37'); $customer = $this->customerRepositoryInterface->getById($payment->getOrder()->getCustomerId()); $customer->setCustomAttribute("stripe_id",$this->session->getCusId()); $customerRepository= $this->customerRepositoryInterface->save($customer); } } } $this->_logger->log(100,print_r("charge",true)); $this->_logger->log(100,print_r($charge,true)); if($charge != null) { \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug('vaidehi38'); $payment ->setTransactionId($charge->id) ->setIsTransactionClosed(0); } $this->session->unsCusId(); $this->session->unsCusexpy(); $this->session->unsCusexpm(); $this->session->unsCusnum(); $this->session->unsCuscid(); $this->session->unsLastFour(); return $this; } public function isStripeCustomer() { $this->_logger->log(100,print_r("HERE customeerrrr",true)); $areaCode = $this->appState->getAreaCode(); $this->_logger->log(100,print_r($areaCode,true)); if($areaCode == \Magento\Framework\App\Area::AREA_ADMINHTML){ $this->_logger->log(100,print_r('aree if condition',true)); //$this->_logger->log(100,print_r($this->checkoutSession->getStripeCustomerd(),true)); if($this->checkoutSession->getStripeCustomerd() != ""){ $this->_logger->log(100,print_r('aree if nested condition',true)); return $this->checkoutSession->getStripeCustomerd(); } // $customer = $this->customerRepository->getById($this->sessionQuote->getCustomerId()); // $emailId = $customer->getEmail(); // $this->_logger->log(100,print_r('client email id',true)); // $this->_logger->log(100,print_r($emailId,true)); $stripe_data=$this->_stripecustomer->create()->getCollection()->addFieldToFilter('email', 'test@test.com')->getData(); if($stripe_data){ return $stripe_data[0]['stripe_customer_id']; }else{ return null; } }else{ if($this->customerSession->isLoggedIn()) { $stripe_id=null; $customer = $this->customerRepository->getById($this->customerSession->getCustomerId()); $emailid=$customer->getEmail(); $stripe_data=$this->_stripecustomer->create() ->getCollection() ->addFieldToFilter('email', $emailid) ->getData(); if($stripe_data) { $stripe_id=$stripe_data[0]['stripe_customer_id']; } if($stripe_id!=null) { return $stripe_id; } else { return false; } } else{ return null; } } } public function useSavedcard($payment_method_id,$stripeId,$order,$amount,$iscapture) { $this->_logger->log(100,print_r("useSavedcard function",true)); $this->_logger->log(100,print_r($payment_method_id,true)); $this->_logger->log(100,print_r($stripeId,true)); $this->_logger->log(100,print_r($order->getIncrementId(),true)); $this->_logger->log(100,print_r($amount,true)); $this->_logger->log(100,print_r($iscapture,true)); $this->_logger->log(100,print_r(number_format($amount,2)*100,true)); $this->_logger->log(100,print_r($order->getBaseCurrencyCode(),true)); try { if($iscapture == true){ $this->_logger->log(100,print_r("useSavedcard function1",true)); $charge = \Stripe\PaymentIntent::create([ 'amount' => number_format($amount,2)*100, 'currency' => $order->getBaseCurrencyCode(), 'customer' => $stripeId, "description" =>'Payment for order #'.$order->getIncrementId(), 'payment_method' => $payment_method_id, 'off_session' => true, 'confirm' => $iscapture, ]); $this->_logger->log(100,print_r($charge,true)); }else{ $this->_logger->log(100,print_r("useSavedcard function2",true)); $charge = \Stripe\PaymentIntent::create([ 'amount' => number_format($amount,2)*100, 'currency' => $order->getBaseCurrencyCode(), 'customer' => $stripeId, "description" =>'Payment for order #'.$order->getIncrementId(), 'payment_method' => $payment_method_id, 'off_session' => false, 'confirm' => true, ]); } } catch (\Exception $e) { $this->_logger->log(100,print_r($e->getMessage(),true)); return false; } return $charge; } public function getStripeCustomerByEmail($emailId) { $stripe_id=null; $stripe_data = $this->_stripecustomer->create()->getCollection()->addFieldToFilter('email', $emailId)->getData(); if($stripe_data) { $stripe_id=$stripe_data[0]['stripe_customer_id']; } if($stripe_id!=null) { return $stripe_id; } else { return false; } } public function autoCaptureInstallmentPayment($order,$amount,$installments) { $orderid = $order->getIncrementId(); try { $stripe_pay_method = $this->_paymentCollection->addFieldToFilter('order_id', $orderid); $this->_logger->log(100,print_r($stripe_pay_method->getData(),true)); if($stripe_pay_method->count()){ $stripe_pay_method = $stripe_pay_method->getFirstItem(); } try { $charge = \Stripe\PaymentIntent::create([ 'amount' => number_format($amount,2)*100, "description" =>'Payment for order #'.$orderid.'-'.$installments, 'currency' => strtolower($order->getBaseCurrencyCode()), 'customer' => $stripe_pay_method->getStripeCustomerId(), 'payment_method' => $stripe_pay_method->getStripePaymentMethodId(), 'off_session' => true, 'confirm' => true, ]); } catch (\Stripe\Exception\CardException $e) { echo 'Error code is:' . $e->getError()->code; $payment_intent_id = $e->getError()->payment_intent->id; $payment_intent = \Stripe\PaymentIntent::retrieve($payment_intent_id); } } catch (\Exception $e) { $this->logging->addDebug("Exception"); $this->logging->addDebug($e->getMessage()); return false; } return $charge; } public function savedcardInstallmentPayment($lastfours,$order,$amount,$installments) { $orderid = $order->getIncrementId(); $customer_id = $this->getStripeCustomerByEmail($order->getCustomerEmail()); if(!$customer_id){ $this->logging->addDebug("Exception False"); return false; } try { $charge = \Stripe\PaymentIntent::create([ 'amount' => $amount*100, 'currency' => $order->getBaseCurrencyCode(), 'customer' => $customer_id, "description" =>'Payment for order #'.$orderid.'-'.$installments, 'payment_method' => $lastfours, 'off_session' => true, 'confirm' => true, ]); } catch (\Stripe\Exception\CardException $e) { echo 'Error code is:' . $e->getError()->code; $payment_intent_id = $e->getError()->payment_intent->id; $payment_intent = \Stripe\PaymentIntent::retrieve($payment_intent_id); } return $charge; } public function cardInstallmentPayment($order,$payment,$amount,$installments) { $this->_logger->log(100,print_r($payment,true)); $billing = $order->getBillingAddress(); $email = $order->getCustomerEmail(); $customer_id = $this->getStripeCustomerByEmail($order->getCustomerEmail()); $this->_logger->log(100,print_r($customer_id,true)); if($billing->getRegionId()) { $regionid = $billing->getRegionId(); } else { $regionid = ''; } try { $stripe = new \Stripe\StripeClient($this->configProvider->getSecretKey()); $payment_method = $stripe->paymentMethods->create([ 'type' => 'card', 'card' => [ 'number' => $payment['cc_number'], 'exp_month' => $payment['cc_exp_month'], 'exp_year' => date("y",strtotime($payment['cc_exp_year'])), 'cvc' => $payment['cc_cid'], ], ]); $payment_method_id = $payment_method->id; $response = $stripe->paymentMethods->attach( $payment_method_id, ['customer' => $customer_id] ); $this->_logger->log(100,print_r($response,true)); try { $charge = \Stripe\PaymentIntent::create([ 'amount' => $amount*100, 'currency' => $order->getBaseCurrencyCode(), 'customer' => $customer_id, "description" =>'Payment for order #'.$order->getIncrementId().'-'.$installments, 'payment_method' => $payment_method_id, 'off_session' => true, 'confirm' => true, ]); } catch (\Stripe\Exception\CardException $e) { echo 'Error code is:' . $e->getError()->code; $payment_intent_id = $e->getError()->payment_intent->id; $payment_intent = \Stripe\PaymentIntent::retrieve($payment_intent_id); } } catch (\Stripe\Exception\CardException $e) { echo 'Error code is:' . $e->getError()->code; } return $charge; } public function chargeNormally($order,$payment,$paymentInfo,$amount,$iscapture) { $this->_logger->log(100,print_r("***chargeNormally callled***",true)); $billing = $order->getBillingAddress(); $email = $order->getCustomerEmail(); if($billing->getRegionId()) { $regionid = $billing->getRegionId(); } else { $regionid = ''; } $token = \Stripe\Token::create(array( "card" => array( 'address_city' =>$billing->getCity(), 'address_country' => $billing->getCountryId(), 'address_line1' => $billing->getStreetLine(1), 'address_line2' => $billing->getStreetLine(2), 'address_zip' => $billing->getPostcode(), 'address_state' => $billing->getRegion(), "name" => $order->getCustomerName(), "number" => $payment->getCcNumber(), "cvc" => $payment->getCcCid(), "exp_month" => sprintf('%02d',$payment->getCcExpMonth()), "exp_year" => $payment->getCcExpYear(), "metadata[regionid]" => $regionid, ))); if(array_key_exists('yesorno',$paymentInfo)) { if(!$paymentInfo['yesorno'] && $paymentInfo['recurringbutloggedout']!=1) { $charge = \Stripe\Charge::create(array( 'amount' => number_format($amount,2)*100, 'currency' => strtolower($order->getBaseCurrencyCode()), 'source' => $token, "description" =>'Payment for order #'.$order->getIncrementId(), 'capture' => $iscapture )); return $charge; } } $rest = substr($payment->getCcNumber(),-4); $token = $token->id; $cardType=$payment->getCcType(); $customer_data=$this->stripeFactory->create() ->getCollection() ->addFieldToFilter('email', $email); $countingcustomers = count($customer_data); if($countingcustomers>0) { $stripedata=$customer_data->getData(); $stripe_id=$stripedata[0]['stripe_id']; $customer = \Stripe\Customer::retrieve($stripe_id); \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug("customer66 :-",[$customer]); $customer->sources->create(array("card" => $token)); } else { try{ $customer = \Stripe\Customer::create([ 'source' => $token, 'email' => $email, ]); } catch (Stripe_Error $e) { throw new \Magento\Framework\Exception\LocalizedException(__('There was a problem processing this payment.')); } } try{ $charge = \Stripe\Charge::create(array( 'amount' => number_format($amount,2)*100, 'currency' => strtolower($order->getBaseCurrencyCode()), "description" =>'Payment for order #'.$order->getIncrementId(), 'customer' => $customer->id, 'capture'=> $iscapture )); } catch (Stripe_Error $e) { throw new \Magento\Framework\Exception\LocalizedException(__('There was a problem processing this payment.')); } $model=$this->stripeFactory->create(); $model->setSubscriptionId($order->getIncrementId()); $model->setStripeId($customer->id); $model->setLastfour($rest); $model->setEmail($email); $model->setType($cardType); $model->save(); return $charge; } public function recurringPayment($sessionstripeid,$amount,$order,$iscapture) { $lastfour=$this->checkoutSession->getLastfour(); if($lastfour=='none') { return false; } $availablecard=$this->getCreditcard($sessionstripeid); $noofcards=count($availablecard); for($i=0;$i number_format($amount,2)*100, 'currency' => strtolower($order->getBaseCurrencyCode()), "description" =>'Payment for subscription order #'.$order->getIncrementId(), 'customer' => $sessionstripeid, "source" => $cardtobeused, 'capture'=>$iscapture ]); } catch (\Exception $e) { $this->logging->addDebug($e->getMessage()); return false; } $this->checkoutSession->unsStripeId(); $this->checkoutSession->unsLastfour(); return $charge; } public function getAllCards($customerId){ try{ $stripe = new \Stripe\StripeClient($this->configProvider->getSecretKey()); $allCard = $stripe->paymentMethods->all([ 'customer' => $customerId, 'type' => 'card', ]); return $allCard; } catch(\Exception $e) { return null; } } public function getCreditcard($customerId = null) { if($customerId == null){ return null; } try{ $stripe = new \Stripe\StripeClient($this->configProvider->getSecretKey()); $allCard = $stripe->paymentMethods->all([ 'customer' => $customerId, 'type' => 'card', ]); return $allCard->data; } catch(\Exception $e) { $this->_logger->log(100,print_r($e->getMessage(),true)); return null; } } public function getCardDetails($token) { \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug("token :-",[$token]); $customer_id = $this->isStripeCustomer(); \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug("customer_id :-",[$customer_id]); $customer = \Stripe\Customer::retrieve($customer_id); **$card = $customer->sources->retrieve($token); return $card;** } public function createCustomer(){ try{ $customer_id = $this->isStripeCustomer(); if($customer_id == ''){ $billing_info = $this->checkoutSession->getQuote()->getBillingAddress(); $billing_street = $billing_info->getStreet(); $stripe = new \Stripe\StripeClient($this->configProvider->getSecretKey()); $stripe_customer = $stripe->customers->create([ 'name' => $billing_info->getFirstname()." ".$billing_info->getLastname(), 'address' => [ 'line1' => $billing_street[0], 'postal_code' => $billing_info->getPostcode(), 'city' => $billing_info->getCity(), 'state' => $billing_info->getRegion(), 'country' => $billing_info->getCountry() ] ]); $customer_id = $stripe_customer->id; } return $customer_id; }catch (Stripe_Error $e) { throw new \Magento\Framework\Exception\LocalizedException(__('There was a problem processing this payment.')); } } it gives error source not found does anyone have idea???from Active questions tagged magento-enterprise - Magento Stack Exchange