PHP 7.4.33
Preview: PaymentMethodGateway.php Size: 11.11 KB
/home/godevadmin/www/admin/braintree/final/braintree_php-master/lib/Braintree/PaymentMethodGateway.php

<?php
namespace Braintree;

use InvalidArgumentException;

/**
 * Braintree PaymentMethodGateway module
 *
 * @package    Braintree
 * @category   Resources
 * @copyright  2015 Braintree, a division of PayPal, Inc.
 */

/**
 * Creates and manages Braintree PaymentMethods
 *
 * <b>== More information ==</b>
 *
 *
 * @package    Braintree
 * @category   Resources
 * @copyright  2015 Braintree, a division of PayPal, Inc.
 *
 */
class PaymentMethodGateway
{
    private $_gateway;
    private $_config;
    private $_http;

    public function __construct($gateway)
    {
        $this->_gateway = $gateway;
        $this->_config = $gateway->config;
        $this->_config->assertHasAccessTokenOrKeys();
        $this->_http = new Http($gateway->config);
    }


    public function create($attribs)
    {
        Util::verifyKeys(self::createSignature(), $attribs);
        return $this->_doCreate('/payment_methods', ['payment_method' => $attribs]);
    }

    /**
     * find a PaymentMethod by token
     *
     * @param string $token payment method unique id
     * @return CreditCard|PayPalAccount
     * @throws Exception\NotFound
     */
    public function find($token)
    {
        $this->_validateId($token);
        try {
            $path = $this->_config->merchantPath() . '/payment_methods/any/' . $token;
            $response = $this->_http->get($path);
            if (isset($response['creditCard'])) {
                return CreditCard::factory($response['creditCard']);
            } else if (isset($response['paypalAccount'])) {
                return PayPalAccount::factory($response['paypalAccount']);
            } else if (isset($response['coinbaseAccount'])) {
                return CoinbaseAccount::factory($response['coinbaseAccount']);
            } else if (isset($response['applePayCard'])) {
                return ApplePayCard::factory($response['applePayCard']);
            } else if (isset($response['androidPayCard'])) {
                return AndroidPayCard::factory($response['androidPayCard']);
            } else if (isset($response['amexExpressCheckoutCard'])) {
                return AmexExpressCheckoutCard::factory($response['amexExpressCheckoutCard']);
            } else if (isset($response['europeBankAccount'])) {
                return EuropeBankAccount::factory($response['europeBankAccount']);
            } else if (isset($response['usBankAccount'])) {
                return UsBankAccount::factory($response['usBankAccount']);
            } else if (isset($response['venmoAccount'])) {
                return VenmoAccount::factory($response['venmoAccount']);
            } else if (is_array($response)) {
                return UnknownPaymentMethod::factory($response);
            }
        } catch (Exception\NotFound $e) {
            throw new Exception\NotFound(
                'payment method with token ' . $token . ' not found'
            );
        }
    }

    public function update($token, $attribs)
    {
        Util::verifyKeys(self::updateSignature(), $attribs);
        return $this->_doUpdate('/payment_methods/any/' . $token, ['payment_method' => $attribs]);
    }

    public function delete($token, $options=[])
    {
        Util::verifyKeys(self::deleteSignature(), $options);
        $this->_validateId($token);
        $queryString = "";
        if (!empty($options)) {
            $queryString = "?" . http_build_query(Util::camelCaseToDelimiterArray($options, '_'));
        }
        return $this->_doDelete('/payment_methods/any/' . $token  . $queryString);
    }

    public function grant($sharedPaymentMethodToken, $attribs=[])
    {
        if (is_bool($attribs) === true) {
            $attribs = ['allow_vaulting' => $attribs];
        }
        $options = [ 'shared_payment_method_token' => $sharedPaymentMethodToken ];

        return $this->_doCreate(
            '/payment_methods/grant',
            [
                'payment_method' => array_merge($attribs, $options)
            ]
        );
    }

