Reputation: 89
I am facing a challenge while using Braintree. I am creating a subscription which is working perfectly fine. I have used Web hooks to trace status of subscription.
But I am failed to track subscription status when user's connection is timed out and Subscription status failed due to Processor declines error. This case is handled when connection is there, as I get reply at time of subscription creation.
I have checked various resources but failed to address my issue. I am using following code in PHP to create subscription.
function createSubscription($customerPaymentToken, $planID, $arrIds)
{
$result = null;
try
{
$result["error"] =0;
$result["result"]= null;
if($customerPaymentToken ==null || empty($customerPaymentToken))
{
$result["error"]= BT_NULL_OR_INVALID_CUSTOMER_PAYMENT_METHOD;
return $result;
}
if($planID== null || empty($planID))
{
$result["error"] = BT_NULL_OR_INVLAID_PLAN_ID;
return $result;
}
if(!is_array($arrIds) || $arrIds==null || count($arrIds)==0)
{
$result["error"]= BT_NULL_OR_EMPTY_UNIQUE_IDs;
return $result;
}
for ($i=0; $i<count($arrIds); $i++)
{
// Create the subscription using the Braintree API
$response = $this->gateway->subscription()->create([
'paymentMethodToken' => $customerPaymentToken, // Use the customer's payment method token
'planId' => $planID,
'options' => [
'startImmediately' => true, // Start the subscription immediately
],
'id'=>$arrIds[$i],
]);
if ($response->success)
{
// Subscription creation was successful
$tmp = array();
$tmp["sid"] = $arrIds[$i]; //$response->subscription->id;
$tmp["error"] = 0;
$tmp["response"]= $response;
$result["result"][] = $tmp;
}
else
{
// Subscription creation failed
$tmp = array();
$tmp["sid"] = $arrIds[$i];// 0;
$tmp["error"] = BT_FAILED_TO_SUBSCRIBE_PLAN;
$tmp["response"] = $response;
$result["result"][] = $tmp;
}
}
}
catch(Exception $ex)
{
$result["error"] = BT_EXCEPTION_OCCURRED_WHILE_SUBSCRIBING_PLAN;
$result["ex"] = $ex;
}
return $result;
}
Upvotes: 0
Views: 32
Reputation: 11
What is the currently configured timeout on the client? The Braintree gateway has an official timeout of 60 secs, so technically the subscription creation can take up to that amount of time.
Unfortunately, if you have a custom timeout set of less than 60 secs then they cannot guarantee a response back within that time. You'll likely need to implement some retry logic to have the client retry to check the state of the subscription creation after the timeout.
See best practice for timeouts here: https://developer.paypal.com/braintree/docs/reference/general/best-practices/php#timeouts
Upvotes: 1