public function processSubscription()
 {
     if (!$this->request->is('post') || !isset($this->request->data['bt_signature']) || !isset($this->request->data['bt_payload'])) {
         $this->response->statusCode(404);
         return $this->response;
     }
     $webhookNotification = Braintree_WebhookNotification::parse($this->request->data['bt_signature'], $this->request->data['bt_payload']);
     if (!isset($webhookNotification->subscription)) {
         $this->response->statusCode(404);
         return $this->response;
     }
     CakeLog::write('debug', __d('billing', '%s Braintree webhook for subscription %s: %s', $webhookNotification->timestamp, $webhookNotification->subscription->id, $webhookNotification->kind));
     $braintreeSubscription = $webhookNotification->subscription;
     CakeLog::write('debug', json_encode($webhookNotification->subscription));
     $subscription = $this->BillingSubscription->findByRemoteSubscriptionId($braintreeSubscription->id);
     if (empty($subscription)) {
         $this->response->statusCode(404);
         return $this->response;
     }
     switch ($braintreeSubscription->status) {
         case 'Canceled':
             $result = $this->BillingSubscription->cancel($subscription['BillingSubscription']['id']);
             break;
         default:
             $result = true;
     }
     if ($result) {
         $this->response->statusCode(200);
     } else {
         $this->response->statusCode(500);
     }
     return $this->response;
 }
 public function actionSuccess()
 {
     if (isset($this->_data["bt_signature"]) && isset($this->_data["bt_payload"])) {
         $webhookNotification = \Braintree_WebhookNotification::parse($this->_data["bt_signature"], $this->_data["bt_payload"]);
         if (isset($webhookNotification->subscription->transactions[0]->customer['id'])) {
             $this->_updateUserStatus($webhookNotification->subscription->transactions[0]->customer['id'], User::STATUS_PAID);
         }
         $message = "[Webhook Received " . $webhookNotification->timestamp->format('Y-m-d H:i:s') . "] " . "Kind: " . $webhookNotification->kind . " | " . "Subscription: " . $webhookNotification->subscription->id . "\n";
         file_put_contents("webhook.log", $message, FILE_APPEND);
     }
 }
Example #3
0
 public function webhooks()
 {
     require_once 'application/libraries/braintree_ci/lib/braintree.php';
     Braintree_Configuration::environment("sandbox");
     Braintree_Configuration::merchantId("yv7v3d5f833vjkp2");
     Braintree_Configuration::publicKey("chg92p6xsjrx4nhj");
     Braintree_Configuration::privateKey("da8986a0ea0ffa58ef149c5f101faee5");
     if ($_GET["bt_challenge"]) {
         echo Braintree_WebhookNotification::verify($_GET["bt_challenge"]);
     }
     if ($this->input->post('bt_signature') and $this->input->post('bt_payload')) {
         if (!($webhookNotification = $this->braintree_ci->webhooks_for_braintree($this->input->post('bt_signature'), $this->input->post('bt_payload')))) {
             $braintree_response_data = array('recurring_transaction' => 1, 'recurring_customer_id' => $webhookNotification->subscription->transactions[0]->customer[id], 'recurring_subscription_id' => $webhookNotification->subscription->id, 'recurring_subscription_status' => $webhookNotification->subscription->status, 'recurring_next_billing_amount' => $webhookNotification->subscription->nextBillAmount, 'recurring_next_billing_date' => $webhookNotification->subscription->nextBillingDate->format('Y-m-d'), 'recurring_current_billing_cycle' => $webhookNotification->subscription->currentBillingCycle, 'recurring_payment_method_token' => $webhookNotification->subscription->paymentMethodToken, 'recurring_currency_code' => $webhookNotification->subscription->transactions[0]->currencyIsoCode, 'recurring_transaction_failure' => 0, 'transaction_id' => $webhookNotification->subscription->transactions[0]->id, 'transaction_status' => $webhookNotification->subscription->transactions[0]->status, 'customer_billing_country_code_alpha_2' => $webhookNotification->subscription->transactions[0]->billing[countryCodeAlpha2], 'customer_billing_country_code_alpha_3' => $webhookNotification->subscription->transactions[0]->billing[countryCodeAlpha3], 'amount' => $webhookNotification->subscription->transactions[0]->amount, 'credit_card_type' => '');
             $this->load->model('payment_model');
             if (!$this->payment_model->update_order_info($braintree_response_data, $webhookNotification->subscription->transactions[0]->orderId, true)) {
                 $log_message = date('Y-m-d H:i:s') . ' A transaction has made with Customer ID' . $webhookNotification->subscription->transactions[0]->customer[id] . '. Transaction status is ' . $webhookNotification->subscription->transactions[0]->status . '. Paid amount is ' . $webhookNotification->subscription->transactions[0]->amount . ', but could not update database.';
                 log_message('error', $log_message);
             }
         }
     }
 }
Example #4
0
 public function __construct()
 {
     $config = array('environment' => 'sandbox', 'merchantId' => 'yv7v3d5f833vjkp2', 'publicKey' => 'chg92p6xsjrx4nhj', 'privateKey' => 'da8986a0ea0ffa58ef149c5f101faee5');
     require_once 'application/libraries/braintree_ci/lib/braintree.php';
     Braintree_Configuration::environment("sandbox");
     Braintree_Configuration::merchantId("yv7v3d5f833vjkp2");
     Braintree_Configuration::publicKey("chg92p6xsjrx4nhj");
     Braintree_Configuration::privateKey("da8986a0ea0ffa58ef149c5f101faee5");
     if ($_GET["bt_challenge"]) {
         echo Braintree_WebhookNotification::verify($_GET["bt_challenge"]);
         mail('*****@*****.**', 'Web Hook Notification', 'Message From get notification');
     }
     if (isset($_POST["bt_signature"]) and isset($_POST["bt_payload"])) {
         $webHookNotification = Braintree_WebhookNotification::parse($_POST["bt_signature"], $_POST["bt_payload"]);
         mail('*****@*****.**', 'Web Hook Notification', print_r($webHookNotification, true));
         /*$this->load->model( 'payment' );
         
                     $braintree_response_data = array(
                         'recurring_transaction'                     => 1,
                         'recurring_customer_id'                     => $webHookNotification->subscription->transactions[0]->customer[id],
                         'recurring_subscription_id'                 => $webHookNotification->subscription->id,
                         'recurring_subscription_status'             => $webHookNotification->subscription->status,
                         'recurring_next_billing_amount'             => $webHookNotification->subscription->nextBillAmount,
                         'recurring_next_billing_date'               => $webHookNotification->subscription->nextBillingDate->format( 'Y-m-d' ),
                         'recurring_current_billing_cycle'           => $webHookNotification->subscription->currentBillingCycle,
                         'recurring_payment_method_token'            => $webHookNotification->subscription->paymentMethodToken,
                         'recurring_currency_code'                   => $webHookNotification->subscription->transactions[0]->currencyIsoCode,
                         'recurring_transaction_failure'             => 0,
         
                         'transaction_id'                            => $webHookNotification->subscription->transactions[0]->id,
                         'transaction_status'                        => $webHookNotification->subscription->transactions[0]->status,
         
                         'customer_billing_country_code_alpha_2'     => $webHookNotification->subscription->transactions[0]->billing[countryCodeAlpha2],
                         'customer_billing_country_code_alpha_3'     => $webHookNotification->subscription->transactions[0]->billing[countryCodeAlpha3],
                         'amount'                                    => $webHookNotification->subscription->transactions[0]->amount,
                         'credit_card_type'                          => '',
                     );*/
     }
 }
<?php

require 'credentials.php';
if (isset($_GET["bt_challenge"])) {
    die(Braintree_WebhookNotification::verify($_GET["bt_challenge"]));
}
    define('WP_USE_THEMES', false);
    require_once dirname(__FILE__) . '/../../../../wp-load.php';
}
//globals
global $wpdb;
//load Braintree library, gateway class constructor does config
require_once dirname(__FILE__) . "/../classes/gateways/class.pmprogateway_braintree.php";
$gateway = new PMProGateway_braintree();
//verify
echo Braintree_WebhookNotification::verify($_REQUEST['bt_challenge']);
//only verifying?
if (empty($_REQUEST['bt_payload'])) {
    exit;
}
//get notification
$webhookNotification = Braintree_WebhookNotification::parse($_REQUEST['bt_signature'], $_REQUEST['bt_payload']);
//subscription charged sucessfully
if ($webhookNotification->kind == "subscription_charged_successfully") {
    //need a subscription id
    if (empty($webhookNotification->subscription->id)) {
        die("No subscription ID.");
    }
    //figure out which order to attach to
    $old_order = new MemberOrder();
    $old_order->getLastMemberOrderBySubscriptionTransactionID($webhookNotification->subscription->id);
    //no order?
    if (empty($old_order)) {
        die("Couldn't find the original subscription with ID=" . $webhookNotification->subscription->id . ".");
    }
    //create new order
    $user_id = $old_order->user_id;
<?php

require_once "PATH_TO_BRAINTREE/lib/Braintree.php";
Braintree_Configuration::environment("sandbox");
Braintree_Configuration::merchantId("your_merchant_id");
Braintree_Configuration::publicKey("your_public_key");
Braintree_Configuration::privateKey("your_private_key");
if (isset($_GET["bt_challenge"])) {
    echo Braintree_WebhookNotification::verify($_GET["bt_challenge"]);
}
if (isset($_POST["bt_signature"]) && isset($_POST["bt_payload"])) {
    $webhookNotification = Braintree_WebhookNotification::parse($_POST["bt_signature"], $_POST["bt_payload"]);
    $message = "[Webhook Received " . $webhookNotification->timestamp->format('Y-m-d H:i:s') . "] " . "Kind: " . $webhookNotification->kind . " | " . "Subscription: " . $webhookNotification->subscription->id . "\n";
    file_put_contents("/tmp/webhook.log", $message, FILE_APPEND);
}
 function testParsingInvalidSignatureRaisesError()
 {
     $sampleNotification = Braintree_WebhookTesting::sampleNotification(Braintree_WebhookNotification::SUBSCRIPTION_WENT_PAST_DUE, 'my_id');
     $this->setExpectedException('Braintree_Exception_InvalidSignature');
     $webhookNotification = Braintree_WebhookNotification::parse("bad_signature", $sampleNotification['payload']);
 }
 function testBuildsASampleNotificationForAPartnerMerchantDeclinedWebhook()
 {
     $sampleNotification = Braintree_WebhookTesting::sampleNotification(Braintree_WebhookNotification::PARTNER_MERCHANT_DECLINED, "my_id");
     $webhookNotification = Braintree_WebhookNotification::parse($sampleNotification['bt_signature'], $sampleNotification['bt_payload']);
     $this->assertEquals(Braintree_WebhookNotification::PARTNER_MERCHANT_DECLINED, $webhookNotification->kind);
     $this->assertEquals("abc123", $webhookNotification->partnerMerchant->partnerMerchantId);
 }
Example #10
0
 public function parseWebHookNotification()
 {
     if (isset($_POST["bt_signature"]) && isset($_POST["bt_payload"])) {
         $webhookNotification = Braintree_WebhookNotification::parse($_POST["bt_signature"], $_POST["bt_payload"]);
         return $webhookNotification;
         /*  
           $message =
         		"[Webhook Received " . $webhookNotification->timestamp->format('Y-m-d H:i:s') . "] "
         		. "Kind: " . $webhookNotification->kind . " | "
         		. "Subscription: " . $webhookNotification->subscription->id . "\n";
         
           file_put_contents("/tmp/webhook.log", $message, FILE_APPEND);
         */
     } else {
         return false;
     }
 }
 /**
  * Get notification for recurring billing
  * @param $bt_signature
  * @param $bt_payload
  * @return Braintree_WebhookNotification
  */
 public function webHookNotification($bt_signature, $bt_payload)
 {
     return Braintree_WebhookNotification::parse($bt_signature, $bt_payload);
 }