create() public static method

public static create ( array $attribs = [] ) : Braintree\Result\Successful | Error
$attribs array
return Braintree\Result\Successful | Braintree\Result\Error
 /**
  * @param User $user
  * @param $request
  * @throws
  * @return User
  */
 public function addCustomer(User $user, $request)
 {
     $result = BraintreeCustomer::create(['id' => 'userid-' . $user->getId(), 'email' => $user->getEmail(), "creditCard" => ["number" => $request->request->get("number"), "cvv" => $request->request->get("cvv"), "expirationMonth" => $request->request->get("month"), "expirationYear" => $request->request->get("year")]]);
     if ($result->success === true) {
         $user->setBraintreeCustomerId($result->customer->id);
     } else {
         throw new Exception("Braintree create customer failed");
     }
     return $result->success;
 }
Example #2
0
 public function testValueForHtmlField()
 {
     $result = Braintree\Customer::create(['email' => 'invalid-email', 'creditCard' => ['number' => 'invalid-number', 'expirationDate' => 'invalid-exp', 'billingAddress' => ['countryName' => 'invalid-country']], 'customFields' => ['store_me' => 'some custom value']]);
     $this->assertEquals(false, $result->success);
     $this->assertEquals('invalid-email', $result->valueForHtmlField('customer[email]'));
     $this->assertEquals('', $result->valueForHtmlField('customer[credit_card][number]'));
     $this->assertEquals('invalid-exp', $result->valueForHtmlField('customer[credit_card][expiration_date]'));
     $this->assertEquals('invalid-country', $result->valueForHtmlField('customer[credit_card][billing_address][country_name]'));
     $this->assertEquals('some custom value', $result->valueForHtmlField('customer[custom_fields][store_me]'));
 }
 public function test_deepAll_givesAllErrorsDeeply()
 {
     $result = Braintree\Customer::create(['email' => 'invalid', 'creditCard' => ['number' => '1234123412341234', 'expirationDate' => 'invalid', 'billingAddress' => ['countryName' => 'invalid']]]);
     $expectedErrors = [Braintree\Error\Codes::CUSTOMER_EMAIL_IS_INVALID, Braintree\Error\Codes::CREDIT_CARD_EXPIRATION_DATE_IS_INVALID, Braintree\Error\Codes::CREDIT_CARD_NUMBER_IS_INVALID, Braintree\Error\Codes::ADDRESS_COUNTRY_NAME_IS_NOT_ACCEPTED];
     $actualErrors = $result->errors->deepAll();
     $this->assertEquals($expectedErrors, self::mapValidationErrorsToCodes($actualErrors));
     $expectedErrors = [Braintree\Error\Codes::CREDIT_CARD_EXPIRATION_DATE_IS_INVALID, Braintree\Error\Codes::CREDIT_CARD_NUMBER_IS_INVALID, Braintree\Error\Codes::ADDRESS_COUNTRY_NAME_IS_NOT_ACCEPTED];
     $actualErrors = $result->errors->forKey('customer')->forKey('creditCard')->deepAll();
     $this->assertEquals($expectedErrors, self::mapValidationErrorsToCodes($actualErrors));
 }
 public function test_paypalAccountEmail()
 {
     $http = new HttpClientApi(Braintree\Configuration::$global);
     $nonce = $http->nonceForPayPalAccount(array('paypal_account' => array('consent_code' => 'PAYPAL_CONSENT_CODE')));
     $customerId = 'UNIQUE_CUSTOMER_ID-' . strval(rand());
     $customerResult = Braintree\Customer::create(array('paymentMethodNonce' => $nonce, 'id' => $customerId));
     $this->assertTrue($customerResult->success);
     $customer = $customerResult->customer;
     $collection = Braintree\Customer::search(array(Braintree\CustomerSearch::id()->is($customer->id), Braintree\CustomerSearch::paypalAccountEmail()->is('*****@*****.**')));
     $this->assertEquals(1, $collection->maximumCount());
     $this->assertEquals($customer->id, $collection->firstItem()->id);
 }
 /**
  * @param array $customerData
  * @return bool | int
  * @throws Exception
  */
 public function createCustomer($customerData)
 {
     $result = Braintree_Customer::create(['firstName' => $customerData['first_name'], 'lastName' => $customerData['last_name'], 'creditCard' => ['paymentMethodNonce' => $customerData['nonce'], 'options' => ['verifyCard' => true], 'billingAddress' => ['firstName' => $customerData['first_name'], 'lastName' => $customerData['last_name'], 'streetAddress' => $customerData['address'], 'locality' => $customerData['city'], 'region' => $customerData['state'], 'postalCode' => $customerData['zip']]]]);
     if ($result->success) {
         return $result->customer->id;
     } else {
         $errors = $result->errors->deepAll();
         if (!empty($errors)) {
             foreach ($errors as $error) {
                 throw new Exception($error->code . ": " . $error->message . "\n");
             }
         } elseif (!empty($result->verification['processorResponseCode']) && $result->verification['processorResponseCode'] >= 2000 && !empty($result->verification['processorResponseText'])) {
             throw new Exception("Card could not be verified: " . $result->verification['processorResponseText'] . " \n");
         } else {
             throw new Exception("Card could not be processed: " . $result->message . " \n");
         }
     }
     return false;
 }
 public function test_GatewayRespectsMakeDefault()
 {
     $result = Braintree\Customer::create();
     $this->assertTrue($result->success);
     $customerId = $result->customer->id;
     $result = Braintree\CreditCard::create(array('customerId' => $customerId, 'number' => '4111111111111111', 'expirationDate' => '11/2099'));
     $this->assertTrue($result->success);
     $clientToken = Test\Helper::decodedClientToken(array("customerId" => $customerId, "options" => array("makeDefault" => true)));
     $authorizationFingerprint = json_decode($clientToken)->authorizationFingerprint;
     $http = new HttpClientApi(Braintree\Configuration::$global);
     $response = $http->post('/client_api/v1/payment_methods/credit_cards.json', json_encode(array("credit_card" => array("number" => "4242424242424242", "expirationDate" => "11/2099"), "authorization_fingerprint" => $authorizationFingerprint, "shared_customer_identifier" => "fake_identifier", "shared_customer_identifier_type" => "testing")));
     $this->assertEquals(201, $response["status"]);
     $customer = Braintree\Customer::find($customerId);
     $this->assertEquals(2, count($customer->creditCards));
     foreach ($customer->creditCards as $creditCard) {
         if ($creditCard->last4 == "4242") {
             $this->assertTrue($creditCard->default);
         }
     }
 }
 public function testSale_withExistingCustomer_storeInVault()
 {
     $customer = Braintree\Customer::create(['firstName' => 'Mike', 'lastName' => 'Jones', 'company' => 'Jones Co.', 'email' => '*****@*****.**', 'phone' => '419.555.1234', 'fax' => '419.555.1235', 'website' => 'http://example.com'])->customer;
     $transaction = Braintree\Transaction::sale(['amount' => '100.00', 'customerId' => $customer->id, 'creditCard' => ['cardholderName' => 'The Cardholder', 'number' => Braintree\Test\CreditCardNumbers::$visa, 'expirationDate' => '05/12'], 'options' => ['storeInVault' => true]])->transaction;
     $this->assertEquals($transaction->creditCardDetails->maskedNumber, '401288******1881');
     $this->assertEquals($transaction->vaultCreditCard()->maskedNumber, '401288******1881');
 }
 public function testOnHtmlField_returnsEmptyForCustomFieldsIfNoErrors()
 {
     $result = Braintree\Customer::create(array('email' => 'invalid', 'creditCard' => array('number' => '5105105105105100', 'expirationDate' => '05/12'), 'customFields' => array('storeMe' => 'value')));
     $this->assertEquals(false, $result->success);
     $this->assertEquals(array(), $result->errors->onHtmlField('customer[custom_fields][store_me]'));
 }
 /**
  * Create a Braintree customer for the given user.
  *
  * @param  string  $token
  * @param  array  $options
  * @return \Braintree\Customer
  */
 public function createAsBraintreeCustomer($token, array $options = [])
 {
     $response = BraintreeCustomer::create(array_replace_recursive(['firstName' => Arr::get(explode(' ', $this->name), 0), 'lastName' => Arr::get(explode(' ', $this->name), 1), 'email' => $this->email, 'paymentMethodNonce' => $token, 'creditCard' => ['options' => ['verifyCard' => true]]], $options));
     if (!$response->success) {
         throw new Exception('Unable to create Braintree customer: ' . $response->message);
     }
     $paymentMethod = $response->customer->paymentMethods[0];
     $paypalAccount = $paymentMethod instanceof PaypalAccount;
     $this->forceFill(['braintree_id' => $response->customer->id, 'paypal_email' => $paypalAccount ? $paymentMethod->email : null, 'card_brand' => !$paypalAccount ? $paymentMethod->cardType : null, 'card_last_four' => !$paypalAccount ? $paymentMethod->last4 : null])->save();
     return $response->customer;
 }
 public function test_multipleValueNode_status()
 {
     $result = Braintree\Customer::create(array('creditCard' => array('cardholderName' => 'Joe Smith', 'number' => '4000111111111115', 'expirationDate' => '12/2016', 'options' => array('verifyCard' => true))));
     $creditCardVerification = $result->creditCardVerification;
     $collection = Braintree\CreditCardVerification::search(array(Braintree\CreditCardVerificationSearch::id()->is($creditCardVerification->id), Braintree\CreditCardVerificationSearch::status()->is($creditCardVerification->status)));
     $this->assertEquals(1, $collection->maximumCount());
     $this->assertEquals($creditCardVerification->id, $collection->firstItem()->id);
     $collection = Braintree\CreditCardVerification::search(array(Braintree\CreditCardVerificationSearch::id()->is($creditCardVerification->id), Braintree\CreditCardVerificationSearch::status()->in(array($creditCardVerification->status, Braintree\Result\CreditCardVerification::VERIFIED))));
     $this->assertEquals(1, $collection->maximumCount());
     $this->assertEquals($creditCardVerification->id, $collection->firstItem()->id);
     $collection = Braintree\CreditCardVerification::search(array(Braintree\CreditCardVerificationSearch::id()->is($creditCardVerification->id), Braintree\CreditCardVerificationSearch::status()->is(Braintree\Result\CreditCardVerification::VERIFIED)));
     $this->assertEquals(0, $collection->maximumCount());
 }
 public function testGrant_returnsANonceThatIsVaultable()
 {
     $partnerMerchantGateway = new Braintree\Gateway(['environment' => 'development', 'merchantId' => 'integration_merchant_public_id', 'publicKey' => 'oauth_app_partner_user_public_key', 'privateKey' => 'oauth_app_partner_user_private_key']);
     $customer = $partnerMerchantGateway->customer()->create(['firstName' => 'Joe', 'lastName' => 'Brown'])->customer;
     $creditCard = $partnerMerchantGateway->creditCard()->create(['customerId' => $customer->id, 'cardholderName' => 'Adam Davis', 'number' => '4111111111111111', 'expirationDate' => '05/2009'])->creditCard;
     $oauthAppGateway = new Braintree\Gateway(['clientId' => 'client_id$development$integration_client_id', 'clientSecret' => 'client_secret$development$integration_client_secret']);
     $code = Test\Braintree\OAuthTestHelper::createGrant($oauthAppGateway, ['merchant_public_id' => 'integration_merchant_id', 'scope' => 'grant_payment_method']);
     $credentials = $oauthAppGateway->oauth()->createTokenFromCode(['code' => $code]);
     $grantingGateway = new Braintree\Gateway(['accessToken' => $credentials->accessToken]);
     $grantResult = $grantingGateway->paymentMethod()->grant($creditCard->token, true);
     $customer = Braintree\Customer::create(['firstName' => 'Bob', 'lastName' => 'Rob'])->customer;
     $result = Braintree\PaymentMethod::create(['customerId' => $customer->id, 'paymentMethodNonce' => $grantResult->nonce]);
     $this->assertTrue($result->success);
 }
 public function testUpdate_canUpdatePaymentMethodWithPaymentMethodNonce()
 {
     $oldCreditCard = SubscriptionHelper::createCreditCard();
     $plan = SubscriptionHelper::triallessPlan();
     $subscription = Braintree\Subscription::create(['paymentMethodToken' => $oldCreditCard->token, 'price' => '54.99', 'planId' => $plan['id']])->subscription;
     $customerId = Braintree\Customer::create()->customer->id;
     $http = new HttpClientApi(Braintree\Configuration::$global);
     $nonce = $http->nonce_for_new_card(["creditCard" => ["number" => "4111111111111111", "expirationMonth" => "11", "expirationYear" => "2099"], "customerId" => $oldCreditCard->customerId, "share" => true]);
     $result = Braintree\Subscription::update($subscription->id, ['paymentMethodNonce' => $nonce]);
     $this->assertTrue($result->success);
     $newCreditCard = Braintree\CreditCard::find($result->subscription->paymentMethodToken);
     $this->assertEquals("1111", $newCreditCard->last4);
     $this->assertNotEquals($oldCreditCard->last4, $newCreditCard->last4);
 }
