Last active
March 27, 2018 12:28
-
-
Save bummzack/c9b5eccfc7134b2922dc21a01c64df36 to your computer and use it in GitHub Desktop.
Conditional payment method in SilverShop
This file contains hidden or 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 | |
use SilverStripe\Omnipay\GatewayInfo; | |
/** | |
*/ | |
class CheckoutStep_CustomPaymentMethod extends CheckoutStep_PaymentMethod | |
{ | |
public static function get_supported_gateways(Order $order = null) | |
{ | |
$gateways = GatewayInfo::getSupportedGateways(); | |
$member = Member::currentUser(); | |
// Remove the invoice option if the member can't pay by invoice. | |
// How you check that and what gateways you remove is entirely up to you. | |
if (!$member || !$member->AllowInvoice()) { | |
unset($gateways['Manual']); | |
return $gateways; | |
} | |
return $gateways; | |
} | |
private static $allowed_actions = array( | |
'paymentmethod', | |
'PaymentMethodForm' | |
); | |
protected function checkoutconfig() | |
{ | |
$config = new CheckoutComponentConfig(ShoppingCart::curr(), false); | |
// The important bit here is to use the custom checkout component | |
$config->addComponent(new CustomPaymentCheckoutComponent()); | |
return $config; | |
} | |
public function paymentmethod() | |
{ | |
return array( | |
'OrderForm' => $this->PaymentMethodForm() | |
); | |
} | |
} |
This file contains hidden or 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 | |
class CustomPaymentCheckoutComponent extends PaymentCheckoutComponent | |
{ | |
public function getFormFields(Order $order) { | |
$fields = new FieldList(); | |
$gateways = CheckoutStep_CustomPaymentMethod::get_supported_gateways(); | |
if(count($gateways) > 1){ | |
$fields->push( | |
new OptionsetField( | |
'PaymentMethod', | |
_t("CheckoutField.PaymentType", "Payment Type"), | |
$gateways, | |
array_keys($gateways) | |
) | |
); | |
} | |
if(count($gateways) == 1){ | |
$fields->push( | |
HiddenField::create('PaymentMethod')->setValue(key($gateways)) | |
); | |
} | |
return $fields; | |
} | |
public function getRequiredFields(Order $order) { | |
return array('PaymentMethod'); | |
} | |
public function validateData(Order $order, array $data) { | |
$result = new ValidationResult(); | |
if(!isset($data['PaymentMethod'])){ | |
$result->error("Payment method not provided", "PaymentMethod"); | |
throw new ValidationException($result); | |
} | |
$methods = CheckoutStep_CustomPaymentMethod::get_supported_gateways(); | |
if(!isset($methods[$data['PaymentMethod']])){ | |
$result->error("Gateway not supported", "PaymentMethod"); | |
throw new ValidationException($result); | |
} | |
} | |
public function getData(Order $order) { | |
$gateways = CheckoutStep_CustomPaymentMethod::get_supported_gateways(); | |
$selected = Checkout::get($order)->getSelectedPaymentMethod(); | |
if(isset($gateways[$selected])){ | |
return array( | |
'PaymentMethod' => Checkout::get($order)->getSelectedPaymentMethod() | |
); | |
} else { | |
return array(); | |
} | |
} | |
public function setData(Order $order, array $data) { | |
if(isset($data['PaymentMethod'])){ | |
Checkout::get($order)->setPaymentMethod($data['PaymentMethod']); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment