Paymaya integration in PHP - php

im trying to create a customer in paymaya using curl in php.
im following this documentation http://developers.paymaya.com.payment-vault.s3-website-ap-southeast-1.amazonaws.com/#card-vault-customers-post
but its not returning the right response(it returns nothing)
<?php
require_once(DIR_VENDOR . 'PayMaya-PHP-SDK-master/sample/autoload.php');
Class Paymaya {
public function paymayaInit(){
PayMayaSDK::getInstance()->initCheckout("pk-nRO7clSfJrojuRmShqRbihKPLdGeCnb9wiIWF8meJE9", "sk-jZK0i8yZ30ph8xQSWlNsF9AMWfGOd3BaxJjQ2CDCCZb", "SANDBOX");
}
public function createCustomer(){
$this->paymayaInit();
// $ch = curl_init("https://pg-sandbox.paymaya.com/payments/v1/customers");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://pg-sandbox.paymaya.com/payments/v1/customers");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Basic c2stOWxSbUZUVjhCSWR4b1hXbTVsaURBbEtGMHlMNGdaendtRFFBbW52eFdPRjo="));
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
$body = array(
"firstName" => "Ysa",
"middleName" => "Cruz",
"lastName" => "Santos",
"birthday" => "1987-10-10",
"sex" => "F",
"contact" => array(
"phone" => "+63(2)1234567890",
"email" => "ysadcsantos#gmail.com"
),
"billingAddress" => array(
"line1" => "9F Robinsons Cybergate 3",
"line2" => "Pioneer Street",
"city" => "Mandaluyong City",
"state" => "Metro Manila",
"zipCode" => "12345",
"countryCode" => "PH"
),
"metadata" => array()
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
$response = curl_exec($ch);
curl_close($ch);
// die(print_r($response));
return json_decode($response);
}
}
here is my class.
does anyone already tried integrating paymaya in php?
also i have to comment the namespace in PayMayaSDK.php to be able to use PayMayaSDK class

Thanks for your credentials posted in question I made few requests and found what was wrong.
First of all I used curl_error($ch) to find out what error was.
It's The requested URL returned error: 400 Bad Request.
Problem is that you set Content-Type to be json, but sending URL encoded query.
Change http_build_query to json_encode
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://pg-sandbox.paymaya.com/payments/v1/customers");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Basic c2stOWxSbUZUVjhCSWR4b1hXbTVsaURBbEtGMHlMNGdaendtRFFBbW52eFdPRjo="));
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
$body = array(
"firstName" => "Ysa",
"middleName" => "Cruz",
"lastName" => "Santos",
"birthday" => "1987-10-10",
"sex" => "F",
"contact" => array(
"phone" => "+63(2)1234567890",
"email" => "ysadcsantos#gmail.com"
),
"billingAddress" => array(
"line1" => "9F Robinsons Cybergate 3",
"line2" => "Pioneer Street",
"city" => "Mandaluyong City",
"state" => "Metro Manila",
"zipCode" => "12345",
"countryCode" => "PH"
),
"metadata" => array()
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
$response = curl_exec($ch);
echo __FILE__."#".__LINE__."<pre>";
var_dump($response, curl_error($ch));
echo "</pre>";
curl_close($ch);

Related

Freshdesk API: Attach dataurl as file using PHP and CURL

I'm trying to create a ticket in Freshdesk with a screenshot as an attachment. Screenshot is captured in a canvas and converted to dataurl. And I'm using the following code to pass it to the Freshdesk API (V2)
$data = [
"description" => $messageBody,
"subject" => 'Bug Report from Client',
"email" => $replyTo,
"priority" => 1,
"status" => 2,
"attachments" => [
[
"type" => "file",
"name" => "Screenshot",
"content-type" => "image/png",
"resource" => $dataUrl,
]
]
];
$url = "https://domain.freshdesk.com/api/v2/tickets";
$apiKey = "key";
$headers = array(
'Content-Type:application/json',
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $apiKey);
$resultStr = curl_exec($ch);
It results in the following error.
{
field: "attachments",
message: "It should contain elements of type valid file format only",
code: "datatype_mismatch"
}
Without the attachments field, the above code works fine. How do I fix this?

How to Create Shopify Order Via Api using Php?

I am trying to Create a Shopify Order using Api this is my code :
$arrOrder= array(
"email"=> "foo#example.com",
"fulfillment_status"=> "fulfilled",
"send_receipt"=> true,
"send_fulfillment_receipt"=> true,
"line_items"=> array(
array(
"product_id"=>875744960642,
"variant_id"=> 3558448932592,
"quantity"=> 1
)
),
"customer"=> array(
"id"=> 458297751235
),
"financial_status"=> "pending"
); echo json_encode($arrOrder);
echo "<br />";
$url = "https://AkiKey:Password#Store.myshopify.com/admin/api/2021-01/orders.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($arrOrder));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
echo "<pre>";
print_r($response);
and the response is :
{"errors":{"order":"Required parameter missing or invalid"}}
I think there is some data mismatch that is sent using API Call, according to the documentation this the format to create an order.
So I think your request is something like this one demo code
$arrOrder= [
"order" =>[
"email" => "foo#example.com",
"fulfillment_status" => "fulfilled",
"send_receipt" => true,
"send_fulfillment_receipt" => true,
"line_items" => [
[
"product_id" => 875744960642,
"variant_id" => 3558448932592,
"quantity" => 1
]
],
"customer" => [
"id"=> 458297751235
],
"financial_status"=> "pending"
]
];

How to format POST request using PHP curl methods?

I'm trying to send a post request with this payload:
$request_content = [
"data" => [
[
"sku" => "0987",
"price" => $price,
"category" => "moveis",
"brand" => "bartira",
"zip_code" => "07400000",
"affiliate" => "google-shopping"
]
]
];
Since it's a post i set the CURLOPT_POST to true;
$encoded_request = json_encode($request_content);
$ch = curl_init("https://my-service/endpoint/");
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Token my-token"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_request);
The $encoded_request content shown in print_r is:
{
"data": [
{
"sku": "0987",
"price": "5.99",
"category": "moveis",
"brand": "bartira",
"zip_code": "07400000",
"affiliate": "google-shopping"
}
]
}
If i use this content on the Postman i get the right response from the service that i'm requesting, but on my code i got the error;
{"data":["This field is required."]}
Which configuration i'm missing on curl_ to format the payload correctly?
You can try to set CURLOPT_HTTPHEADER and change your variable $request_content, something like this:
//set your data
$request_content = [
"data" => [
"sku" => "0987",
"price" => $price,
"category" => "moveis",
"brand" => "bartira",
"zip_code" => "07400000",
"affiliate" => "google-shopping"
]
];
$encoded_request = json_encode($request_content);
$ch = curl_init("https://my-service/endpoint/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_request);
// Set HTTP Header for POST request
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Token my-token',
'Content-Type: application/json',
'Content-Length: ' . strlen($encoded_request)]
);

You must log in before using this part of Bugzilla, code:410

I am able to get the GET request working but having issues related to authentication in POST and PUT request. I am getting the error "You must log in before using this part of Bugzilla". I have provided the correct username and password. I have tried CURLAUTH_ANY as well as CURLAUTH_BASIC. I have tried both PUT and POST request. Any help is appreciated.
$url ="http://localhost:8080/bugzilla/rest/bug/2";
$apikey = "IZC4rs2gstCal0jEZosFjDBRV9AQv2gF0udh4hgq";
$data = array(
"product" => "TestProduct",
"component" => "TestComponent",
"version" => "unspecified",
"summary" => "This is a test bug - please disregard",
"alias" => "SomeAlias",
"op_sys" => "All",
"priority" => "P1",
"rep_platform" => "All"
);
$str_data = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$str_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Content-Type: application/json", "Accept: application/json"));
$username = 'ashish.sureka#in.abb.com';
$password = 'abbincrc';
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $result
Following code solved my problem. I have written a blog on it which might be useful to others encountering the same problem.
<?php
$url = 'http://localhost:8080//bugzilla/xmlrpc.cgi';
$ch = curl_init();
$header = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array( 'Content-Type: text/xml', 'charset=utf-8' )
);
curl_setopt_array($ch, $header);
$bugreport = array(
'login' => 'ashish.sureka#in.abb.com',
'password' => 'abbincrc',
'product' => "TestProduct",
'component' => "TestComponent",
'summary' => "Bug Title : A One Line Summary",
'assigned_to' => "ashish.sureka#in.abb.com",
'version' => "unspecified",
'description' => "Bug Description : A Detailed Problem Description",
'op_sys' => "All",
'platform' => "All",
'priority' => "Normal",
'severity' => "Trivial"
);
$request = xmlrpc_encode_request("Bug.create", $bugreport);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_exec($ch)
?>

