PHP 7.4.33
Preview: SubscriptionGateway.php Size: 7.14 KB
/home/godevadmin/www/admin/braintree/final/braintree/lib/Braintree/SubscriptionGateway.php

<?php
namespace Braintree;

use InvalidArgumentException;

/**
 * Braintree SubscriptionGateway module
 *
 * <b>== More information ==</b>
 *
 * For more detailed information on Subscriptions, see {@link http://www.braintreepayments.com/gateway/subscription-api http://www.braintreepaymentsolutions.com/gateway/subscription-api}
 *
 * PHP Version 5
 *
 * @package   Braintree
 */
class SubscriptionGateway
{
    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($attributes)
    {
        Util::verifyKeys(self::_createSignature(), $attributes);
        $path = $this->_config->merchantPath() . '/subscriptions';
        $response = $this->_http->post($path, ['subscription' => $attributes]);
        return $this->_verifyGatewayResponse($response);
    }

    public function find($id)
    {
        $this->_validateId($id);

        try {
            $path = $this->_config->merchantPath() . '/subscriptions/' . $id;
            $response = $this->_http->get($path);
            return Subscription::factory($response['subscription']);
        } catch (Exception\NotFound $e) {
            throw new Exception\NotFound('subscription with id ' . $id . ' not found');
        }

    }

    public function search($query)
    {
        $criteria = [];
        foreach ($query as $term) {
            $criteria[$term->name] = $term->toparam();
        }


        $path = $this->_config->merchantPath() . '/subscriptions/advanced_search_ids';
        $response = $this->_http->post($path, ['search' => $criteria]);
        $pager = [
            'object' => $this,
            'method' => 'fetch',
            'methodArgs' => [$query]
            ];

        return new ResourceCollection($response, $pager);
    }

    public function fetch($query, $ids)
    {
        $criteria = [];
        foreach ($query as $term) {
            $criteria[$term->name] = $term->toparam();
        }
        $criteria["ids"] = SubscriptionSearch::ids()->in($ids)->toparam();
        $path = $this->_config->merchantPath() . '/subscriptions/advanced_search';
        $response = $this->_http->post($path, ['search' => $criteria]);

        return Util::extractAttributeAsArray(
            $response['subscriptions'],
            'subscription'
        );
    }

    public function update($subscriptionId, $attributes)
    {
        Util::verifyKeys(self::_updateSignature(), $attributes);
        $path = $this->_config->merchantPath() . '/subscriptions/' . $subscriptionId;
        $response = $this->_http->put($path, ['subscription' => $attributes]);
        return $this->_verifyGatewayResponse($response);
    }

    public function retryCharge($subscriptionId, $amount = null)
    {
        $transaction_params = ['type' => Transaction::SALE,
            'subscriptionId' => $subscriptionId];
        if (isset($amount)) {
            $transaction_params['amount'] = $amount;
        }

        $path = $this->_config->merchantPath() . '/transactions';
        $response = $this->_http->post($path, ['transaction' => $transaction_params]);
        return $this->_verifyGatewayResponse($response);
    }

    public function cancel($subscriptionId)
    {
        $path = $this->_config->merchantPath() . '/subscriptions/' . $subscriptionId . '/cancel';
        $response = $this->_http->put($path);
        return $this->_verifyGatewayResponse($response);
    }

    private static function _createSignature()
    {
        return array_merge(
            [
                'billingDayOfMonth',
                'firstBillingDate',
                'createdAt',
                'updatedAt',
                'id',
                'merchantAccountId',
                'neverExpires',
                'numberOfBillingCycles',
                'paymentMethodToken',
                'paymentMethodNonce',
                'planId',
                'price',
                'trialDuration',
                'trialDurationUnit',
                'trialPeriod',
                ['descriptor' => ['name', 'phone', 'url']],
                ['options' => ['doNotInheritAddOnsOrDiscounts', 'startImmediately']],
            ],
            self::_addOnDiscountSignature()
        );
    }

    private static function _updateSignature()
    {
        return array_merge(
            [
                'merchantAccountId', 'numberOfBillingCycles', 'paymentMethodToken', 'planId',
                'paymentMethodNonce', 'id', 'neverExpires', 'price',
                ['descriptor' => ['name', 'phone', 'url']],
                ['options' => ['prorateCharges', 'replaceAllAddOnsAndDiscounts', 'revertSubscriptionOnProrationFailure']],
            ],
            self::_addOnDiscountSignature()
        );
    }

    private static function _addOnDiscountSignature()
    {
        return [
            [
                'addOns' => [
                    ['add' => ['amount', 'inheritedFromId', 'neverExpires', 'numberOfBillingCycles', 'quantity']],
                    ['update' => ['amount', 'existingId', 'neverExpires', 'numberOfBillingCycles', 'quantity']],
                    ['remove' => ['_anyKey_']],
                ]
            ],
            [
                'discounts' => [
                    ['add' => ['amount', 'inheritedFromId', 'neverExpires', 'numberOfBillingCycles', 'quantity']],
                    ['update' => ['amount', 'existingId', 'neverExpires', 'numberOfBillingCycles', 'quantity']],
                    ['remove' => ['_anyKey_']],
                ]
            ]
        ];
    }

    /**
     * @ignore
     */
    private function _validateId($id = null) {
        if (empty($id)) {
           throw new InvalidArgumentException(
                   'expected subscription id to be set'
                   );
        }
        if (!preg_match('/^[0-9A-Za-z_-]+$/', $id)) {
            throw new InvalidArgumentException(
                    $id . ' is an invalid subscription id.'
                    );
        }
    }

    /**
     * @ignore
     */
    private function _verifyGatewayResponse($response)
    {
        if (isset($response['subscription'])) {
            return new Result\Successful(
                Subscription::factory($response['subscription'])
            );
        } else if (isset($response['transaction'])) {
            // return a populated instance of Transaction, for subscription retryCharge
            return new Result\Successful(
                Transaction::factory($response['transaction'])
            );
        } else if (isset($response['apiErrorResponse'])) {
            return new Result\Error($response['apiErrorResponse']);
        } else {
            throw new Exception\Unexpected(
            "Expected subscription, transaction, or apiErrorResponse"
            );
        }
    }
}
class_alias('Braintree\SubscriptionGateway', 'Braintree_SubscriptionGateway');

Directory Contents

Dirs: 9 × Files: 97

Name Size Perms Modified Actions
Dispute DIR
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
Error DIR
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
Exception DIR
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
Result DIR
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
Test DIR
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
Xml DIR
- drwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.06 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.37 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
579 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1005 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
4.04 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
9.17 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.28 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.59 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.73 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.63 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.17 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
3.38 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.77 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
3.40 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
16.01 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
818 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
813 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
4.28 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
9.71 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
15.33 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.34 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.32 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.52 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
12.78 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
20.17 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.64 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
134 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.62 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.45 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
693 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
462 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
775 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.53 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
259 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.72 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
244 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
696 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
3.93 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
6.66 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.44 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.40 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.93 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.84 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
431 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
432 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
4.36 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
988 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.17 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
5.80 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.34 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
553 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
946 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.01 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
776 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
3.95 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
755 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.59 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
691 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
430 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
950 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
671 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.00 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
11.90 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.30 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.58 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.70 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
5.22 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.33 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
854 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
822 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
3.49 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
655 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.11 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.88 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
486 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
4.00 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
7.14 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.60 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.46 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
269 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
690 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
20.21 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
17.71 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
7.79 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
3.08 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
9.06 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.59 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.46 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.94 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
14.89 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
1.95 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
625 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
5.00 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
4.34 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
2.27 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
14.69 KB lrwxrwxr-x 2023-11-07 19:59:44
Edit Download
697 B lrwxrwxr-x 2023-11-07 19:59:44
Edit Download

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