/** * @param string $customer_id * @param string $plan_id * @param array $addOns * @param array $discounts * @param array $removeAddOns * @param array $removeDiscounts * @return bool | int * @throws Exception */ public function createSubscription($customer_id, $plan_id, $addOns = [], $discounts = [], $removeAddOns = [], $removeDiscounts = []) { $customer = Braintree_Customer::find($customer_id); $the_token = null; if (!empty($customer)) { $the_token = $customer->paymentMethods[0]->token; } else { throw new Exception("Customer not found \n"); } $formattedAddOns = []; foreach ($addOns as $addOn) { $formattedAddOns[] = ['inheritedFromId' => $addOn]; } $formattedDiscounts = []; foreach ($discounts as $discount) { $formattedDiscounts[] = ['inheritedFromId' => $discount]; } $result = Braintree_Subscription::create(['paymentMethodToken' => $the_token, 'planId' => $plan_id, 'addOns' => ['add' => $formattedAddOns, 'remove' => $removeAddOns], 'discounts' => ['add' => $formattedDiscounts, 'remove' => $removeDiscounts]]); if ($result->success) { return $result->subscription->id; } else { foreach ($result->errors->deepAll() as $error) { throw new Exception($error->code . ": " . $error->message . "\n"); } } return false; }
/** * Handle the event. * * @param \Laravel\Spark\Events\Profile\ContactInformationUpdated $event */ public function handle($event) { if (!$event->user->hasBillingProvider()) { return; } BraintreeCustomer::update($event->user->braintree_id, ['email' => $event->user->email]); }
public function testCreate_fromPaymentMethodToken() { $customer = Braintree\Customer::createNoValidate(); $card = Braintree\CreditCard::create(['customerId' => $customer->id, 'cardholderName' => 'Cardholder', 'number' => '5105105105105100', 'expirationDate' => '05/12'])->creditCard; $result = Braintree\PaymentMethodNonce::create($card->token); $this->assertTrue($result->success); $this->assertNotNull($result->paymentMethodNonce); $this->assertNotNull($result->paymentMethodNonce->nonce); }
/** * @depends testCustomerCreate */ public function testTokenPayment() { $customer = Customer::find(self::$customer->id); $this->assertInstanceOf('\\Braintree\\Customer', $customer); $this->assertArrayHasKey(0, $customer->paymentMethods()); $model = new BraintreeForm(); $model->setScenario('saleFromVault'); $this->assertTrue($model->load(['amount' => rand(1, 200), 'paymentMethodToken' => $customer->paymentMethods()[0]->token], '')); $this->assertNotFalse($model->send()); }
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 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 testSale_createsASaleUsingGivenToken() { $customer = Braintree\Customer::createNoValidate(); $http = new HttpClientApi(Braintree\Configuration::$global); $nonce = Test\Helper::generateValidUsBankAccountNonce(); $result = Braintree\PaymentMethod::create(['customerId' => $customer->id, 'paymentMethodNonce' => $nonce]); $result = Braintree\UsBankAccount::sale($result->paymentMethod->token, ['merchantAccountId' => 'us_bank_merchant_account', 'amount' => '100.00']); $this->assertTrue($result->success); $transaction = $result->transaction; $this->assertEquals(Braintree\Transaction::SETTLEMENT_PENDING, $transaction->status); $this->assertEquals(Braintree\Transaction::SALE, $transaction->type); $this->assertEquals('100.00', $transaction->amount); $this->assertEquals('021000021', $transaction->usBankAccount->routingNumber); $this->assertEquals('1234', $transaction->usBankAccount->last4); $this->assertEquals('checking', $transaction->usBankAccount->accountType); $this->assertEquals('PayPal Checking - 1234', $transaction->usBankAccount->accountDescription); $this->assertEquals('Dan Schulman', $transaction->usBankAccount->accountHolderName); $this->assertRegExp('/CHASE/', $transaction->usBankAccount->bankName); }
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 testFindErrorsOnWhitespaceId() { $this->setExpectedException('InvalidArgumentException'); Braintree\Customer::find('\\t'); }
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 test_throwsIfNoOperatorNodeGiven() { $this->setExpectedException('InvalidArgumentException', 'Operator must be provided'); Braintree\Customer::search(array(Braintree\CustomerSearch::creditCardExpirationDate())); }
public function testDelete_worksWithPayPalAccounts() { $paymentMethodToken = 'PAYPAL_TOKEN-' . strval(rand()); $customer = Braintree\Customer::createNoValidate(); $http = new HttpClientApi(Braintree\Configuration::$global); $nonce = $http->nonceForPayPalAccount(array('paypal_account' => array('consent_code' => 'PAYPAL_CONSENT_CODE', 'token' => $paymentMethodToken))); $paypalAccountResult = Braintree\PaymentMethod::create(array('customerId' => $customer->id, 'paymentMethodNonce' => $nonce)); $this->assertTrue($paypalAccountResult->success); Braintree\PaymentMethod::delete($paymentMethodToken); $this->setExpectedException('Braintree\\Exception\\NotFound'); Braintree\PaymentMethod::find($paymentMethodToken); }
public function updateCustomerViaTr($regularParams, $trParams) { Test\Helper::suppressDeprecationWarnings(); $trData = Braintree\TransparentRedirect::updateCustomerData(array_merge($trParams, ["redirectUrl" => "http://www.example.com"])); return Test\Helper::submitTrRequest(Braintree\Customer::updateCustomerUrl(), $regularParams, $trData); }
/** * generic method for validating incoming gateway responses * * creates a new Customer 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['customer'])) { // return a populated instance of Customer return new Result\Successful(Customer::factory($response['customer'])); } else { if (isset($response['apiErrorResponse'])) { return new Result\Error($response['apiErrorResponse']); } else { throw new Exception\Unexpected("Expected customer or apiErrorResponse"); } } }
public static function createCreditCard() { $customer = Braintree\Customer::createNoValidate(['creditCard' => ['number' => '5105105105105100', 'expirationDate' => '05/2010']]); return $customer->creditCards[0]; }
public function test_rangeNode_amount() { $customer = Braintree\Customer::createNoValidate(); $creditCard = Braintree\CreditCard::create(['customerId' => $customer->id, 'cardholderName' => 'Jane Everywoman' . rand(), 'number' => '5105105105105100', 'expirationDate' => '05/12'])->creditCard; $t_1000 = Braintree\Transaction::saleNoValidate(['amount' => '1000.00', 'paymentMethodToken' => $creditCard->token]); $t_1500 = Braintree\Transaction::saleNoValidate(['amount' => '1500.00', 'paymentMethodToken' => $creditCard->token]); $t_1800 = Braintree\Transaction::saleNoValidate(['amount' => '1800.00', 'paymentMethodToken' => $creditCard->token]); $collection = Braintree\Transaction::search([Braintree\TransactionSearch::creditCardCardholderName()->is($creditCard->cardholderName), Braintree\TransactionSearch::amount()->greaterThanOrEqualTo('1700')]); $this->assertEquals(1, $collection->maximumCount()); $this->assertEquals($t_1800->id, $collection->firstItem()->id); $collection = Braintree\Transaction::search([Braintree\TransactionSearch::creditCardCardholderName()->is($creditCard->cardholderName), Braintree\TransactionSearch::amount()->lessThanOrEqualTo('1250')]); $this->assertEquals(1, $collection->maximumCount()); $this->assertEquals($t_1000->id, $collection->firstItem()->id); $collection = Braintree\Transaction::search([Braintree\TransactionSearch::creditCardCardholderName()->is($creditCard->cardholderName), Braintree\TransactionSearch::amount()->between('1100', '1600')]); $this->assertEquals(1, $collection->maximumCount()); $this->assertEquals($t_1500->id, $collection->firstItem()->id); }
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)); }
public function testUnknownCardTypeIndicators() { $customer = Braintree\Customer::createNoValidate(); $result = Braintree\CreditCard::create(array('customerId' => $customer->id, 'cardholderName' => 'Cardholder', 'number' => CardTypeIndicators::UNKNOWN, 'expirationDate' => '05/12', 'options' => array('verifyCard' => true))); $this->assertEquals(Braintree\CreditCard::PREPAID_UNKNOWN, $result->creditCard->prepaid); $this->assertEquals(Braintree\CreditCard::DURBIN_REGULATED_UNKNOWN, $result->creditCard->durbinRegulated); $this->assertEquals(Braintree\CreditCard::PAYROLL_UNKNOWN, $result->creditCard->payroll); $this->assertEquals(Braintree\CreditCard::DEBIT_UNKNOWN, $result->creditCard->debit); $this->assertEquals(Braintree\CreditCard::HEALTHCARE_UNKNOWN, $result->creditCard->healthcare); $this->assertEquals(Braintree\CreditCard::COMMERCIAL_UNKNOWN, $result->creditCard->commercial); $this->assertEquals(Braintree\CreditCard::COUNTRY_OF_ISSUANCE_UNKNOWN, $result->creditCard->countryOfIssuance); $this->assertEquals(Braintree\CreditCard::ISSUING_BANK_UNKNOWN, $result->creditCard->issuingBank); }
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 testUpdateNoValidate() { $customer = Braintree\Customer::createNoValidate(); $createdAddress = Braintree\Address::createNoValidate(['customerId' => $customer->id, 'firstName' => 'Old First', 'lastName' => 'Old Last', 'company' => 'Old Company', 'streetAddress' => '1 E Old St', 'extendedAddress' => 'Apt Old', 'locality' => 'Old Chicago', 'region' => 'Old Region', 'postalCode' => 'Old Postal', 'countryName' => 'United States of America']); $address = Braintree\Address::updateNoValidate($customer->id, $createdAddress->id, ['firstName' => 'New First', 'lastName' => 'New Last', 'company' => 'New Company', 'streetAddress' => '1 E New St', 'extendedAddress' => 'Apt New', 'locality' => 'New Chicago', 'region' => 'New Region', 'postalCode' => 'New Postal', 'countryName' => 'Mexico']); $this->assertEquals('New First', $address->firstName); $this->assertEquals('New Last', $address->lastName); $this->assertEquals('New Company', $address->company); $this->assertEquals('1 E New St', $address->streetAddress); $this->assertEquals('Apt New', $address->extendedAddress); $this->assertEquals('New Chicago', $address->locality); $this->assertEquals('New Region', $address->region); $this->assertEquals('New Postal', $address->postalCode); $this->assertEquals('Mexico', $address->countryName); }
/** * @param string $idCustomer * @return Customer */ public function findCustomer($idCustomer) { return Customer::find($idCustomer); }
/** * Get the Braintree customer for the user. * * @return \Braintree\Customer */ public function asBraintreeCustomer() { return BraintreeCustomer::find($this->braintree_id); }
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); }
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]')); }
/** * @param \AppBundle\Entity\User $user * @param float $amount * @return boolean */ public function chargeUser(User $user, float $amount, $description) { //get braintree customer $customer = BraintreeCustomer::find($this->getCustomerId($user)); $result = BraintreeTransaction::sale(['paymentMethodToken' => $customer->creditCards[0]->token, 'amount' => round($amount)]); $payment = new Payment(); $payment->setUser($user); $payment->setSuccess(true); $payment->setDate(new \DateTime()); $payment->setIntegration('braintree'); $payment->setAmount($amount * 100); $payment->setReference($result->transaction->id); $this->persist($payment); $this->flush(); return $result; }
public function testSale_createsASaleUsingGivenToken() { $nonce = Braintree\Test\Nonces::$paypalFuturePayment; $customer = Braintree\Customer::createNoValidate(['paymentMethodNonce' => $nonce]); $paypalAccount = $customer->paypalAccounts[0]; $result = Braintree\PayPalAccount::sale($paypalAccount->token, ['amount' => '100.00']); $this->assertTrue($result->success); $this->assertEquals('100.00', $result->transaction->amount); $this->assertEquals($customer->id, $result->transaction->customerDetails->id); $this->assertEquals($paypalAccount->token, $result->transaction->paypalDetails->token); }
public function testCreate_withVaultedPayPal() { $paymentMethodToken = 'PAYPAL_TOKEN-' . strval(rand()); $customer = Braintree\Customer::createNoValidate(); $http = new HttpClientApi(Braintree\Configuration::$global); $nonce = $http->nonceForPayPalAccount(['paypal_account' => ['consent_code' => 'PAYPAL_CONSENT_CODE', 'token' => $paymentMethodToken]]); Braintree\PaymentMethod::create(['customerId' => $customer->id, 'paymentMethodNonce' => $nonce]); $result = Braintree\Transaction::sale(['amount' => Braintree\Test\TransactionAmounts::$authorize, 'paymentMethodToken' => $paymentMethodToken]); $this->assertTrue($result->success); $transaction = $result->transaction; $this->assertEquals('*****@*****.**', $transaction->paypalDetails->payerEmail); $this->assertNotNull($transaction->paypalDetails->imageUrl); $this->assertNotNull($transaction->paypalDetails->debugId); }