Source of file BraintreeExtension.php
Size: 4,466 Bytes - Last Modified: 2022-02-21T10:01:05+00:00
/var/www/docs.ssmods.com/process/src/src/BraintreeExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 | <?php namespace AlexT\BTPayment; use Braintree\Gateway; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\TextField; use SilverStripe\ORM\DataExtension; use SilverStripe\Security\Member; use SilverStripe\Security\Security; use SilverStripe\SiteConfig\SiteConfig; /** * Class BraintreeExtension * An extension of site configuration which adds Braintree Payment Settings in the admin page. * This class also provides some interface to get gateway, client id or create a customer in the vault if not exists * * @package SilverStripe\Lessons */ class BraintreeExtension extends DataExtension { // Gateway object will be created if null private static $gateway = null; /** * {@inheritdoc} */ private static $db = [ 'BTEnvironment' => 'Varchar', 'BTMerchantId' => 'Varchar', 'BTPublicKey' => 'Varchar', 'BTPrivateKey' => 'Varchar' ]; /** * Update site configuration with 'Brain Tree Payment' tab which includes 4 setting fields for payment credentials * * @param FieldList $fields list of fields of site configuration */ public function updateCMSFields(FieldList $fields) { $fields->addFieldsToTab('Root.BraintreePayment', array( DropdownField::create('BTEnvironment', 'Environment', array('sandbox' => 'sandbox', 'production' => 'production')), TextField::create('BTMerchantId', 'Merchant ID'), TextField::create('BTPublicKey', 'Public Key'), TextField::create('BTPrivateKey', 'Private Key') )); } /** * Get a Braintree Gateway object based on the Braintree credentials in the setting * * @return Gateway|null */ public static function BTGateway() { // get current site config $siteConfig = SiteConfig::current_site_config(); // create a new Gateway object if null if (self::$gateway == null) { self::$gateway = new Gateway([ 'environment' => $siteConfig->BTEnvironment, 'merchantId' => $siteConfig->BTMerchantId, 'publicKey' => $siteConfig->BTPublicKey, 'privateKey' => $siteConfig->BTPrivateKey ]); } return self::$gateway; } /** * Generate client token based on customer id in the vault (should it exists) * * @param $customerId Customer Id stored in the vault * @return client token (string) */ public static function BTClientToken($gateway, $member) { if ($gateway != null && $member != null) { // generate a client token from customer id return $gateway->ClientToken()->generate(["customerId" => self::BTClientId($gateway, $member)]); } return null; } /** * Get customer(client) id * * @return A string represents customer id * @throws \SilverStripe\ORM\ValidationException */ public static function BTClientId($gateway, $member) { if ($member != null) { $btClientId = $member->BTClientId; // if customer is not created in the vault if (empty($btClientId) || is_null($btClientId)) { // create a new customer in the vault, return $btClientId = self::BTCreateClient($gateway, $member); // write back to database $member->BTClientId = $btClientId; $member->write(); } return $btClientId; } else { return -1; } } /** * Create a new customer in the vault * * @param $gateway Braintree gateway * @param $member current SilverStripe member * @return new customer id */ public static function BTCreateClient($gateway, $member) { if ($gateway != null && $member != null) { $result = $gateway->customer()->create([ 'firstName' => $member->FirstName, 'lastName' => $member->SurName, /* 'company' => '', 'email' => '', 'phone' => '', 'fax' => '', 'website' => '' */ ]); if ($result->success) { return $result->customer->id; } else { return ''; } } else { return ''; } } } |