    public function revoke($sharedPaymentMethodToken)
    {
        return $this->_doCreate(
            '/payment_methods/revoke',
            [
                'payment_method' => [
                    'shared_payment_method_token' => $sharedPaymentMethodToken
                ]
            ]
        );
    }

    private static function baseSignature()
    {
        $billingAddressSignature = AddressGateway::createSignature();
        $optionsSignature = [
            'failOnDuplicatePaymentMethod',
            'makeDefault',
            'verificationMerchantAccountId',
            'verifyCard',
            'verificationAmount'
        ];
        return [
            'billingAddressId',
            'cardholderName',
            'cvv',
            'deviceData',
            'expirationDate',
            'expirationMonth',
            'expirationYear',
            'number',
            'paymentMethodNonce',
            'token',
            ['options' => $optionsSignature],
            ['billingAddress' => $billingAddressSignature]
        ];
    }

    public static function createSignature()
    {
        $signature = array_merge(self::baseSignature(), ['customerId']);
        return $signature;
    }

    public static function updateSignature()
    {
        $billingAddressSignature = AddressGateway::updateSignature();
        array_push($billingAddressSignature, [
            'options' => [
                'updateExisting'
            ]
        ]);
        $signature = array_merge(self::baseSignature(), [
            'deviceSessionId',
            'venmoSdkPaymentMethodCode',
            'fraudMerchantId',
            ['billingAddress' => $billingAddressSignature]
        ]);
        return $signature;
    }

    private static function deleteSignature()
    {
        return ['revokeAllGrants'];
    }

    /**
     * sends the create request to the gateway
     *
     * @ignore
     * @param string $subPath
     * @param array $params
     * @return mixed
     */
    public function _doCreate($subPath, $params)
    {
        $fullPath = $this->_config->merchantPath() . $subPath;
        $response = $this->_http->post($fullPath, $params);

        return $this->_verifyGatewayResponse($response);
    }

    /**
     * sends the update request to the gateway
     *
     * @ignore
     * @param string $subPath
     * @param array $params
     * @return mixed
     */
    public function _doUpdate($subPath, $params)
    {
        $fullPath = $this->_config->merchantPath() . $subPath;
        $response = $this->_http->put($fullPath, $params);

        return $this->_verifyGatewayResponse($response);
    }


    /**
     * sends the delete request to the gateway
     *
     * @ignore
     * @param string $subPath
     * @return mixed
     */
    public function _doDelete($subPath)
    {
        $fullPath = $this->_config->merchantPath() . $subPath;
        $this->_http->delete($fullPath);
        return new Result\Successful();
    }

    /**
     * generic method for validating incoming gateway responses
     *
     * creates a new CreditCard or PayPalAccount object
     * and encapsulates it inside a Result\Successful object, or
     * encapsulates a Errors object inside a Result\Error
     * alternatively, throws an Unexpected exception if the response is invalid.
     *
     * @ignore
     * @param array $response gateway response values
     * @return Result\Successful|Result\Error
     * @throws Exception\Unexpected
     */
    private function _verifyGatewayResponse($response)
    {
        if (isset($response['creditCard'])) {
            return new Result\Successful(
                CreditCard::factory($response['creditCard']),
                'paymentMethod'
            );
        } else if (isset($response['paypalAccount'])) {
            return new Result\Successful(
                PayPalAccount::factory($response['paypalAccount']),
                "paymentMethod"
            );
        } else if (isset($response['coinbaseAccount'])) {
            return new Result\Successful(
                CoinbaseAccount::factory($response['coinbaseAccount']),
                "paymentMethod"
            );
        } else if (isset($response['applePayCard'])) {
            return new Result\Successful(
                ApplePayCard::factory($response['applePayCard']),
                "paymentMethod"
            );
        } else if (isset($response['androidPayCard'])) {
            return new Result\Successful(
                AndroidPayCard::factory($response['androidPayCard']),
                "paymentMethod"
            );
        } else if (isset($response['amexExpressCheckoutCard'])) {
            return new Result\Successful(
                AmexExpressCheckoutCard::factory($response['amexExpressCheckoutCard']),
                "paymentMethod"
            );
        } else if (isset($response['europeBankAccount'])) {
            return new Result\Successful(
                EuropeBankAccount::factory($response['europeBankAccount']),
                "paymentMethod"
            );
        } else if (isset($response['usBankAccount'])) {
            return new Result\Successful(
                UsBankAccount::factory($response['usBankAccount']),
                "paymentMethod"
            );
        } else if (isset($response['venmoAccount'])) {
            return new Result\Successful(
                VenmoAccount::factory($response['venmoAccount']),
                "paymentMethod"
            );
        } else if (isset($response['paymentMethodNonce'])) {
            return new Result\Successful(
                PaymentMethodNonce::factory($response['paymentMethodNonce']),
                "paymentMethodNonce"
            );
        } else if (isset($response['apiErrorResponse'])) {
            return new Result\Error($response['apiErrorResponse']);
        } else if (is_array($response)) {
            return new Result\Successful(
                UnknownPaymentMethod::factory($response),
                "paymentMethod"
            );
        } else {
            throw new Exception\Unexpected(
            'Expected payment method or apiErrorResponse'
            );
        }
    }

