Skip to content

Commit

Permalink
deleting payment method
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Birch committed Dec 18, 2019
1 parent 7522204 commit d307928
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 41 deletions.
43 changes: 43 additions & 0 deletions src/controllers/PaymentMethodsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Affiliate plugin for Craft CMS 3.x
*
* Plugin to add affiliates to Craft Commerce
*
* @link https://kurious.agency
* @copyright Copyright (c) 2019 Kurious Agency
*/

namespace kuriousagency\commerce\braintree\controllers;

use Craft;
use craft\web\Controller;
use kuriousagency\commerce\braintree\gateways\Gateway;

use craft\commerce\Plugin as Commerce;

/**
* @author Kurious Agency
* @package Affiliate
* @since 1.0.0
*/
class PaymentMethodsController extends Controller
{

public function actionDelete()
{

$request = Craft::$app->getRequest();

$handle = $request->getBodyParam('gateway');
$token = $request->getBodyParam('token');

$gateway = Commerce::getInstance()->getGateways()->getGatewayByHandle($handle);

$gateway->deletePaymentMethod($token);

return $this->redirectToPostedUrl();

}

}
113 changes: 72 additions & 41 deletions src/gateways/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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
{
Expand All @@ -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]);

Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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.'));
}
Expand All @@ -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.'));
}

Expand Down Expand Up @@ -571,15 +596,15 @@ public function updateSubscriptionPayment(Subscription $subscription, BasePlan $
*/
public function getHasBillingIssues(Subscription $subscription): bool
{
throw new NotSupportedException();
return false;
}

/**
* @inheritdoc
*/
public function getBillingIssueDescription(Subscription $subscription): string
{
throw new NotSupportedException();
return '';
}

/**
Expand All @@ -606,7 +631,8 @@ public function processWebHook(): WebResponse
break;
case 'subscription_charged_successfully':
$this->_handleSubscriptionCharged($webhookNotification->subscription);
break;
break;
// subscription_charged_unsuccessfully

}

Expand Down Expand Up @@ -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 = [])
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d307928

Please sign in to comment.