Can't Parse webhook of razropay - php

The webhook pings successfully, but I'm unable to parse it, I spent 4 hours still no luck, I tried all methods but still failed,I just don't know why is this happening , i have used it same for stripe webhook, it works but this is working for razor pay
Below is the header and response I receive on my webhook:
Source: https://webhook.site/
Headers
connection close
accept-encoding gzip
x-razorpay-signature f0087994b59ee5ec1b1828a87f794c89bb398033a428bb79971875e5f295322e
x-razorpay-event-id Gb5kMCdcAZ8jJ8
request-id Gb5kMCdcAZ8jJ8
content-type application/json
content-length 1053
user-agent Razorpay-Webhook/v1
host webhook.site
Raw content
{
"entity": "event",
"account_id": "acc_DOXUp08NQX6Sr9",
"event": "payment.captured",
"contains": [
"payment"
],
"payload": {
"payment": {
"entity": {
"id": "pay_Gb5kLMBMjON6So",
"entity": "payment",
"amount": 1132,
"currency": "USD",
"base_amount": 82174,
"base_currency": "INR",
"status": "captured",
"order_id": "order_Gb5kEv5w8pveU5",
"invoice_id": null,
"international": false,
"method": "card",
"amount_refunded": 0,
"refund_status": null,
"captured": true,
"description": null,
"card_id": "card_Gb5kLUZEfKlXF4",
"card": {
"id": "card_Gb5kLUZEfKlXF4",
"entity": "card",
"name": "231231231231231231",
"last4": "8826",
"network": "Visa",
"type": "prepaid",
"issuer": "SBIN",
"international": false,
"emi": false,
"sub_type": "business"
},
"bank": null,
"wallet": null,
"vpa": null,
"email": "niteshkmr366#gmail.com",
"contact": "+918169179450",
"notes": {
"ytsubme_account_email": "niteshkmr366#gmail.com",
"phone": "8169179450"
},
"fee": 1644,
"tax": 0,
"error_code": null,
"error_description": null,
"error_source": null,
"error_step": null,
"error_reason": null,
"acquirer_data": {
"auth_code": null
},
"created_at": 1613226609
}
}
},
"created_at": 1613226609
}
My PHP Code:
<?
$data = file_get_contents('php://input');
$obj = json_decode($data);
file_put_contents('data.txt',$data);
//data.txt file is still empty
?>

Thanks to #IMSoP,
For reminding me to sort out why it's empty before you worry about parsing it.
My webhook was pinging to the URL where login was necessity,
)more info : SESSIONS : i.e. if session wasn't set it was redirecting them to / page.)
Due to which I was getting empty data ,feeling so dumb about this.

Related

How to echo raw data if it has same name string with others

I have this POST Response from my online shop but the Raw Data has 2 TOKEN string i just want to echo the TOKEN form payment_method what will be PHP code for my problem?
And also in case i want to print the "message" from "transaction" what will be the code though? i already tried to search through internet but its hard for me to figure it out
here is the raw data
{
"transaction": {
"token": "TWN0rD9euove3BEUNRs0PPv2s9w", // this it the token that i dont want to echo
"created_at": "2019-07-05T09:34:21Z",
"updated_at": "2019-07-05T09:34:22Z",
"succeeded": true,
"transaction_type": "AddPaymentMethod",
"retained": false,
"state": "succeeded",
"message_key": "messages.transaction_succeeded",
"message": "Succeeded!",
"payment_method": {
"token": "BXtwsBABZ2z15DkyN5dbTWrwakV", //This is the token that i need to echo
"created_at": "2019-07-05T09:34:21Z",
"updated_at": "2019-07-05T09:34:22Z",
"email": "testemail#gmail.com",
"data": null,
"storage_state": "cached",
"test": false,
"metadata": null,
"callback_url": null,
"last_four_digits": "4242",
"first_six_digits": "424242",
"card_type": "visa",
"first_name": "First",
"last_name": "Last",
"month": 6,
"year": 2022,
"address1": null,
"address2": null,
"city": null,
"state": null,
"zip": "1000",
"country": null,
"phone_number": null,
"company": null,
"full_name": "Test Test",
"eligible_for_card_updater": true,
"shipping_address1": null,
"shipping_address2": null,
"shipping_city": null,
"shipping_state": null,
"shipping_zip": null,
"shipping_country": null,
"shipping_phone_number": null,
"payment_method_type": "credit_card",
"errors": [],
"fingerprint": "eeebce09a3bf9fba352df975a5c999998",
"verification_value": "XXX",
"number": "XXXX-XXXX-XXXX-4242"
}
} }
You can use json_decode with true as second parameter to convert it into array,
echo 'token: '.$arr['transaction']['payment_method']['token']."\n";
echo 'message: '.$arr['transaction']['message'];
Demo.
Output:
token: BXtwsBABZ2z15DkyN5dbTWrwakV
message: Succeeded!

How to get customer id from stripe event

I have set up a webhook where events sent from stripe are handled. I, however, have noticed that not all events share the same structure.
I am currently retrieving the customer this way:
$input = #file_get_contents("php://input");
$event_json = json_decode($input);
$customerId = $event_json->data->object->customer
After a day or two I found out that not all events contain the customer under $event_json->data->object->customer. Here is an example response:
{
"object": {
"id": "cus_Ac3Sx3Bn7cuvqB",
"object": "customer",
"account_balance": -3099,
"created": 1494163341,
"currency": "usd",
"default_source": "card_1AGpL4ByNDe65wcFOfqQZGCc",
"delinquent": false,
"description": "John John",
"discount": null,
"email": "john.john#john.com",
"livemode": false,
"metadata": {
},
"shipping": null,
"sources": {
"object": "list",
"data": [
{
"id": "card_1AGpL4ByNDe65wcFOfqQZGCc",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": "cus_Ac3Sx3Bn7cuvqB",
"cvc_check": "pass",
"dynamic_last4": null,
"exp_month": 7,
"exp_year": 2017,
"fingerprint": "lI2tl3FOGKOG7PcZ",
"funding": "credit",
"last4": "4242",
"metadata": {
},
"name": "John John",
"tokenization_method": null
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/customers/cus_Ac3Sx3Bn7cuvqB/sources"
},
"subscriptions": {
"object": "list",
"data": [
],
"has_more": false,
"total_count": 0,
"url": "/v1/customers/cus_Ac3Sx3Bn7cuvqB/subscriptions"
}
}
}
What I am asking is where I can see an example of all possible event structures so that I can make sure my webhook doesn't return Could not determine which URL to request: Stripe\Customer instance has invalid ID: (500 Internal Server Error)?
Note: I did see this question - How to get a customer ID from event object in stripe but there the only given way is $event_json->data->object->customer
Stripe webhooks share the same structure as the API endpoints for the relevant objects. In the example above, the object field is set to customer, so you can see the structure in the API docs at https://stripe.com/docs/api#customer_object . If you go to https://dashboard.stripe.com/account/webhooks and add an endpoint, and choose "Select types to send", you can see all the types of webhooks that will be sent to you.
There are a lot of events where it doesn't really make sense to look for a customer on the event, so it would probably be useful to only subscribe to the event types you want.

Shopify Api working with PHP

I am working with Shopify Webhook for order fulfillment(in PHP) now i want the payment details of "Information from the gateway" which are available in the shopify admin but not showing in Webhook details so anyone can please give any solution for this. following is the screenshot of details which i want to get in order.
These following are details which I want when any new order is confirmed:
1) Authorization key
2) Name on Credit card
3) Exp month
4) Exp year
details which i want to get in order
For this information you needs to use following shopify method
GET /admin/orders/#{id}/transactions.json
And following response you get:
HTTP/1.1 200 OK
{
"transactions": [
{
"id": 179259969,
"order_id": 450789469,
"amount": "209.00",
"kind": "refund",
"gateway": "bogus",
"status": "success",
"message": null,
"created_at": "2005-08-05T12:59:12-04:00",
"test": false,
"authorization": "authorization-key",
"currency": "USD",
"location_id": null,
"user_id": null,
"parent_id": null,
"device_id": null,
"receipt": {},
"error_code": null,
"source_name": "web"
},
{
"id": 389404469,
"order_id": 450789469,
"amount": "409.94",
"kind": "authorization",
"gateway": "bogus",
"status": "success",
"message": null,
"created_at": "2005-08-01T11:57:11-04:00",
"test": false,
"authorization": "authorization-key",
"currency": "USD",
"location_id": null,
"user_id": null,
"parent_id": null,
"device_id": null,
"receipt": {
"testcase": true,
"authorization": "123456"
},
"error_code": null,
"source_name": "web",
"payment_details": {
"credit_card_bin": null,
"avs_result_code": null,
"cvv_result_code": null,
"credit_card_number": "•••• •••• •••• 4242",
"credit_card_company": "Visa"
}
},
{
"id": 801038806,
"order_id": 450789469,
"amount": "250.94",
"kind": "capture",
"gateway": "bogus",
"status": "success",
"message": null,
"created_at": "2005-08-05T10:22:51-04:00",
"test": false,
"authorization": "authorization-key",
"currency": "USD",
"location_id": null,
"user_id": null,
"parent_id": null,
"device_id": null,
"receipt": {},
"error_code": null,
"source_name": "web"
}
]
}
And you want to more details than please refer the below link:
https://help.shopify.com/api/reference/transaction

How to redirect to URL received from JSON response using PHP

