TransWikia.com

Magento 2 REST API For Place Order gives "transaction declined" error after successful Payment For Payfort Payment Gateway

Magento Asked by Shahzad H. on February 17, 2021

I am using Payfort extension to process credit Card Payments online for Payfort Payment Gateway. For Website it’s working Fine and Placing an order after successful payment.

But when I am trying to place an order via API after successful payment via it’s iOs/Android SDK.
It’s giving me this Error.

Transaction has been declined. Please try again later.

This is How I am passing each information I received from Payfort after Successful Payment in this API.

Endpoint

/rest/V1/carts/mine/set-payment-information

Request

{
"billingAddress": {
"city": "Dubai",
"countryId": "AE",
"customerAddressId": "2885",
"customerId": "3438",
"fax": "+971521231234",
"firstname": "Test",
"lastname": "User",
"postcode": "1111",
"region": "Dubai",
"regionCode": "DXB",
"regionId": "597",
"street": [
"Street 44, Oud Maitha"
],
"telephone": "+971521231234"
},
"cartId": "24361",
"paymentMethod": {
"additional_data": {
"amount": "29995",
"authorization_code": "614835",
"card_number": "411111******1111",
"card_holder_name": "Test User ",
"customer_email": "[email protected]",
"customer_ip": "43.241.194.95",
"expiry_date": "2102",
"fort_id": "169996200000452654",
"is_active_payment_token_enabler": false,
"merchant_reference": "XXXXXXXX",
"payment_option": "VISA",
"sdk_token": "c85bf903408b45a19a194710a941607a",
"token_name": "82f62b316c3246908ba29c8f2e683f45"
},
"method": "md_payfort"
}
}

I am getting "True" in the response of this API call.

Then, I am using Create Order API of Magento to Place an Order.

Endpoint

/rest/V1/carts/mine/order

Request

{
"paymentMethod": {
"method": "md_payfort"
},
"shippingMethod": {
"additionalProperties": {},
"carrier_code": "freeshipping",
"method_code": "freeshipping"
}
}

After debug, I found that this error is coming from this file.

vendor/magento/module-payment/Gateway/Command/GatewayCommand.php

And it’s because the "execute" method in this class is sending the Payment request again to Payfort and they receive the "Signature mismatch" error from Payfort because of the unauthorized request. It should not send the Payment request again to Payfort.
I am not able to figure out how to prevent Magento to make this Payment request and just Place the Order.

If anyone can help me out to solve this problem and place an order successfully would be really appreciated.

One Answer

Even thought I have done lots of work on the checkout API have never used the endpoint /V1/carts/mine/order.

The normal way to place an order in my experience is to make a POST to /V1/carts/mine/payment-information (or for guest carts /V1/guest-carts/:cartId/payment-information). Both of these map to the method savePaymentInformationAndPlaceOrder which additionally removes the need for a 2nd API call after saving the payment data.

Take a look here which gives a great breakdown of the entire process. https://devdocs.magento.com/guides/v2.4/rest/tutorials/orders/order-create-order.html

I hope this helps.

Answered by Craig on February 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP