I am trying to integrate a php with 2checkout api to make a new subscription for a user. In 2checkout documentation they provide a JSON body to be sent on the request:-
$payload = '{
"AdditionalInfo": null,
"CustomPriceBillingCyclesLeft": 2,
"DeliveryInfo": {
"Codes": [
{
"Code": "___TEST___CODE____",
"Description": null,
"ExtraInfo": null,
"File": null
}
],
"Description": null
},
"EndUser": {
"Address1": "Test Address",
"Address2": "",
"City": "LA",
"Company": "",
"CountryCode": "us",
"Email": "customer@2Checkout.com",
"Fax": "",
"FirstName": "Customer",
"Language": "en",
"LastName": "2Checkout",
"Phone": "",
"State": "CA",
"Zip": "12345"
},
"ExpirationDate": "2020-06-27",
"ExternalCustomerReference": null,
"ExternalSubscriptionReference": "ThisIsYourUniqueIdentifier123",
"NextRenewalPrice": 19.99,
"NextRenewalPriceCurrency": "usd",
"PartnerCode": "",
"Payment": {
"CCID": "123",
"CardNumber": "4111111111111111",
"CardType": "VISA",
"ExpirationMonth": "12",
"ExpirationYear": "2018",
"HolderName": "John Doe"
},
"Product": {
"ProductCode": "nfbox-1m",
"ProductId": "29810789",
"ProductName": "1 Month NFBOX - Platinum Membership - عضوية البلاتينوم ",
"ProductQuantity": 1,
"ProductVersion": ""
},
"StartDate": "2020-05-27",
"SubscriptionValue": 19.99,
"SubscriptionValueCurrency": "usd",
"Test": 1
}';
When i test by return :
return json_encode($payload)
It gave the following output:-
"{\r\n \"AdditionalInfo\": null,\r\n \"CustomPriceBillingCyclesLeft\": 2,\r\n \"DeliveryInfo\": {\r\n \"Codes\": [\r\n {\r\n \"Code\": \"___TEST___CODE____\",\r\n \"Description\": null,\r\n \"ExtraInfo\": null,\r\n \"File\": null\r\n }\r\n ],\r\n \"Description\": null\r\n },\r\n \"EndUser\": {\r\n \"Address1\": \"Test Address\",\r\n \"Address2\": \"\",\r\n \"City\": \"LA\",\r\n \"Company\": \"\",\r\n \"CountryCode\": \"us\",\r\n \"Email\": \"customer@2Checkout.com\",\r\n \"Fax\": \"\",\r\n \"FirstName\": \"Customer\",\r\n \"Language\": \"en\",\r\n \"LastName\": \"2Checkout\",\r\n \"Phone\": \"\",\r\n \"State\": \"CA\",\r\n \"Zip\": \"12345\"\r\n },\r\n \"ExpirationDate\": \"2020-06-27\",\r\n \"ExternalCustomerReference\": null,\r\n \"ExternalSubscriptionReference\": \"ThisIsYourUniqueIdentifier123\",\r\n \"NextRenewalPrice\": 19.99,\r\n \"NextRenewalPriceCurrency\": \"usd\",\r\n \"PartnerCode\": \"\",\r\n \"Payment\": {\r\n \"CCID\": \"123\",\r\n \"CardNumber\": \"4111111111111111\",\r\n \"CardType\": \"VISA\",\r\n \"ExpirationMonth\": \"12\",\r\n \"ExpirationYear\": \"2018\",\r\n \"HolderName\": \"John Doe\"\r\n },\r\n \"Product\": {\r\n \r\n \"ProductCode\": \"nfbox-1m\",\r\n \"ProductId\": \"29810789\",\r\n \"ProductName\": \"1 Month NFBOX - Platinum Membership - \u0639\u0636\u0648\u064a\u0629 \u0627\u0644\u0628\u0644\u0627\u062a\u064a\u0646\u0648\u0645 \",\r\n \"ProductQuantity\": 1,\r\n \"ProductVersion\": \"\"\r\n },\r\n \"StartDate\": \"2020-05-27\",\r\n \"SubscriptionValue\": 19.99,\r\n \"SubscriptionValueCurrency\": \"usd\",\r\n \"Test\": 1\r\n}"
And i am using a Guzzlehttp to post a request to 2checkout:-
$client = new GuzzleHttp\Client(['base_uri' => 'https://api.avangate.com/rest/6.0/']);
$r = $client->request('POST', 'orders/', [
'headers' => [
'X-Avangate-Authentication' => $header,
'Content-Type' => 'application/json',
'Accept' => 'application/json'
], 'json' => json_encode($payload)
]);
$response = $r->getBody();
return json_decode($response);
How can i include the json body on the above request ?
The startpoint of the payload data should be an array in order to encode it to json. Otherwise you might encounter problems since you are then trying to encode data already in json format.
Step-1) Create a variable $payload where you store the data you wish to send with your request.
Step-2) Add body into your request (below headers), where $payload is your data, and json-encode the $payload.
'body' => json_encode($payload)
To simplify the test you can also just add the pure text string.
'body' => json_encode('mydata goes here...')