    /**
     * verifies that a valid payment method identifier is being used
     * @ignore
     * @param string $identifier
     * @param Optional $string $identifierType type of identifier supplied, default 'token'
     * @throws InvalidArgumentException
     */
    private function _validateId($identifier = null, $identifierType = 'token')
    {
        if (empty($identifier)) {
           throw new InvalidArgumentException(
                   'expected payment method id to be set'
                   );
        }
        if (!preg_match('/^[0-9A-Za-z_-]+$/', $identifier)) {
            throw new InvalidArgumentException(
                    $identifier . ' is an invalid payment method ' . $identifierType . '.'
                    );
        }
    }
}
class_alias('Braintree\PaymentMethodGateway', 'Braintree_PaymentMethodGateway');

Directory Contents

Dirs: 9 × Files: 89

Name Size Perms Modified Actions
Dispute DIR
- drwxrwxr-x 2023-11-07 19:59:46
Edit Download
Error DIR
- drwxrwxr-x 2023-11-07 19:59:46
Edit Download
Exception DIR
- drwxrwxr-x 2023-11-07 19:59:46
Edit Download
- drwxrwxr-x 2023-11-07 19:59:46
Edit Download
Result DIR
- drwxrwxr-x 2023-11-07 19:59:46
Edit Download
- drwxrwxr-x 2023-11-07 19:59:46
Edit Download
Test DIR
- drwxrwxr-x 2023-11-07 19:59:46
Edit Download
- drwxrwxr-x 2023-11-07 19:59:46
Edit Download
Xml DIR
- drwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.12 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.42 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
579 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1005 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
4.10 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
9.22 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
2.38 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
2.65 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
2.79 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.69 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.17 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
3.38 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
2.88 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
3.46 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
15.30 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
4.33 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
9.73 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
15.39 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.34 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
2.32 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.52 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
11.98 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
19.88 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
2.64 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
134 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.69 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.45 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
752 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
462 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
775 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
2.59 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
259 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.78 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
303 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
696 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
3.65 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
6.49 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.90 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
431 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
432 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
988 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
2.17 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
5.80 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.34 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
553 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
946 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.01 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
839 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
4.00 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
818 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
2.61 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
691 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
430 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1009 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
564 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.12 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
11.11 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.42 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.70 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
2.81 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
5.34 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.33 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
854 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
822 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
3.54 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
655 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.11 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
2.88 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
486 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
4.06 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
7.20 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.60 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.46 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
269 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
690 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
19.16 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
17.70 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
7.79 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
3.13 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
9.12 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
1.71 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
2.57 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
3.05 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
14.96 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
2.01 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
688 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
5.46 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
13.65 KB lrwxrwxr-x 2023-11-07 19:59:46
Edit Download
760 B lrwxrwxr-x 2023-11-07 19:59:46
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).