MALFORMED_REQUEST trying to communicate with paypal

I'm having trouble just sending my first API call to PayPal.
I'm hoping its just simple syntax that I got wrong.
The error I'm getting:
"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($ch);
if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
$sAccessToken = $json->access_token;
$sTokenType = $json->token_type;
$sAppID = $json->app_id;
print_r($json);
}
curl_close($ch);
$ch2 = curl_init();
//curl -v https://api.sandbox.paypal.com/v1/payments/payment
curl_setopt($ch2, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
//-H "Content-Type:application/json" \
//-H "Authorization: Bearer <Access-Token>" \
curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
"Content-Type:application/json",
"Authorization: Bearer ".$sAccessToken,
));
$payer = array(
'payment_method' => 'credit_card',
'funding_instruments' => array(
'credit_card' => array(
'number' => "4417119669820331",
"type" => "visa",
"expire_month" => "11",
"expire_year" => "2018",
"cvv2" => "874",
"first_name" => "Betsy",
"last_name" => "Buyer",
"billing_address" => array(
"line1" => "111 First Street",
"city" => "Saratoga",
"state" => "CA",
"postal_code" => "95070",
"country_code" => "US"
),
),
),
);
$params = array(
'intent' => 'sale',
'payer' => $payer,
'transactions' => array(array(
"amount" =>"7.47",
"currency" =>"USD",
"details" => array(
"subtotal"=>"7.41",
"tax"=>"0.03",
"shipping"=>"0.03"
),
"description" => "This is the payment transaction description."
)
));
curl_setopt($ch2, CURLOPT_POST, true );
curl_setopt($ch2, CURLOPT_POSTFIELDS, json_encode( $params ) );
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true );
$response2 = curl_exec( $ch2 );
$errrors2 = curl_error($ch2);
print_r($response2." / ");
print_r($errrors2);
curl_close($ch2);

Categories