Last active
February 18, 2020 18:32
-
-
Save AugustMiller/df325dfdd66998edc794bc70be45eda4 to your computer and use it in GitHub Desktop.
Quick method for setting a Payment Source on a Commerce Order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace modules\mymodule\controllers; | |
use Craft; | |
use craft\web\Controller; | |
use yii\web\ForbiddenHttpException; | |
/** | |
* The Base Controller for all Module Controllers | |
* | |
* @author oof. Studio <[email protected]> | |
* @since 1.0 | |
*/ | |
abstract class BaseController extends Controller | |
{ | |
/** | |
* Returns an error based on the content type that the client accepts. | |
* | |
* @param string $message | |
* @param array|null $params Route params to pass back to the template. | |
*/ | |
protected function _sendErrorResponse(string $message, array $params = []) | |
{ | |
if (Craft::$app->getRequest()->getAcceptsJson()) { | |
return $this->asJson([ | |
'success' => false, | |
'message' => $message | |
]); | |
} | |
Craft::$app->getSession()->setError($message); | |
if ($params) { | |
Craft::$app->getUrlManager()->setRouteParams($params); | |
} | |
return null; | |
} | |
/** | |
* Returns a successful response based on the content type that the client accepts. | |
* | |
* @param string $message | |
* @param mixed $redirectParams Data to make available when rendering the redirect string template. Can be an object or array. | |
*/ | |
protected function _sendSuccessResponse(string $message, $redirectParams = null) | |
{ | |
if (Craft::$app->getRequest()->getAcceptsJson()) { | |
return $this->asJson([ | |
'success' => true, | |
'message' => $message, | |
'data' => $redirectParams | |
]); | |
} | |
Craft::$app->getSession()->setNotice($message); | |
return $this->redirectToPostedUrl($redirectParams); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace modules\mymodule\controllers; | |
use Craft; | |
use craft\commerce\Plugin as Commerce; | |
/** | |
* Orders Controllers | |
* | |
* Supplemental actions for modifying orders. | |
* | |
* @author oof. Studio <[email protected]> | |
* @since 2.0.0 | |
*/ | |
class OrdersController extends BaseController | |
{ | |
/** | |
* Updates or un-sets the payment method for the current cart. | |
* | |
* @return mixed | |
*/ | |
public function actionUpdatePaymentSource() | |
{ | |
$this->requirePostRequest(); | |
$this->requireLogin(); | |
$currentUser = Craft::$app->getUser()->getIdentity(); | |
$cart = Commerce::getInstance()->getCarts()->getCart(); | |
$paymentSourceId = Craft::$app->getRequest()->getBodyParam('paymentSourceId'); | |
// Cast to integer, if one was provided: | |
if ($paymentSourceId) { | |
$cart->paymentSourceId = (int)$paymentSourceId; | |
} else { | |
$cart->paymentSourceId = null; | |
} | |
if (!Craft::$app->getElements()->saveElement($cart)) { | |
return $this->_sendErrorResponse(Craft::t('mymodule', 'Could not set payment source.'), compact('cart')); | |
} | |
// The message can be a little more useful when we've just *removed* a payment source: | |
if ($cart->paymentSourceId) { | |
return $this->_sendSuccessResponse(Craft::t('mymodule', 'Payment source selected.'), $cart); | |
} | |
return $this->_sendSuccessResponse(Craft::t('mymodule', 'Please provide details for your new payment method.'), $cart); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment