creating invoice is not working through Paypal REST API - php

I am facing the problem to create the PayPal invoice through the REST API. I got the access token by using auth API of https://api.sandbox.paypal.com/v1/oauth2/token. I am passing the token in the HEADER to create an invoice in the create invoice API of https://api.sandbox.paypal.com/v1/invoicing/invoices/.
Header:
Below is my JSON body.
{
"merchant_info": {
"email": "krpdas-facilitator#gmail.com",
"first_name": "prabhu",
"last_name": "kr",
"business_name": "prabhu kr",
"phone": {
"country_code": "91",
"national_number": "2222233333"
},
"address": {
"line1": "xxxxxxxxxxx,xxxxxxx",
"city": "xxxxxxx",
"state": "xxxxxx",
"postal_code": "888888",
"country_code": "IN"
}
},
"billing_info": [
{
"email": "lalith-facilitator#abacies.com",
"business_name": "test facilitator",
"additional_info": "test facilitator's Test Store",
"address": {
"line1": "1 Main St",
"city": "San Jose",
"state": "CA",
"postal_code": "95131",
"country_code": "US"
}
}
],
"shipping_info": {
"first_name": "Lalith",
"last_name": "Kumar",
"address": {
"line1": "1234 Main Street",
"city": "Anytown",
"state": "CA",
"postal_code": "98765",
"country_code": "US"
}
},
"items": [
{
"name": "Zoom System wireless headphones",
"quantity": 2,
"unit_price": {
"currency": "USD",
"value": "120"
},
"tax": {
"name": "Tax",
"percent": 8
}
},
{
"name": "Bluetooth speaker",
"quantity": 1,
"unit_price": {
"currency": "USD",
"value": "145"
},
"tax": {
"name": "Tax",
"percent": 8
}
}
],
"discount": {
"percent": 1
},
"shipping_cost": {
"amount": {
"currency": "USD",
"value": "10"
}
},
"note": "Thank you for your business.",
"terms": "No refunds after 30 days."
}
I tried with changing the access token and merchant email ids. But always I am getting the same error as 401 unauthorized. I am following the documentation of https://developer.paypal.com/docs/api/invoicing/.
ERROR:
{
"name": "AUTHORIZATION_ERROR",
"message": "Authorization error occurred.",
"information_link": "https://developer.paypal.com/docs/api/invoicing/#errors",
"debug_id": "f89a30f937ced"
}
Scope:
"scope": "https://api.paypal.com/v1/payments/.*
https://uri.paypal.com/services/payments/refund
https://uri.paypal.com/services/applications/webhooks
https://uri.paypal.com/services/payments/payment/authcapture
https://uri.paypal.com/payments/payouts https://api.paypal.com/v1/vault/credit-card/.*
https://uri.paypal.com/services/disputes/read-seller
https://uri.paypal.com/services/subscriptions
https://uri.paypal.com/services/disputes/read-buyer
https://api.paypal.com/v1/vault/credit-card openid
https://uri.paypal.com/services/disputes/update-seller
https://uri.paypal.com/services/payments/realtimepayment",

Related

Magento 2 Register and login REST API

I'm creating an app for magento2 with Rest API. I'm facing the below issues.
Im able to create the user through REST API
auth Berear token : bbiotqwwj04prtja2oum5gvfsqt8dnjh
API : Register
URL : http://demo-acm-2.bird.eu/rest/all/V1/customers
method : POST
Request Body
{
"customer": {
"email": "pradeep123#gmail.com",
"firstname": "Abc",
"lastname": "Xyz",
"storeId": 1
},
"password": "Qwe#123123"
}
Response
{
"id": 3,
"group_id": 1,
"created_at": "2020-04-18 12:30:40",
"updated_at": "2020-04-18 12:30:40",
"created_in": "Default Store View",
"email": "pradeep123#gmail.com",
"firstname": "Abc",
"lastname": "Xyz",
"store_id": 1,
"website_id": 1,
"addresses": [],
"disable_auto_group_change": 0,
"extension_attributes": {
"is_subscribed": false
}
}
But when I try to login with the same user to get token it gives me a message like an error. Is there any other way to get a user token/login with rest api in magento2
API: Login
URL: http://demo-acm-2.bird.eu/rest/all/V1/integration/customer/token
method : POST
Request Body
{
"username" : "pradeep123#gmail.com",
"password" : "Qwe#123123"
}
Response
{
"message": "You did not sign in correctly or your account is temporarily disabled."
}
Customer Registration API
API Name: Customer Registration API
Description: Create customer account. Perform necessary business operations like sending email.
API URL: {baseurl}/rest/V1/customers
Method: POST
Body:
{
"customer": {
"email": "testtest123#gmail.com",
"firstname": "Kavin",
"lastname": "Peter",
"addresses": [{
"defaultShipping": true,
"defaultBilling": true,
"firstname": "Kavin",
"lastname": "Peter",
"region": {
"regionCode": "NY",
"region": "New York",
"regionId":43
},
"postcode": "10755",
"street": ["123 Oak Ave"],
"city": "Purchase",
"telephone": "1234567890",
"countryId": "US"
}],
"extension_attributes": {
"is_subscribed": true
},
"custom_attributes": [
{
"attribute_code": "customer_mobile",
"value": "1234567890"
}
]
},
"password": "test123"
}
Response:
{
"id": 55,
"group_id": 1,
"default_billing": "32",
"default_shipping": "32",
"created_at": "2020-08-29 12:10:14",
"updated_at": "2020-08-29 12:10:31",
"created_in": "Default Store View",
"email": "testtest123#gmail.com",
"firstname": "Kavin",
"lastname": "Peter",
"store_id": 1,
"website_id": 1,
"addresses": [
{
"id": 32,
"customer_id": 55,
"region": {
"region_code": "NY",
"region": "New York",
"region_id": 43
},
"region_id": 43,
"country_id": "US",
"street": [
"123 Oak Ave"
],
"telephone": "1234567890",
"postcode": "10755",
"city": "Purchase",
"firstname": "Kavin",
"lastname": "Peter",
"default_shipping": true,
"default_billing": true
}
],
"disable_auto_group_change": 0,
"extension_attributes": {
"is_subscribed": true
},
"custom_attributes": [
{
"attribute_code": "customer_mobile",
"value": "1234567890"
}
]
}
Login API
API Name: Login API
Description: Create access token for the customer credentials.
API URL: {baseurl}/rest/V1/integration/customer/token
Method: POST
Body:
{
"username": "kirti.nariya#magedelight.com",
"password": "test#$123"
}
Response:
"bdw1x6cb3ntbdj6d4pqfzr8xksjezkv2"

PHP Getting a value from a Multi Layered JSON Array

I seem to be having some trouble with JSON arrays. I was wondering if someone could please assist me.
I have the following JSON Array, and I want to get the figure in invoice_number
$json = '
{
"id": "PAY-7MP775806F4135612LLCHE4I",
"intent": "sale",
"state": "approved",
"cart": "9KA22662P3559221J",
"payer":
{
"payment_method": "paypal",
"status": "VERIFIED",
"payer_info":
{
"email": "accounts-buyer#traxprint.com",
"first_name": "test",
"last_name": "buyer",
"payer_id": "ZR6SRXGS252RG",
"shipping_address":
{
"recipient_name": "test buyer",
"line1": "1 Cheeseman Ave Brighton East",
"city": "Melbourne",
"state": "Victoria",
"postal_code": "3001",
"country_code": "AU"
},
"phone": "0364424947",
"country_code": "AU"
}
},
"transactions":
[
{
"amount":
{
"total": "3.00",
"currency": "USD",
"details":
{
"subtotal": "3.00"
}
},
"payee":
{
"merchant_id": "DSTEYCMCDUL3Y"
},
"description": "Payment description",
"invoice_number": "abc1231522823790",
"item_list":
{
"items":
[
{
"name": "Subscribe",
"sku": "sub1",
"price": "3.00",
"currency": "USD",
"tax": "0.00",
"quantity": 1
}
],
"shipping_address":
{
"recipient_name": "test buyer",
"line1": "1 Cheeseman Ave Brighton East",
"city": "Melbourne",
"state": "Victoria",
"postal_code": "3001",
"country_code": "AU"
}
},
"related_resources":
[
{
"sale":
{
"id": "41B66647LJ233225Y",
"state": "completed",
"amount":
{
"total": "3.00",
"currency": "USD",
"details":
{
"subtotal": "3.00"
}
},
"payment_mode": "INSTANT_TRANSFER",
"protection_eligibility": "ELIGIBLE",
"protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE",
"transaction_fee":
{
"value": "0.37",
"currency": "USD"
},
"parent_payment": "PAY-7MP775806F4135612LLCHE4I",
"create_time": "2018-04-04T06:36:59Z",
"update_time": "2018-04-04T06:36:59Z",
"links":
[
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/41B66647LJ233225Y",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/41B66647LJ233225Y/refund",
"rel": "refund",
"method": "POST"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-7MP775806F4135612LLCHE4I",
"rel": "parent_payment",
"method": "GET"
}
]
}
}
]
}
],
"create_time": "2018-04-04T06:36:33Z",
"links":
[
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-7MP775806F4135612LLCHE4I",
"rel": "self",
"method": "GET"
}
]
}';
The code I was trying to use is but it comes up with error Undefined property: stdClass::$invoice_number
$decoded = json_decode($json);
echo $Info = $decoded->transactions[0]->payee->invoice_number;
Could someone please tell me why this does not work?
Thanks
Robert
echo $Info = $decoded->transactions[0]->invoice_number;

How to link PayPal webhook and user?

I think I'm over looking something incredibly simple here.
When I create a new billing agreement through the PayPal PHP SDK I'm returned to my store by the following URL..
/paypal-success?success=true&token=EC-3K353628LH3197445
I have created a WebHook for the Subscription Created event that posts JSON as follows
{
"id": "WH-5UR49681MC703253M-3S161814MB664705V",
"event_version": "1.0",
"create_time": "2018-03-17T17:21:47.120Z",
"resource_type": "Agreement",
"event_type": "BILLING.SUBSCRIPTION.CREATED",
"summary": "A billing subscription was created",
"resource": {
"agreement_details": {
"outstanding_balance": {
"value": "0.00"
},
"num_cycles_remaining": "0",
"num_cycles_completed": "0",
"next_billing_date": "2018-03-17T10:00:00Z",
"last_payment_date": "2018-03-17T17:21:44Z",
"last_payment_amount": {
"value": "1.00"
},
"final_payment_due_date": "1970-01-01T00:00:00Z",
"failed_payment_count": "0"
},
"description": "x",
"links": [{
"href": "api.sandbox.paypal.com/v1/payments/billing-agreements/I-55KA5N9GPPJW",
"rel": "self",
"method": "GET"
}],
"shipping_address": {
"recipient_name": "test buyer",
"line1": "Spitalfields Arts Market, 112 Brick Lane,",
"city": "London",
"state": "London",
"postal_code": "E1 6RL",
"country_code": "GB"
},
"id": "I-55KA5N9GPPJW",
"state": "Active",
"payer": {
"payment_method": "paypal",
"status": "verified",
"payer_info": {
"email": "jamie-buyer#x.com",
"first_name": "test",
"last_name": "buyer",
"payer_id": "Q3TZFN4F7NHJ6",
"shipping_address": {
"recipient_name": "test buyer",
"line1": "Spitalfields Arts Market, 112 Brick Lane,",
"city": "London",
"state": "London",
"postal_code": "E1 6RL",
"country_code": "GB"
}
}
},
"plan": {
"curr_code": "USD",
"links": [],
"payment_definitions": [{
"type": "REGULAR",
"frequency": "Month",
"frequency_interval": "1",
"amount": {
"value": "49.99"
},
"cycles": "0",
"charge_models": [{
"type": "TAX",
"amount": {
"value": "0.00"
}
},
{
"type": "SHIPPING",
"amount": {
"value": "0.00"
}
}]
}],
"merchant_preferences": {
"setup_fee": {
"value": "1.00"
},
"auto_bill_amount": "YES",
"max_fail_attempts": "0"
}
},
"start_date": "2018-03-17T00:00:00Z"
},
"links": [{
"href": "https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-5UR49681MC703253M-3S161814MB664705V",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-5UR49681MC703253M-3S161814MB664705V/resend",
"rel": "resend",
"method": "POST"
}]
}
That token generated when the user is redirected back to my store does not appear to be in the WebHooks JSON.
How do you link your PayPal users once they are redirected back to your store to the incoming WebHooks?
The token returned in your success url is used to activate the agreement after the user accepts the agreement on PayPal. Once you activate the agreement you can retrieve the agreement id. That id is in your web hook under resources/id. "id": "I-55KA5N9GPPJW". There may be a better way to deal with this but I created a subscription table that associates the agreement id to the user id and a transactions table that has the agreement id and the associated webhook information.

PHP Get JSON Values Issue [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
I'm receiving some JSON POST data from a Webhook into my server. I can get the JSON data as follows:
$orderJSON = file_get_contents('php://input');
which returns this JSON:
{
"order": {
"billing_address": {
"address_1": "76 Pacific Drive",
"address_2": "",
"city": "Bondi",
"company": "Hanson Media",
"country": "AU",
"email": "testing#gmail.com",
"first_name": "Hansel",
"last_name": "Gretten",
"phone": "212 554 7855",
"postcode": "2026",
"state": "NSW"
},
"cart_tax": "3.60",
"completed_at": "2016-12-19T11:07:15Z",
"coupon_lines": [],
"created_at": "2016-12-19T11:07:15Z",
"currency": "AUD",
"customer": {
"billing_address": {
"address_1": "76 Pacific Drive",
"address_2": "",
"city": "Bondi",
"company": "Hanson Media",
"country": "AU",
"email": "testing#gmail.com",
"first_name": "Hansel",
"last_name": "Gretten",
"phone": "212 554 7855",
"postcode": "2026",
"state": "NSW"
},
"email": "testing#gmail.com",
"first_name": "Hansel",
"id": 0,
"last_name": "Gretten",
"shipping_address": {
"address_1": "76 Pacific Drive",
"address_2": "",
"city": "Bondi",
"company": "Hanson Media",
"country": "AU",
"first_name": "Hansel",
"last_name": "Gretten",
"postcode": "2026",
"state": "NSW"
}
},
"fee_lines": [],
"id": 3304,
"is_vat_exempt": false,
"line_items": [
{
"id": 113,
"meta": [],
"name": "Happy Ninja",
"price": "18.00",
"product_id": 37,
"quantity": 2,
"sku": "",
"subtotal": "36.00",
"subtotal_tax": "3.60",
"tax_class": null,
"total": "36.00",
"total_tax": "3.60"
},
{
"id": 114,
"meta": [],
"name": "Water Bottles",
"price": "20.50",
"product_id": 3291,
"quantity": 1,
"sku": "PD885536",
"subtotal": "20.50",
"subtotal_tax": "0.00",
"tax_class": "standard",
"total": "20.50",
"total_tax": "0.00"
}
],
"note": "Call to arrange delivery time",
"order_key": "wc_order_5857bf639d951",
"order_number": 3304,
"payment_details": {
"method_id": "eway",
"method_title": "Credit Card",
"paid": false
},
"shipping_address": {
"address_1": "76 Pacific Drive",
"address_2": "",
"city": "Bondi",
"company": "Hanson Media",
"country": "AU",
"first_name": "Hansel",
"last_name": "Gretten",
"postcode": "2026",
"state": "NSW"
},
"shipping_lines": [
{
"id": 115,
"method_id": "local_pickup:1",
"method_title": "Local Pickup",
"total": "0.00"
}
],
"shipping_methods": "Local Pickup",
"shipping_tax": "0.00",
"status": "pending",
"subtotal": "56.50",
"tax_lines": [
{
"code": "AU-GST-1",
"compound": false,
"id": 116,
"rate_id": "1",
"title": "GST",
"total": "3.60"
}
],
"total": "60.10",
"total_discount": "0.00",
"total_line_items_quantity": 3,
"total_shipping": "0.00",
"total_tax": "3.60",
"updated_at": "2016-12-19T11:07:15Z",
"view_order_url": "https://mywebsite.com/my-account/view-order/3304"
}
}
I now need to get individual elements from the JSON, e.g. I would like to get the id value (3304). I've tried:
$orderID = $orderJSON->id;
and
$orderID = $orderJSON[id];
but this just generates errors like 'Trying to get property of non-object'.
Use the json_decode() function to translate your JSON file into a PHP readable data.
<?php
$json = json_decode($orderJSON);
Once you get your JSON file decoded, you can use the function print_r() to print a nice representation of your datas.
<?php
print_r($orderJSON);
This can help you identify how the file is formed and thus determine all the dimensions you need to display the wanted value.
JSON to nice
Now, if you look closely, you'll see that in order to print your ID, you need to pass through the order dimension.
So you may want to use the syntax $json->order->id to target the wanted ID.
<?php
echo $json->order->id;
Display the ID from JSON
PHP : JSON.
PHP : print_r().
You need to use json_decode() first as I can't see this in your question.
id is under order parent so you need to do sonething like $orderJSON->order->id
You must also check decoded $orderJSON is not empty
Example
$orderJSON = json_decode($orderJSON);
if (empty($orderJSON)) {
throw new RuntimeException('Malformed json');
}
$orderID = $orderJSON->order->id;

json stripping in php

Hi I am trying to get the invoice_number object out of a json file. I am using json_decode in php and getting an undefined property error. I am unsure what I am doing wrong.
$json = '{ "id": "PAY-xxxxx", "intent": "sale", "state": "approved", "cart": "xxxx", "payer": { "payment_method": "paypal", "status": "VERIFIED", "payer_info": { "email": "hullsantarun-buyer#gmail.com", "first_name": "test", "last_name": "buyer", "payer_id": "xxxxx", "shipping_address": { "recipient_name": "test buyer", "line1": "1 Main Terrace", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" }, "country_code": "GB", "billing_address": { "line1": "1 Main Terrace", "line2": "", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" } } }, "transactions": [ { "amount": { "total": "10.00", "currency": "GBP", "details": { "subtotal": "10.00", "shipping": "0.00" } }, "payee": { "merchant_id": "xxx", "email": "facilitator#gmail.com" }, "description": "Pay for all your santa fun", "invoice_number": "57c4007c11a60", "item_list": { "items": [ { "name": "wayne : \u00a310 ; ", "price": "10.00", "currency": "GBP", "quantity": 1 } ], "shipping_address": { "recipient_name": "test buyer", "line1": "1 Main Terrace", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" } }, "related_resources": [ { "sale": { "id": "23V89902U7365974Y", "state": "completed", "amount": { "total": "10.00", "currency": "GBP", "details": { "subtotal": "10.00" } }, "payment_mode": "INSTANT_TRANSFER", "protection_eligibility": "ELIGIBLE", "protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE", "transaction_fee": { "value": "0.54", "currency": "GBP" }, "parent_payment": "PAY-2C432486DK345933DK7CAA7I", "create_time": "2016-08-29T09:29:53Z", "update_time": "2016-08-29T09:29:54Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/sale/23V89902U7365974Y", "rel": "self", "method": "GET" }, { "href": "https://api.sandbox.paypal.com/v1/payments/sale/23V89902U7365974Y/refund", "rel": "refund", "method": "POST" }, { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2C432486DK345933DK7CAA7I", "rel": "parent_payment", "method": "GET" } ] } } ] } ], "billing_plan_units": [ { "billing_plan_approved": false } ], "redirect_urls": { "return_url": "https://www.hullsantarun.org/php/pay.php?success=true&paymentId=PAY-2C432486DK345933DK7CAA7I", "cancel_url": "https://www.hullsantarun.org/php/pay.php?success=false" }, "create_time": "2016-08-29T09:29:54Z", "update_time": "2016-08-29T09:29:53Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2C432486DK345933DK7CAA7I", "rel": "self", "method": "GET" } ] }';
$obj = json_decode($json);
print $obj->{'invoice_number'};
invoice_number is a property of an object in the transactions array.
This will work:
$obj->transactions[0]->invoice_number;
You can always do:
print_r($obj);
to see how the object actually looks.
Try this it's works for you.
I have try your given json response and i do some change in your code and it's work try..
$json = '{ "id": "PAY-xxxxx", "intent": "sale", "state": "approved", "cart": "xxxx", "payer": { "payment_method": "paypal", "status": "VERIFIED", "payer_info": { "email": "hullsantarun-buyer#gmail.com", "first_name": "test", "last_name": "buyer", "payer_id": "xxxxx", "shipping_address": { "recipient_name": "test buyer", "line1": "1 Main Terrace", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" }, "country_code": "GB", "billing_address": { "line1": "1 Main Terrace", "line2": "", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" } } }, "transactions": [ { "amount": { "total": "10.00", "currency": "GBP", "details": { "subtotal": "10.00", "shipping": "0.00" } }, "payee": { "merchant_id": "xxx", "email": "facilitator#gmail.com" }, "description": "Pay for all your santa fun", "invoice_number": "57c4007c11a60", "item_list": { "items": [ { "name": "wayne : \u00a310 ; ", "price": "10.00", "currency": "GBP", "quantity": 1 } ], "shipping_address": { "recipient_name": "test buyer", "line1": "1 Main Terrace", "city": "Wolverhampton", "state": "West Midlands", "postal_code": "W12 4LQ", "country_code": "GB" } }, "related_resources": [ { "sale": { "id": "23V89902U7365974Y", "state": "completed", "amount": { "total": "10.00", "currency": "GBP", "details": { "subtotal": "10.00" } }, "payment_mode": "INSTANT_TRANSFER", "protection_eligibility": "ELIGIBLE", "protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE", "transaction_fee": { "value": "0.54", "currency": "GBP" }, "parent_payment": "PAY-2C432486DK345933DK7CAA7I", "create_time": "2016-08-29T09:29:53Z", "update_time": "2016-08-29T09:29:54Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/sale/23V89902U7365974Y", "rel": "self", "method": "GET" }, { "href": "https://api.sandbox.paypal.com/v1/payments/sale/23V89902U7365974Y/refund", "rel": "refund", "method": "POST" }, { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2C432486DK345933DK7CAA7I", "rel": "parent_payment", "method": "GET" } ] } } ] } ], "billing_plan_units": [ { "billing_plan_approved": false } ], "redirect_urls": { "return_url": "https://www.hullsantarun.org/php/pay.php?success=true&paymentId=PAY-2C432486DK345933DK7CAA7I", "cancel_url": "https://www.hullsantarun.org/php/pay.php?success=false" }, "create_time": "2016-08-29T09:29:54Z", "update_time": "2016-08-29T09:29:53Z", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2C432486DK345933DK7CAA7I", "rel": "self", "method": "GET" } ] }';
$obj = json_decode($json);
print $obj->transactions[0]->invoice_number;
OR
echo $obj->transactions[0]->invoice_number;

Categories