/** * scheduled_subscription_payment * * Hooked to woocommerce_scheduled_subscription_payment_{gateway_id} * Completes recurring payments for a subscription */ public function scheduled_subscription_payment($amount_to_charge, $order) { $this->log(__FUNCTION__, "Info: Beginning processing of scheduled payment for order {$order->id} for the amount of {$amount_to_charge}"); $this->log(__FUNCTION__, "Info: Merchant ID = {$this->merchant_id}"); // token is required $payment_method_token = get_post_meta($order->id, '_wc_paypal_braintree_payment_method_token', true); if (empty($payment_method_token)) { $this->log(__FUNCTION__, "Error: Payment method token is missing on order meta"); WC_Subscriptions_Manager::process_subscription_payment_failure_on_order($order); return; } // as is the customer id $braintree_customer_id = get_post_meta($order->id, '_wc_paypal_braintree_customer_id', true); if (empty($braintree_customer_id)) { $this->log(__FUNCTION__, "Error: Braintree customer ID is missing on order meta"); WC_Subscriptions_Manager::process_subscription_payment_failure_on_order($order); return; } // Create the gateway instance require_once dirname(__FILE__) . '/../braintree_sdk/lib/Braintree.php'; $gateway = new Braintree_Gateway(array('accessToken' => $this->merchant_access_token)); // Process the sale with the stored token and customer $sale_args = apply_filters('wc_gateway_paypal_braintree_sale_args', array('amount' => $amount_to_charge, 'paymentMethodToken' => $payment_method_token, 'recurring' => true, 'customerId' => $braintree_customer_id, 'channel' => 'WooThemes_BT', 'orderId' => $order->id, 'options' => array('submitForSettlement' => true, 'storeInVaultOnSuccess' => true))); try { $result = $gateway->transaction()->sale($sale_args); } catch (Exception $e) { $this->log(__FUNCTION__, 'Error: Unable to process scheduled payment. Reason: ' . $e->getMessage()); return false; } if (!$result->success) { $this->log(__FUNCTION__, "Error: Unable to process scheduled payment: {$result->message}"); return false; } $transaction_id = $result->transaction->id; $this->log(__FUNCTION__, "Info: Successfully processed schedule payment, transaction id = {$transaction_id}"); WC_Subscriptions_Manager::process_subscription_payments_on_order($order); $this->log(__FUNCTION__, "Info: Completed processing of scheduled payment for order {$order->id}"); $order->add_order_note(sprintf(__('PayPal Braintree charge complete (Charge ID: %s)', 'woocommerce-gateway-paypal-braintree'), $transaction_id)); $order->payment_complete($transaction_id); }
function testGatewayGenerate_returnsAnEmptyCollectionWhenThereIsNoData() { $gateway = new Braintree_Gateway(array('environment' => 'development', 'merchantId' => 'integration_merchant_id', 'publicKey' => 'integration_public_key', 'privateKey' => 'integration_private_key')); $result = $gateway->settlementBatchSummary()->generate('2000-01-01'); $this->assertTrue($result->success); $this->assertEquals(0, count($result->settlementBatchSummary->records)); }
function testGateway_searchEmpty() { $query = array(); $query[] = Braintree_CreditCardVerificationSearch::creditCardCardholderName()->is('Not Found'); $gateway = new Braintree_Gateway(array('environment' => 'development', 'merchantId' => 'integration_merchant_id', 'publicKey' => 'integration_public_key', 'privateKey' => 'integration_private_key')); $collection = $gateway->creditCardVerification()->search($query); $this->assertEquals(0, $collection->maximumCount()); }
function testGateway_VersionDefaultsToTwo() { $gateway = new Braintree_Gateway(array('environment' => 'development', 'merchantId' => 'integration_merchant_id', 'publicKey' => 'integration_public_key', 'privateKey' => 'integration_private_key')); $encodedClientToken = $gateway->clientToken()->generate(); $clientToken = base64_decode($encodedClientToken); $version = json_decode($clientToken)->version; $this->assertEquals(2, $version); }
function testBadPaymentMethods() { $gateway = new Braintree_Gateway(array('clientId' => 'client_id$development$integration_client_id', 'clientSecret' => 'client_secret$development$integration_client_secret')); $result = $gateway->merchant()->create(array('email' => '*****@*****.**', 'countryCodeAlpha3' => 'USA', 'paymentMethods' => ['fake_money'])); $this->assertEquals(false, $result->success); $errors = $result->errors->forKey('merchant')->onAttribute('paymentMethods'); $this->assertEquals(Braintree_Error_Codes::MERCHANT_ACCOUNT_PAYMENT_METHODS_ARE_INVALID, $errors[0]->code); }
/** * @expectedException Braintree_Exception_Configuration * @expectedExceptionMessage Braintree_Configuration::merchantId needs to be set (or accessToken needs to be passed to Braintree_Gateway). */ function testConfigGetsAssertedValid() { Braintree_Configuration::environment('development'); //Braintree_Configuration::merchantId('integration_merchant_id'); Braintree_Configuration::publicKey('integration_public_key'); Braintree_Configuration::privateKey('integration_private_key'); $gateway = new Braintree_Gateway(Braintree_Configuration::$global); $gateway->addOn(); }
function testGatewayCreate() { $gateway = new Braintree_Gateway(array('environment' => 'development', 'merchantId' => 'integration_merchant_id', 'publicKey' => 'integration_public_key', 'privateKey' => 'integration_private_key')); $result = $gateway->merchantAccount()->create(self::$validParams); $this->assertEquals(true, $result->success); $merchantAccount = $result->merchantAccount; $this->assertEquals(Braintree_MerchantAccount::STATUS_PENDING, $merchantAccount->status); $this->assertEquals("sandbox_master_merchant_account", $merchantAccount->masterMerchantAccount->id); }
function testGatewayCreate() { $gateway = new Braintree_Gateway(array('environment' => 'development', 'merchantId' => 'integration_merchant_id', 'publicKey' => 'integration_public_key', 'privateKey' => 'integration_private_key')); $result = $gateway->customer()->create(array('firstName' => 'Mike', 'lastName' => 'Jones')); $this->assertEquals(true, $result->success); $customer = $result->customer; $this->assertEquals('Mike', $customer->firstName); $this->assertEquals('Jones', $customer->lastName); $this->assertNotNull($customer->merchantId); }
function testCreateWithAccessToken() { $credentials = Braintree_OAuthTestHelper::createCredentials(array('clientId' => 'client_id$development$integration_client_id', 'clientSecret' => 'client_secret$development$integration_client_secret', 'merchantId' => 'integration_merchant_id')); $gateway = new Braintree_Gateway(array('accessToken' => $credentials->accessToken)); $result = $gateway->customer()->create(array('firstName' => 'Mike', 'lastName' => 'Jones')); $this->assertEquals(true, $result->success); $customer = $result->customer; $this->assertEquals('Mike', $customer->firstName); $this->assertEquals('Jones', $customer->lastName); $this->assertNotNull($customer->merchantId); }
function testGatewayCreate() { $customer = Braintree_Customer::createNoValidate(); $gateway = new Braintree_Gateway(array('environment' => 'development', 'merchantId' => 'integration_merchant_id', 'publicKey' => 'integration_public_key', 'privateKey' => 'integration_private_key')); $result = $gateway->address()->create(array('customerId' => $customer->id, 'streetAddress' => '1 E Main St', 'locality' => 'Chicago', 'region' => 'IL', 'postalCode' => '60622')); $this->assertTrue($result->success); $address = $result->address; $this->assertEquals('1 E Main St', $address->streetAddress); $this->assertEquals('Chicago', $address->locality); $this->assertEquals('IL', $address->region); $this->assertEquals('60622', $address->postalCode); }
function testGatewayCreate() { $customer = Braintree_Customer::createNoValidate(); $gateway = new Braintree_Gateway(array('environment' => 'development', 'merchantId' => 'integration_merchant_id', 'publicKey' => 'integration_public_key', 'privateKey' => 'integration_private_key')); $result = $gateway->creditCard()->create(array('customerId' => $customer->id, 'cardholderName' => 'Cardholder', 'number' => '5105105105105100', 'expirationDate' => '05/12')); $this->assertTrue($result->success); $this->assertEquals($customer->id, $result->creditCard->customerId); $this->assertEquals('510510', $result->creditCard->bin); $this->assertEquals('5100', $result->creditCard->last4); $this->assertEquals('Cardholder', $result->creditCard->cardholderName); $this->assertEquals('05/2012', $result->creditCard->expirationDate); }
function testGatewayCreate_fromVaultedCreditCardNonce() { $customer = Braintree_Customer::createNoValidate(); $http = new Braintree_HttpClientApi(Braintree_Configuration::$global); $nonce = $http->nonce_for_new_card(array('credit_card' => array('number' => '4111111111111111', 'expirationMonth' => '11', 'expirationYear' => '2099'), 'share' => true)); $gateway = new Braintree_Gateway(array('environment' => 'development', 'merchantId' => 'integration_merchant_id', 'publicKey' => 'integration_public_key', 'privateKey' => 'integration_private_key')); $result = $gateway->paymentMethod()->create(array('customerId' => $customer->id, 'paymentMethodNonce' => $nonce)); $this->assertSame('411111', $result->paymentMethod->bin); $this->assertSame('1111', $result->paymentMethod->last4); $this->assertNotNull($result->paymentMethod->token); $this->assertNotNull($result->paymentMethod->imageUrl); }
function testGatewayCreateTransactionUsingNonce() { $http = new Braintree_HttpClientApi(Braintree_Configuration::$global); $nonce = $http->nonce_for_new_card(array("creditCard" => array("number" => "4111111111111111", "expirationMonth" => "11", "expirationYear" => "2099"), "share" => true)); $gateway = new Braintree_Gateway(array('environment' => 'development', 'merchantId' => 'integration_merchant_id', 'publicKey' => 'integration_public_key', 'privateKey' => 'integration_private_key')); $result = $gateway->transaction()->sale(array('amount' => '47.00', 'paymentMethodNonce' => $nonce)); $this->assertTrue($result->success); $transaction = $result->transaction; $this->assertEquals(Braintree_Transaction::AUTHORIZED, $transaction->status); $this->assertEquals(Braintree_Transaction::SALE, $transaction->type); $this->assertEquals('47.00', $transaction->amount); }
function testGatewayFind() { $paymentMethodToken = 'PAYPALToken-' . strval(rand()); $customer = Braintree_Customer::createNoValidate(); $http = new Braintree_HttpClientApi(Braintree_Configuration::$global); $nonce = $http->nonceForPayPalAccount(array('paypal_account' => array('consent_code' => 'PAYPAL_CONSENT_CODE', 'token' => $paymentMethodToken))); Braintree_PaymentMethod::create(array('customerId' => $customer->id, 'paymentMethodNonce' => $nonce)); $gateway = new Braintree_Gateway(array('environment' => 'development', 'merchantId' => 'integration_merchant_id', 'publicKey' => 'integration_public_key', 'privateKey' => 'integration_private_key')); $foundPayPalAccount = $gateway->paypalAccount()->find($paymentMethodToken); $this->assertSame('*****@*****.**', $foundPayPalAccount->email); $this->assertSame($paymentMethodToken, $foundPayPalAccount->token); $this->assertNotNull($foundPayPalAccount->imageUrl); }
function testGatewayCreate_whenSuccessful() { $creditCard = Braintree_SubscriptionTestHelper::createCreditCard(); $plan = Braintree_SubscriptionTestHelper::triallessPlan(); $gateway = new Braintree_Gateway(array('environment' => 'development', 'merchantId' => 'integration_merchant_id', 'publicKey' => 'integration_public_key', 'privateKey' => 'integration_private_key')); $result = $gateway->subscription()->create(array('paymentMethodToken' => $creditCard->token, 'planId' => $plan['id'])); Braintree_TestHelper::assertPrintable($result); $this->assertTrue($result->success); $subscription = $result->subscription; $this->assertEquals($creditCard->token, $subscription->paymentMethodToken); $this->assertEquals(0, $subscription->failureCount); $this->assertEquals($plan['id'], $subscription->planId); $this->assertEquals(Braintree_TestHelper::defaultMerchantAccountId(), $subscription->merchantAccountId); $this->assertEquals(Braintree_Subscription::ACTIVE, $subscription->status); }
function testCanExchangeNonceForEuropeBankAccount() { $gateway = new Braintree_Gateway(array('environment' => 'development', 'merchantId' => 'altpay_merchant', 'publicKey' => 'altpay_merchant_public_key', 'privateKey' => 'altpay_merchant_private_key')); $result = $gateway->customer()->create(); $this->assertTrue($result->success); $customer = $result->customer; $clientApi = new Braintree_HttpClientApi($gateway->config); $nonce = $clientApi->nonceForNewEuropeanBankAccount(array("customerId" => $customer->id, "sepa_mandate" => array("locale" => "de-DE", "bic" => "DEUTDEFF", "iban" => "DE89370400440532013000", "accountHolderName" => "Bob Holder", "billingAddress" => array("streetAddress" => "123 Currywurst Way", "extendedAddress" => "Lager Suite", "firstName" => "Wilhelm", "lastName" => "Dix", "locality" => "Frankfurt", "postalCode" => "60001", "countryCodeAlpha2" => "DE", "region" => "Hesse")))); $result = $gateway->paymentMethod()->create(array("customerId" => $customer->id, "paymentMethodNonce" => $nonce)); $this->assertTrue($result->success); $paymentMethod = $result->paymentMethod; $account = $gateway->paymentMethod()->find($paymentMethod->token); $this->assertEquals($paymentMethod->token, $account->token); $this->assertEquals($account->bic, "DEUTDEFF"); }
public function nonceForNewEuropeanBankAccount($options) { $clientTokenOptions = array('sepaMandateType' => 'business', 'sepaMandateAcceptanceLocation' => 'Rostock, Germany'); if (array_key_exists("customerId", $options)) { $clientTokenOptions["customerId"] = $options["customerId"]; unset($options["customerId"]); } $gateway = new Braintree_Gateway($this->_config); $clientToken = json_decode(base64_decode($gateway->clientToken()->generate($clientTokenOptions))); $options["authorization_fingerprint"] = $clientToken->authorizationFingerprint; $response = $this->post('/client_api/v1/sepa_mandates/', json_encode($options)); if ($response["status"] == 201 || $response["status"] == 202) { $body = json_decode($response["body"]); return $body->europeBankAccounts[0]->nonce; } else { throw new Exception(var_dump($response)); } }
function testGatewayAll_returnsAllAddOns() { $newId = strval(rand()); $addOnParams = array("amount" => "100.00", "description" => "some description", "id" => $newId, "kind" => "add_on", "name" => "php_add_on", "neverExpires" => "false", "numberOfBillingCycles" => "1"); $http = new Braintree_Http(Braintree_Configuration::$global); $path = Braintree_Configuration::$global->merchantPath() . "/modifications/create_modification_for_tests"; $http->post($path, array("modification" => $addOnParams)); $gateway = new Braintree_Gateway(array('environment' => 'development', 'merchantId' => 'integration_merchant_id', 'publicKey' => 'integration_public_key', 'privateKey' => 'integration_private_key')); $addOns = $gateway->addOn()->all(); foreach ($addOns as $addOn) { if ($addOn->id == $newId) { $actualAddOn = $addOn; } } $this->assertNotNull($actualAddOn); $this->assertEquals($addOnParams["amount"], $actualAddOn->amount); $this->assertEquals($addOnParams["description"], $actualAddOn->description); $this->assertEquals($addOnParams["id"], $actualAddOn->id); }
function testGatewayCreateTransactionFromTransparentRedirect() { $params = array('transaction' => array('customer' => array('first_name' => 'First'), 'credit_card' => array('number' => '5105105105105100', 'expiration_date' => '05/12'))); $trParams = array('transaction' => array('type' => Braintree_Transaction::SALE, 'amount' => '100.00')); $gateway = new Braintree_Gateway(array('environment' => 'development', 'merchantId' => 'integration_merchant_id', 'publicKey' => 'integration_public_key', 'privateKey' => 'integration_private_key')); $trData = $gateway->transparentRedirect()->transactionData(array_merge($trParams, array("redirectUrl" => "http://www.example.com"))); $queryString = Braintree_TestHelper::submitTrRequest($gateway->transparentRedirect()->url(), $params, $trData); $result = $gateway->transparentRedirect()->confirm($queryString); $this->assertTrue($result->success); $this->assertEquals('100.00', $result->transaction->amount); $this->assertEquals(Braintree_Transaction::SALE, $result->transaction->type); $this->assertEquals(Braintree_Transaction::AUTHORIZED, $result->transaction->status); $creditCard = $result->transaction->creditCardDetails; $this->assertEquals('US', $creditCard->customerLocation); $this->assertEquals('05/2012', $creditCard->expirationDate); $this->assertEquals('510510******5100', $creditCard->maskedNumber); $customer = $result->transaction->customerDetails; $this->assertequals('First', $customer->firstName); }
function testGatewayAll_returnsAllPlans() { $newId = strval(rand()); $params = array("id" => $newId, "billingDayOfMonth" => "1", "billingFrequency" => "1", "currencyIsoCode" => "USD", "description" => "some description", "name" => "php test plan", "numberOfBillingCycles" => "1", "price" => "1.00", "trialPeriod" => "false"); $http = new Braintree_Http(Braintree_Configuration::$global); $path = Braintree_Configuration::$global->merchantPath() . "/plans/create_plan_for_tests"; $http->post($path, array("plan" => $params)); $gateway = new Braintree_Gateway(array('environment' => 'development', 'merchantId' => 'integration_merchant_id', 'publicKey' => 'integration_public_key', 'privateKey' => 'integration_private_key')); $plans = $gateway->plan()->all(); foreach ($plans as $plan) { if ($plan->id == $newId) { $actualPlan = $plan; } } $this->assertNotNull($actualPlan); $this->assertEquals($params["billingDayOfMonth"], $actualPlan->billingDayOfMonth); $this->assertEquals($params["billingFrequency"], $actualPlan->billingFrequency); $this->assertEquals($params["currencyIsoCode"], $actualPlan->currencyIsoCode); $this->assertEquals($params["description"], $actualPlan->description); $this->assertEquals($params["name"], $actualPlan->name); $this->assertEquals($params["numberOfBillingCycles"], $actualPlan->numberOfBillingCycles); $this->assertEquals($params["price"], $actualPlan->price); }
/** * @expectedException Braintree_Exception_Configuration * @expectedExceptionMessage clientId needs to be set. */ function testAssertsHasCredentials() { $gateway = new Braintree_Gateway(array('clientSecret' => 'client_secret$development$integration_client_secret')); $gateway->merchant()->create(array('email' => '*****@*****.**', 'countryCodeAlpha3' => 'USA')); }
public function testComputeSignature() { $gateway = new Braintree_Gateway(array('clientId' => 'client_id$development$integration_client_id', 'clientSecret' => 'client_secret$development$integration_client_secret')); $urlToSign = 'http://localhost:3000/oauth/connect?business%5Bname%5D=We+Like+Spaces&client_id=client_id%24development%24integration_client_id'; $signature = $gateway->oauth()->computeSignature($urlToSign); $this->assertEquals("a36bcf10dd982e2e47e0d6a2cb930aea47ade73f954b7d59c58dae6167894d41", $signature); }
function testBuildConnectUrlWithoutOptionalParams() { $gateway = new Braintree_Gateway(array('clientId' => 'client_id$development$integration_client_id', 'clientSecret' => 'client_secret$development$integration_client_secret')); $url = $gateway->oauth()->connectUrl(); $queryString = parse_url($url)['query']; parse_str($queryString, $query); $this->assertEquals('client_id$development$integration_client_id', $query['client_id']); $this->assertArrayNotHasKey('merchant_id', $query); $this->assertArrayNotHasKey('redirect_uri', $query); $this->assertArrayNotHasKey('scope', $query); }
/** * Process a refund if supported * @param int $order_id * @param float $amount * @param string $reason * @return boolean True or false based on success, or a WP_Error object */ public function process_refund($order_id, $refund_amount = null, $reason = '') { $this->log(__FUNCTION__, "Info: Beginning processing refund/void for order {$order_id}"); $this->log(__FUNCTION__, "Info: Merchant ID = {$this->merchant_id}"); $order = wc_get_order($order_id); if (!$this->can_refund_order($order)) { $this->log(__FUNCTION__, "Error: Unable to refund/void order {$order_id}. Order has no transaction ID."); return false; } if (!$refund_amount) { $refund_amount = floatval($order->get_total()); } $this->log(__FUNCTION__, "Info: Amount = {$refund_amount}"); $transaction_id = $order->get_transaction_id(); require_once dirname(__FILE__) . '/../braintree_sdk/lib/Braintree.php'; $gateway = new Braintree_Gateway(array('accessToken' => $this->merchant_access_token)); // See if the transaction is not yet settled and should be voided instead of refunded // If for some reason the transaction cannot be found, an Exception will be thrown try { $transaction = $gateway->transaction()->find($transaction_id); } catch (Exception $e) { $this->log(__FUNCTION__, "Error: Unable to find transaction with transaction ID {$transaction_id}. Reason: " . $e->getMessage()); return false; } $this->log(__FUNCTION__, "Info: Order {$order_id} with transaction ID {$transaction_id} has status {$transaction->status}"); // Ref: https://developers.braintreepayments.com/reference/request/transaction/void/php // Ref: https://developers.braintreepayments.com/reference/request/transaction/refund/php $action_to_take = ''; switch ($transaction->status) { case Braintree_Transaction::AUTHORIZED: case Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT: case Braintree_Transaction::SETTLEMENT_PENDING: $action_to_take = "void"; break; case Braintree_Transaction::SETTLED: case Braintree_Transaction::SETTLING: $action_to_take = "refund"; break; } if (empty($action_to_take)) { $this->log(__FUNCTION__, "Error: The transaction cannot be voided nor refunded in its current state: state = {$transaction->status}"); return false; } // Only void transaction when refund amount equals to order's total. if ('void' === $action_to_take && $refund_amount != $order->get_total()) { return new WP_Error('unable_to_void', __('Unable to void unsettled transaction when refunding partially.', 'woocommerce-gateway-paypal-braintree')); } try { if ("void" === $action_to_take) { $result = $gateway->transaction()->void($transaction_id); } else { $result = $gateway->transaction()->refund($transaction_id, $refund_amount); } } catch (Exception $e) { $this->log(__FUNCTION__, 'Error: The transaction cannot be voided nor refunded. Reason: ' . $e->getMessage()); return false; } if (!$result->success) { $this->log(__FUNCTION__, "Error: The transaction cannot be voided nor refunded - reason: = {$result->message}"); return false; } // Get the refund/void transaction ID $latest_transaction_id = $result->transaction->id; if ("void" === $action_to_take) { $order->add_order_note(sprintf(__('Voided - Void ID: %s - Reason: %s', 'woocommerce-gateway-paypal-braintree'), $latest_transaction_id, $reason)); $this->log(__FUNCTION__, "Info: Successfully voided order {$order_id}"); } else { $order->add_order_note(sprintf(__('Refunded %s - Refund ID: %s - Reason: %s', 'woocommerce-gateway-paypal-braintree'), wc_price($refund_amount), $latest_transaction_id, $reason)); $this->log(__FUNCTION__, "Info: Successfully refunded {$refund_amount} for order {$order_id}"); } return true; }
function testHandlesEuropeBankAccounts() { $gateway = new Braintree_Gateway(array('environment' => 'development', 'merchantId' => 'altpay_merchant', 'publicKey' => 'altpay_merchant_public_key', 'privateKey' => 'altpay_merchant_private_key')); $result = $gateway->customer()->create(); $this->assertTrue($result->success); $customer = $result->customer; $clientApi = new Braintree_HttpClientApi($gateway->config); $nonce = $clientApi->nonceForNewEuropeanBankAccount(array("customerId" => $customer->id, "sepa_mandate" => array("locale" => "de-DE", "bic" => "DEUTDEFF", "iban" => "DE89370400440532013000", "accountHolderName" => "Bob Holder", "billingAddress" => array("streetAddress" => "123 Currywurst Way", "extendedAddress" => "Lager Suite", "firstName" => "Wilhelm", "lastName" => "Dix", "locality" => "Frankfurt", "postalCode" => "60001", "countryCodeAlpha2" => "DE", "region" => "Hesse")))); $transactionResult = $gateway->transaction()->sale(array("customerId" => $customer->id, "paymentMethodNonce" => $nonce, "merchantAccountId" => "fake_sepa_ma", "amount" => 100)); $this->assertTrue($transactionResult->success); $collection = $gateway->transaction()->search(array(Braintree_TransactionSearch::customerId()->is($customer->id), Braintree_TransactionSearch::europeBankAccountIban()->is("DE89370400440532013000"))); $this->assertEquals(1, $collection->maximumCount()); $this->assertEquals($transactionResult->transaction->id, $collection->firstItem()->id); }
/** * Cancel authorization * * @param int $order_id */ public function cancel_payment($order_id) { $order = wc_get_order($order_id); if (in_array($order->payment_method, array('paypalbraintree_cards', 'paypalbraintree_paypal'))) { require_once dirname(__FILE__) . '/braintree_sdk/lib/Braintree.php'; $trans_id = get_post_meta($order_id, '_transaction_id', true); $token = get_option('wc_paypal_braintree_merchant_access_token', ''); $gateway = new Braintree_Gateway(array('accessToken' => $token)); $transaction_details = false; if ($trans_id) { try { $transaction_details = $gateway->transaction()->find($trans_id); } catch (Exception $e) { $this->log(__FUNCTION__, "Error: Unable to find transaction with transaction ID {$trans_id}. Reason: " . $e->getMessage()); $order->add_order_note(__('Unable to void charge!', 'woocommerce-gateway-paypal-braintree') . ' ' . $e->getMessage()); return; } } if ($trans_id && 'authorized' === $transaction_details->status) { try { $result = $gateway->transaction()->void($trans_id); $order->add_order_note(sprintf(__('PayPal Braintree charge voided (Charge ID: %s)', 'woocommerce-gateway-paypal-braintree'), $result->transaction->id)); delete_post_meta($order->id, '_pp_braintree_charge_captured'); delete_post_meta($order->id, '_transaction_id'); } catch (Exception $e) { $this->log(__FUNCTION__, 'Error: Unable to void charge. Reason: ' . $e->getMessage()); $order->add_order_note(__('Unable to void charge!', 'woocommerce-gateway-paypal-braintree') . ' ' . $e->getMessage()); } } } }
function testGatewayRejectionOnApplicationIncomplete() { $gateway = new Braintree_Gateway(array('clientId' => 'client_id$development$integration_client_id', 'clientSecret' => 'client_secret$development$integration_client_secret')); $result = $gateway->merchant()->create(array('email' => '*****@*****.**', 'countryCodeAlpha3' => 'USA', 'paymentMethods' => array('credit_card', 'paypal'))); $gateway = new Braintree_Gateway(array('accessToken' => $result->credentials->accessToken)); $result = $gateway->transaction()->sale(array('amount' => '4000.00', 'creditCard' => array('number' => '4111111111111111', 'expirationDate' => '05/20'))); $this->assertFalse($result->success); $transaction = $result->transaction; $this->assertEquals(Braintree_Transaction::APPLICATION_INCOMPLETE, $transaction->gatewayRejectionReason); }
public function testBuildConnectUrlWithPaymentMethods() { $gateway = new Braintree_Gateway(array('clientId' => 'client_id$development$integration_client_id', 'clientSecret' => 'client_secret$development$integration_client_secret')); $url = $gateway->oauth()->connectUrl(array('paymentMethods' => array('credit_card', 'paypal'))); $queryString = parse_url($url)['query']; parse_str($queryString, $query); $this->assertEquals(array('credit_card', 'paypal'), $query['payment_methods']); }