From d307928796160dfaddbfee6716ecd908bfce6668 Mon Sep 17 00:00:00 2001 From: Samuel Birch Date: Wed, 18 Dec 2019 11:41:24 +0000 Subject: [PATCH] deleting payment method --- src/controllers/PaymentMethodsController.php | 43 +++++++ src/gateways/Gateway.php | 113 ++++++++++++------- 2 files changed, 115 insertions(+), 41 deletions(-) create mode 100644 src/controllers/PaymentMethodsController.php diff --git a/src/controllers/PaymentMethodsController.php b/src/controllers/PaymentMethodsController.php new file mode 100644 index 0000000..ac9c68e --- /dev/null +++ b/src/controllers/PaymentMethodsController.php @@ -0,0 +1,43 @@ +getRequest(); + + $handle = $request->getBodyParam('gateway'); + $token = $request->getBodyParam('token'); + + $gateway = Commerce::getInstance()->getGateways()->getGatewayByHandle($handle); + + $gateway->deletePaymentMethod($token); + + return $this->redirectToPostedUrl(); + + } + +} \ No newline at end of file diff --git a/src/gateways/Gateway.php b/src/gateways/Gateway.php index 4ee50d1..a377d41 100644 --- a/src/gateways/Gateway.php +++ b/src/gateways/Gateway.php @@ -192,7 +192,7 @@ public function getCustomer($user) public function getPaymentMethod($token) { return $this->gateway->paymentMethod()->find($token); - } + } public function createPaymentMethod(BasePaymentForm $sourceData, int $userId) { @@ -207,10 +207,17 @@ public function createPaymentMethod(BasePaymentForm $sourceData, int $userId) 'customerId' => $user->uid, 'paymentMethodNonce' => $sourceData->nonce, 'options' => [ - 'makeDefault' => (boolean)$sourceData->default, + 'makeDefault' => (boolean)$sourceData->default, ], ]); - } + } + + public function deletePaymentMethod($token) + { + $result = $this->gateway->paymentMethod()->delete($token); + + return $result->success; + } public function authorize(Transaction $transaction, BasePaymentForm $form): RequestResponseInterface { @@ -237,10 +244,12 @@ public function createPaymentSource(BasePaymentForm $sourceData, int $userId): P //check for existing paymentSource $sources = Commerce::getInstance()->getPaymentSources()->getAllGatewayPaymentSourcesByUserId($this->id, $userId); - // foreach ($sources as $source) - // { - // Commerce::getInstance()->getPaymentSources()->deletePaymentSourceById($source->id); - // } + foreach ($sources as $source) + { + if ($source->token == $response->paymentMethod->token) { + Commerce::getInstance()->getPaymentSources()->deletePaymentSourceById($source->id); + } + } $description = Craft::t('commerce-braintree', '{cardType} ending in ••••{last4}', ['cardType' => $response->paymentMethod->cardType, 'last4' => $response->paymentMethod->last4]); @@ -350,6 +359,21 @@ public function cancelSubscription(Subscription $subscription, BaseCancelSubscri $response = $this->gateway->subscription()->cancel($subscription->reference); if($response->success) { + // remove paymentsource + $source = $this->getPaymentSource($subscription->userId, $subscription->subscriptionData['paymentMethodToken']); + if ($source) { + // check if any other subscriptions are using this payment source + $canDelete = true; + foreach (Subscription::find()->gatewayId($this->id)->userId($subscription->userId)->isCanceled(0)->reference(['not',$subscription->reference])->all() as $sub) { + if ($sub->subscriptionData['paymentMethodToken'] == $source->token) { + $canDelete = false; + } + } + if ($canDelete) { + Commerce::getInstance()->getPaymentSources()->deletePaymentSourceById($source->id); + } + } + return new SubscriptionResponse($response->subscription); } else { @@ -486,7 +510,7 @@ public function getSubscriptionPlans(): array public function subscribe(User $user, BasePlan $plan, SubscriptionForm $parameters): SubscriptionResponseInterface { - $source = $this->getPaymentSource($user->id); + $source = $this->getPaymentSource($user->id); if (!$source) { throw new PaymentSourceException(Craft::t('commerce-braintree', 'No payment sources are saved to use for subscriptions.')); } @@ -497,9 +521,10 @@ public function subscribe(User $user, BasePlan $plan, SubscriptionForm $paramete 'planId' => $plan->reference, 'price' => $plan->price, 'merchantAccountId' => $this->merchantAccountId[$plan->getCurrency()], - ]); - - if (!$response->success) { + ]); + + if (!$response->success) { + //Craft::dd($response); throw new SubscriptionException(Craft::t('commerce-braintree', 'Unable to subscribe at this time.')); } @@ -571,7 +596,7 @@ public function updateSubscriptionPayment(Subscription $subscription, BasePlan $ */ public function getHasBillingIssues(Subscription $subscription): bool { - throw new NotSupportedException(); + return false; } /** @@ -579,7 +604,7 @@ public function getHasBillingIssues(Subscription $subscription): bool */ public function getBillingIssueDescription(Subscription $subscription): string { - throw new NotSupportedException(); + return ''; } /** @@ -606,7 +631,8 @@ public function processWebHook(): WebResponse break; case 'subscription_charged_successfully': $this->_handleSubscriptionCharged($webhookNotification->subscription); - break; + break; + // subscription_charged_unsuccessfully } @@ -670,18 +696,24 @@ public function supportsReactivation(): bool } - private function getPaymentSource($userId) + private function getPaymentSource($userId, $token=null) { $sources = Commerce::getInstance()->getPaymentSources()->getAllGatewayPaymentSourcesByUserId($this->id, $userId); if (\count($sources) === 0) { return null; - } + } + + if ($token) { + foreach ($sources as $source) { + if ($source->token == $token) { + return $source; + } + } + } // get first payment source - $source = $sources[0]; - - return $source; + return $sources[0]; } private function getCpPaymentFormHtml(array $params = []) @@ -740,9 +772,9 @@ private function getSitePaymentFormHtml(array $params = []) $previousMode = $view->getTemplateMode(); $view->setTemplateMode(View::TEMPLATE_MODE_CP); - $view->registerJsFile('https://js.braintreegateway.com/web/dropin/1.21.0/js/dropin.min.js'); - $view->registerAssetBundle(DropinUiAsset::class); - $html = $view->renderTemplate('commerce-braintree/paymentForms/dropin-ui', $params); + $view->registerJsFile('https://js.braintreegateway.com/web/dropin/1.21.0/js/dropin.min.js'); + $view->registerAssetBundle(DropinUiAsset::class); + $html = $view->renderTemplate('commerce-braintree/paymentForms/dropin-ui', $params); $view->setTemplateMode($previousMode); @@ -827,26 +859,25 @@ private function _handleSubscriptionCharged($data) Commerce::getInstance()->getSubscriptions()->receivePayment($subscription, $payment, $data->nextBillingDate); } - - private function _formatAddress($data) { - - if (!$data) { - return []; - } - - return [ - 'firstName' => $data->firstName, - 'lastName' => $data->lastName, - 'company' => $data->businessName, - 'streetAddress' => $data->address1, - 'extendedAddress'=> $data->address2, - 'locality' => $data->city, - 'region' => ($data->state && $data->country && $data->country->iso == 'US') ? $data->state->abbreviation : $data->stateName, - 'postalCode' => $data->zipCode, - //'countryName' => $data->country ? $data->country->name : '', - 'countryCodeAlpha2' => $data->country ? $data->country->iso : '' - ]; + + private function _formatAddress($data) + { + if (!$data) { + return []; + } + return [ + 'firstName' => $data->firstName, + 'lastName' => $data->lastName, + 'company' => $data->businessName, + 'streetAddress' => $data->address1, + 'extendedAddress'=> $data->address2, + 'locality' => $data->city, + 'region' => ($data->state && $data->country && $data->country->iso == 'US') ? $data->state->abbreviation : $data->stateName, + 'postalCode' => $data->zipCode, + //'countryName' => $data->country ? $data->country->name : '', + 'countryCodeAlpha2' => $data->country ? $data->country->iso : '' + ]; } public function format3DSAddress($order)