1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
<?php
defined( 'ABSPATH' ) || exit();
class WC_Stripe_API_Operation {
private $client;
private $property;
private $service;
private $gateway;
private $mode = '';
public function __construct( $gateway, $client, $property ) {
$this->client = $client;
$this->property = $property;
$this->gateway = $gateway;
$service = $this->client->__get( $property );
if ( ! $service ) {
throw new InvalidArgumentException( sprintf( 'Property %s is not a valid entry', $property ) );
}
$this->service = $service;
}
public function __call( $method, $args ) {
if ( ! method_exists( $this->service, $method ) ) {
throw new InvalidArgumentException( sprintf( 'Method %s does not exist for class %s.', $method, get_class( $this->service ) ) );
}
$args = $this->parse_args( $args, $method );
try {
$args = apply_filters( 'wc_stripe_api_request_args', $args, $this->property, $method );
return $this->service->{$method}( ...$args );
}
catch ( \Stripe\Exception\ApiErrorException $e ) {
return $this->gateway->get_wp_error( $e, $this->property . '-error' );
}
catch ( \Stripe\Exception\UnexpectedValueException $e ) {
return new WP_Error( 'stripe-error', $e->getMessage(), $e );
}
catch ( \Stripe\Exception\InvalidArgumentException $e ) {
return new WP_Error( 'stripe-error', $e->getMessage(), $e );
}
}
public function mode( $mode ) {
$this->mode = $mode;
return $this;
}
private function parse_args( $args, $method ) {
$reflection_method = new ReflectionMethod( get_class( $this->service ), $method );
$num_args = $reflection_method->getNumberOfParameters();
foreach ( $reflection_method->getParameters() as $parameter ) {
if ( ! isset( $args[ $parameter->getPosition() ] ) && $parameter->isOptional() ) {
$args[ $parameter->getPosition() ] = $parameter->getDefaultValue();
}
}
$args[ $num_args - 1 ] = wp_parse_args( $args[ $num_args - 1 ], $this->gateway->get_api_options( $this->mode ) );
return $args;
}
}