Example #13
0
 /**
  * Create a Braintree customer for the given user.
  *
  * @param string $token
  * @param array $options
  *
  * @return BraintreeCustomer
  *
  * @throws Exception
  */
 public function createAsBraintreeCustomer($token, array $options = [])
 {
     $response = BraintreeCustomer::create(array_replace_recursive(['firstName' => ArrayHelper::getValue(explode(' ', $this->username), 0), 'lastName' => ArrayHelper::getValue(explode(' ', $this->username), 1), 'email' => $this->email, 'paymentMethodNonce' => $token, 'creditCard' => ['options' => ['verifyCard' => true]]], $options));
     if (!$response->success) {
         throw new Exception('Unable to create Braintree customer: ' . $response->message);
     }
     $paymentMethod = $response->customer->paymentMethods[0];
     $paypalAccount = $paymentMethod instanceof PaypalAccount;
     // Update user braintree info
     $this->braintreeId = $response->customer->id;
     $this->paypalEmail = $paypalAccount ? $paymentMethod->email : null;
     $this->cardBrand = !$paypalAccount ? $paymentMethod->cardType : null;
     $this->cardLastFour = !$paypalAccount ? $paymentMethod->last4 : null;
     $this->save();
     return $response->customer;
 }
Example #14
0
 public function testUpdateFromTransparentRedirect_withUpdateExisting()
 {
     Test\Helper::suppressDeprecationWarnings();
     $customer = Braintree\Customer::create(['firstName' => 'Mike', 'lastName' => 'Jones', 'creditCard' => ['number' => '5105105105105100', 'expirationDate' => '05/12', 'cardholderName' => 'Mike Jones', 'billingAddress' => ['firstName' => 'Drew', 'lastName' => 'Smith']]])->customer;
     $queryString = $this->updateCustomerViaTr([], ['customerId' => $customer->id, 'customer' => ['firstName' => 'New First', 'lastName' => 'New Last', 'creditCard' => ['number' => '4111111111111111', 'expirationDate' => '05/13', 'cardholderName' => 'New Cardholder', 'options' => ['updateExistingToken' => $customer->creditCards[0]->token], 'billingAddress' => ['firstName' => 'New First Billing', 'lastName' => 'New Last Billing', 'options' => ['updateExisting' => true]]]]]);
     $result = Braintree\Customer::updateFromTransparentRedirect($queryString);
     $this->assertTrue($result->success);
     $this->assertEquals(true, $result->success);
     $customer = $result->customer;
     $this->assertEquals('New First', $customer->firstName);
     $this->assertEquals('New Last', $customer->lastName);
     $this->assertEquals(1, sizeof($result->customer->creditCards));
     $creditCard = $customer->creditCards[0];
     $this->assertEquals('411111', $creditCard->bin);
     $this->assertEquals('1111', $creditCard->last4);
     $this->assertEquals('New Cardholder', $creditCard->cardholderName);
     $this->assertEquals('05/2013', $creditCard->expirationDate);
     $this->assertEquals(1, sizeof($result->customer->addresses));
     $address = $customer->addresses[0];
     $this->assertEquals($address, $creditCard->billingAddress);
     $this->assertEquals('New First Billing', $address->firstName);
     $this->assertEquals('New Last Billing', $address->lastName);
 }
