Magento – Payment with REST API

braintreemagento2payment-methodsrest api

I'm using Braintree payment gateway and having issues when tying to make a payment through REST API. I am calling carts/mine/payment-information endpoint with following values.

Header:

Authorization: Bearer customer-token
Content-type: application/json

Body:

{
  "paymentMethod": {
    "po_number": "",
    "method": "braintree",
    "additional_data":{ 
        "cc_last4":"1111", 
        "store_in_vault":true, 
        "payment_method_nonce":"nonce-goes-here", 
        "cc_token":"", 
        "device_data":"", 
        "cc_type":"Visa", 
        "cc_exp_year":"2055", 
        "cc_exp_month":"01" 
    },
    "extension_attributes": {
      "agreement_ids": [
        "string"
      ]
    }
  },
  "billingAddress": {
    "id": 0,
    "region": "Victoria",
    "region_id": 546,
    "region_code": "VIC",
    "country_id": "AU",
    "street": [
      "Main St"
    ],
    "company": "Company Name",
    "telephone": "987654321",
    "fax": "",
    "postcode": "3000",
    "city": "Melbourne",
    "firstname": "Firstname",
    "lastname": "Lastname",
    "middlename": "Middlename",
    "prefix": "Mr",
    "suffix": "",
    "vat_id": "",
    "customer_id": 0,
    "email": "name@domain.name",
    "same_as_billing": 0,
    "customer_address_id": 0,
    "save_in_address_book": 0,
    "extension_attributes": {},
    "custom_attributes": {}
  }
} 

I have generated a braintree token and used that token to generate the nonce. With the use of nonce, I was able to create a Braintree transaction as below.

   $result = Braintree_Transaction::sale([
        'amount' => $transactionAmount,
        'paymentMethodNonce' => $nonceFromTheClient,
        'options' => [
            'submitForSettlement' => True
        ]
    ]);

The transaction gets successful but when I try to place an order on Magento using my transaction details, I keep getting the following error.

Transaction has been declined. Please try again later

Can anyone advise me what's wrong with my process/ request please?

Thanks!

Best Answer

Here how Braintree integration looks like (https://developers.braintreepayments.com/start/overview) enter image description here

So you need do the following

1) Get client token from Magento server. I didn't find rest endpoint for it so I've created a custom extension for this purpose (https://github.com/troublediehard/mma-customapi)

2) Then use sdk and token to get nonce from you card

3) And send your request with nonce data like this

{
  paymentMethod: {
    method: 'braintree',
    additional_data: { 
      payment_method_nonce: nonce, 
    }
  }
}
Related Topic