I using process.php to post to an API and receive the following response
{ "payment_request":
{
"id": "554efa8b701c4021be7310b8916cc30b",
"phone": null,
"email": null,
"buyer_name": null,
"amount": "12",
"purpose": "product-sleek",
"status": "Pending",
"send_sms": false,
"send_email": false,
"sms_status": null,
"email_status": null,
"shorturl": null,
"longurl": "https://www.example.com/#user/554efa8b701c4021be7310b8916cc30b",
"redirect_url": "https://example.com/order-received/",
"webhook": null,
"created_at": "2016-04-06T05:01:04.042Z",
"modified_at": "2016-04-06T05:01:04.042Z",
"allow_repeated_payments": false
},
"success": true
}
Now after receiving the response, I want to redirect the page to the longurl value in the response json, How should I do it?
use json_decode to parse json in array or object and then use header function to redirect it
$json = '{ "payment_request":
{
"id": "554efa8b701c4021be7310b8916cc30b",
"phone": null,
"email": null,
"buyer_name": null,
"amount": "12",
"purpose": "product-sleek",
"status": "Pending",
"send_sms": false,
"send_email": false,
"sms_status": null,
"email_status": null,
"shorturl": null,
"longurl": "https://www.example.com/#user/554efa8b701c4021be7310b8916cc30b",
"redirect_url": "https://example.com/order-received/",
"webhook": null,
"created_at": "2016-04-06T05:01:04.042Z",
"modified_at": "2016-04-06T05:01:04.042Z",
"allow_repeated_payments": false
},
"success": true
}';
$json_array = json_decode($json, true);
$longurl = $json_array['payment_request']['longurl'];
header("Location: $longurl");

Counting array in API JSON Response

I'm trying to do a simple count of how many refunds are in my Stripe Response but count() isn't working and I don't really know any other way of achieving this.
Could anyone point me in the right direction?
$retrieve_event = Stripe_Event::retrieve("evt_00000000000000");
$event_json_id = json_decode($retrieve_event);
$refund_array = $event_json_id->{'data'}->{'object'}->{'refunds'};
die(count($refund_array));
This is the response of $retrieve_event
{
"created": 1326853478,
"livemode": false,
"id": "evt_00000000000000",
"type": "charge.refunded",
"object": "event",
"request": null,
"data": {
"object": {
"id": "ch_00000000000000",
"object": "charge",
"created": 1402433517,
"livemode": false,
"paid": true,
"amount": 1000,
"currency": "usd",
"refunded": true,
"card": {
"id": "card_00000000000000",
"object": "card",
"last4": "0028",
"type": "Visa",
"exp_month": 8,
"exp_year": 2015,
"fingerprint": "a5KWlTcrmCYk5DIYa",
"country": "US",
"name": "First Last",
"address_line1": "null",
"address_line2": null,
"address_city": "null",
"address_state": "null",
"address_zip": "null",
"address_country": "US",
"cvc_check": null,
"address_line1_check": "fail",
"address_zip_check": "pass",
"customer": "cus_00000000000000"
},
"captured": true,
"refunds": [
{
"id": "re_104CKt4uGeYuVLAahMwLA2TK",
"amount": 100,
"currency": "usd",
"created": 1402433533,
"object": "refund",
"charge": "ch_104CKt4uGeYuVLAazSyPqqLV",
"balance_transaction": "txn_104CKt4uGeYuVLAaSNZCR867",
"metadata": {}
},
{
"id": "re_104CKt4uGeYuVLAaDIMHoIos",
"amount": 200,
"currency": "usd",
"created": 1402433539,
"object": "refund",
"charge": "ch_104CKt4uGeYuVLAazSyPqqLV",
"balance_transaction": "txn_104CKt4uGeYuVLAaqSwkNKPO",
"metadata": {}
},
{
"id": "re_4CL6n1r91dY5ME",
"amount": 700,
"currency": "usd",
"created": 1402434306,
"object": "refund",
"charge": "ch_4CL6FNWhGzVuAV",
"balance_transaction": "txn_4CL6qa4vwlVaDJ"
}
],
"balance_transaction": "txn_00000000000000",
"failure_message": null,
"failure_code": null,
"amount_refunded": 1000,
"customer": "cus_00000000000000",
"invoice": null,
"description": "this is a description",
"dispute": null,
"metadata": {},
"statement_description": "this is a description",
"fee": 0
}
}
}
It's just the way you're trying to output the count. This example outputs 3:
echo count($refund_array);
exit;
Whereas this example doesn't:
die(count($refund_array));
The reason is because you're simply passing in an integer into die(). From the manual:
If status is an integer, that value will be used as the exit status and not printed. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.
This example works because the message is a string:
die('Count: ' . count($refund_array)); // Count: 3
...or:
die((string) count($refund_array)); // 3
The count() just needs to be cast as a string:
die((string) count($refund_array));
As per new version of Stripe API which was released on 2014-06-17, Refunds object is modified for Charges Method.
Now you can directly get refund count using total_count parameter in refunds object.
data->object->refund->total_count

Categories