Example #15
0
 /**
  * This save customer to braintree and returns result array.
  * @return array
  */
 public function saveCustomer()
 {
     if (isset($this->options['customerId'])) {
         $this->options['customer']['id'] = $this->options['customerId'];
     }
     $result = Customer::create($this->options['customer']);
     if ($result->success) {
         return ['status' => true, 'result' => $result];
     } else {
         return ['status' => false, 'result' => $result];
     }
 }
Example #16
0
 /**
  * Create a Braintree customer for the given user.
  *
  * @param string $nonce
  * @param array  $options
  *
  * @return bool|\Braintree\Customer
  */
 public function createAsBraintreeCustomer($nonce, array $options = [])
 {
     // Here we will create the customer instance on Braintree and store the provided ID of the
     // user from Braintree. This ID will correspond with the Braintree user instances
     // and allow us to retrieve users from Braintree later when we need to work.
     $result = BraintreeCustomer::create(array_merge($options, ['email' => $this->email, 'paymentMethodNonce' => $nonce, 'creditCard' => ['options' => ['makeDefault' => true]]]));
     if ($result->success) {
         $this->braintree_id = $result->customer->id;
         $paymentMethod = $result->customer->paymentMethods[0];
         if ($paymentMethod instanceof PayPalAccount) {
             $this->payment_type = 'paypal';
         } else {
             $this->payment_type = 'card';
             $this->card_brand = $paymentMethod->cardType;
             $this->card_last_four = $paymentMethod->last4;
         }
         $this->save();
         return $result->customer;
     }
     return false;
 }
 public function testUpdateCreditCardFromTransparentRedirect()
 {
     $customer = Braintree\Customer::create(array('firstName' => 'Mike', 'lastName' => 'Jonez'))->customer;
     $creditCard = Braintree\CreditCard::create(array('customerId' => $customer->id, 'number' => Braintree\Test\CreditCardNumbers::$masterCard, 'expirationMonth' => '10', 'expirationYear' => '10'))->creditCard;
     $params = array('credit_card' => array('number' => Braintree\Test\CreditCardNumbers::$visa));
     $trParams = array('paymentMethodToken' => $creditCard->token, 'creditCard' => array('expirationMonth' => '11', 'expirationYear' => '11'));
     $trData = Braintree\TransparentRedirect::updateCreditCardData(array_merge($trParams, array("redirectUrl" => "http://www.example.com")));
     $queryString = Test\Helper::submitTrRequest(Braintree\TransparentRedirect::url(), $params, $trData);
     Braintree\TransparentRedirect::confirm($queryString);
     $creditCard = Braintree\CreditCard::find($creditCard->token);
     $this->assertequals('401288', $creditCard->bin);
     $this->assertequals('1881', $creditCard->last4);
     $this->assertequals('11/2011', $creditCard->expirationDate);
 }
 public function testCount_returnsTheNumberOfErrors()
 {
     $result = Braintree\Customer::create(['email' => 'invalid', 'creditCard' => ['number' => 'invalid', 'expirationDate' => 'invalid', 'billingAddress' => ['countryName' => 'invaild']]]);
     $this->assertEquals(false, $result->success);
     $this->assertEquals(4, count($result->errors));
 }