HasRouteMethods.php
74 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\PaymentGateways\Traits; |
| 4 | |
| 5 | use Give\Framework\Exceptions\Primitives\Exception; |
| 6 | use Give\Framework\PaymentGateways\Exceptions\PaymentGatewayException; |
| 7 | use Give\Framework\PaymentGateways\SubscriptionModule; |
| 8 | |
| 9 | /** |
| 10 | * @since 2.20.0 |
| 11 | * @property SubscriptionModule $subscriptionModule |
| 12 | * @property array $routeMethods |
| 13 | * @property array $secureRouteMethods |
| 14 | */ |
| 15 | trait HasRouteMethods |
| 16 | { |
| 17 | /** |
| 18 | * Route methods are used to extend the gateway api. |
| 19 | * By adding a custom routeMethod, you are effectively |
| 20 | * registering a new public route url that will resolve itself and |
| 21 | * call your method. |
| 22 | * |
| 23 | * @var string[] |
| 24 | */ |
| 25 | public $routeMethods = []; |
| 26 | |
| 27 | /** |
| 28 | * Secure Route methods are used to extend the gateway api with an additional wp_nonce. |
| 29 | * By adding a custom secureRouteMethod, you are effectively |
| 30 | * registering a new route url that will resolve itself and |
| 31 | * call your method after validating the nonce. |
| 32 | * |
| 33 | * @var string[] |
| 34 | */ |
| 35 | public $secureRouteMethods = []; |
| 36 | |
| 37 | /** |
| 38 | * @since 2.20.0 |
| 39 | * |
| 40 | * @param string $method |
| 41 | * |
| 42 | * @return bool |
| 43 | */ |
| 44 | public function supportsMethodRoute($method) |
| 45 | { |
| 46 | $allGatewayMethods = array_merge($this->routeMethods, $this->secureRouteMethods); |
| 47 | |
| 48 | return in_array($method, $allGatewayMethods, true); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @since 2.20.0 |
| 53 | * |
| 54 | * @param string $method |
| 55 | * |
| 56 | * @throws Exception |
| 57 | */ |
| 58 | public function callRouteMethod($method, $queryParams) |
| 59 | { |
| 60 | if ($this->supportsMethodRoute($method)) { |
| 61 | return $this->$method($queryParams); |
| 62 | } |
| 63 | |
| 64 | throw new PaymentGatewayException( |
| 65 | sprintf( |
| 66 | '%1$s route method is not supported by %2$s and %3$s', |
| 67 | $method, |
| 68 | get_class($this), |
| 69 | get_class($this->subscriptionModule) |
| 70 | ) |
| 71 | ); |
| 72 | } |
| 73 | } |
| 74 |