mardi 28 juin 2016

Braintree was unable to perform a charge: Custom field is invalid: description

we are using Laravel 5.2 with Cashier/Braintree.

We had a subscription based website which works find. These were for monthly recurring membership packages.

We are now trying to add in a single charge for a few days taster. It seems to all work fine until the last step when we get an error saying the description is invalid. Looking at the documentation and source code we are not able to find why this is not working.

Here is the code:

$plan = $request->get('plan');
$coupon = $request->get('coupon');
$user = $this->user;

if ( false === $user->hasBraintreeId() ){
    $customer = $user->createAsBraintreeCustomer($request->get('payment_method_nonce'), array());
    $user->setCustomerId($customer->id);
    $user->save();
} else {
    $customer = $user->asBraintreeCustomer();
}

if ( $plan == '5-day-taster' ){ // One off payment
    $this->user->invoiceFor('5-day-taster', 5.99);
} elseif ( $plan == '10-day-taster' ){ // One off payment
    $this->user->invoiceFor('10-day-taster', 9.99);
} elseif ( $plan == 'personal-standard' ){ // Subscription(s)
    if ( isset($coupon) && !empty($coupon) )
        $this->user->newSubscription($plan, $plan)
            ->withCoupon($coupon)
            ->create($request->get('payment_method_nonce'), [
                'email' => $this->user->email,
            ]);
    else
        $this->user->newSubscription($plan, $plan)
            ->create($request->get('payment_method_nonce'), [
                'email' => $this->user->email,
            ]);
} 

We get the following error:

Exception in Billable.php line 41:
Braintree was unable to perform a charge: Custom field is invalid: description.

Source code from Braintree (not edited, just using for reference):

/**
     * Make a "one off" charge on the customer for the given amount.
     *
     * @param  int  $amount
     * @param  array  $options
     * @return BraintreeTransaction
     */
    public function charge($amount, array $options = [])
    {
        $customer = $this->asBraintreeCustomer();

        $response = BraintreeTransaction::sale(array_merge([
            'amount' => $amount * (1 + ($this->taxPercentage() / 100)),
            'paymentMethodToken' => $customer->paymentMethods[0]->token,
            'options' => [
                'submitForSettlement' => true,
            ],
            'recurring' => true,
        ], $options));

        if (! $response->success) {
            throw new Exception('Braintree was unable to perform a charge: '.$response->message);
        }

        return $response;
    }

    /**
     * Invoice the customer for the given amount.
     *
     * @param  string  $description
     * @param  int  $amount
     * @param  array  $options
     * @return BraintreeTransaction
     */
    public function invoiceFor($description, $amount, array $options = [])
    {
        return $this->charge($amount, array_merge($options, [
            'customFields' => [
                'description' => $description,
            ],
        ]));
    }

Thank you in advance for any advice given.

Aucun commentaire:

Enregistrer un commentaire