paygate payweb3 integration in php website - php

I am trying to integrate paygate payment gateway integration into my PHP API. I have gone through their documentation but it is not clear for me. There were three endpoints
initiate
return
query
I don't which endpoint to use. Now I am using initiate endpoint, but when testing in postman I am getting an error
ERROR=DATA_CHK error
Can anyone guide me to sort this out...
public function subscription(Request $request) {
$validator = Validator::make(
$request->all(),
array(
'subscription' => 'required|integer',
'email' => 'required|exists:users,email',
'amount' => 'required|integer',
'payment_mode' => 'required|integer',
),
array(
'exists' => 'The :attribute doesn\'t exists',
)
);
if ($validator->fails()) {
$error_messages = implode(',', $validator->messages()->all());
$response_array = array('success' => false, 'error' => Helper::get_error_message(101), 'error_code' => 101, 'error_messages'=>$error_messages);
} else {
$encryptionKey = 'AQ2Uted8BidhkIEv6oYgrqwS';
$data = array(
'PAYGATE_ID' => 1039433100014,
'REFERENCE' => 'pgtest_123456789',
'AMOUNT' => 3299,
'CURRENCY' => 'ZAR',
'RETURN_URL' => 'http://localhost/avvata/public/home',
'TRANSACTION_DATE' => date('Y-m-d H:i:s'),
'LOCALE' => 'en-za',
'COUNTRY' => 'ZAF',
'EMAIL' => $request->email,
);
$checksum = md5(implode('', $data) . $encryptionKey);
$data['CHECKSUM'] = $checksum;
$fieldsString = http_build_query($data);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, 'https://secure.paygate.co.za/payweb3/initiate.trans');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldsString);
//execute post
$result = curl_exec($ch);
echo $result;
exit();
//close connection
curl_close($ch);
}`
Thanks in advance :-)

Related

Using PHP Telegram Bot InputMediaPhoto Method

I was trying to send some images into one message with my Telegram bot. I used InputMediaPhoto method to send, but unfortunately doesn't work.
Here is my code:
$url = "https://api.telegram.org/bot" . "TOKEN" . "/InputMediaPhoto";
$postContent = [
'chat_id' => $GLOBALS['chatId'],
'media' => [
['type'=>'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg'], //Just for test
['type' => 'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg'],
['type' => 'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg']
]
];
post($url, $postContent);
function post($url, $postContent)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postContent);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
There are two problems.
The medthod you need to use is called sendMediaGroup. Also the media you are sending must be in a JSON-serialized array.
Changing your code to this should work:
$url = "https://api.telegram.org/bot" . "TOKEN" . "/sendMediaGroup";
$postContent = [
'chat_id' => $GLOBALS['chatId'],
'media' => json_encode([
['type'=>'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg'],
['type' => 'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg'],
['type' => 'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg']
])
];

how to send access token using CURL PHP

I am new in CURL I need to authenticate user is logged in or not using access token. But how to send x-access-token with post request into header.
I am using. curl library
require __DIR__ . '/vendor/autoload.php';
use \Curl\Curl;
$curl = new Curl();
$curl->post('http://localhost:3011/user/reset-password',array('x-access-token'=>$user['token']), array(
'newPassword' => $_POST['newPassword'],
'confirmPassword' => $_POST['confirmPassword']
));
if ($curl->error) {
$response = array(
'status' => $curl->errorCode,
'message' => $curl->errorMessage,
'response' => $curl->response
);
echo json_encode($response);
} else {
$response = array(
'status' => $curl->httpStatusCode,
'message' => 'Successfylly Login',
'response' => $curl->response
);
echo json_encode($response);
}
You can do this way also.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.abctest.com/abc.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//********************Check these lines for headers*******************
$headers = [
'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
'x-access-token':'sdsdgdsggrtyrtghf'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//************************ END Headers *********************************
$server_output = curl_exec ($ch);
curl_close ($ch);
print $server_output ;
You can use setHeader method in Curl class.
RTFM plz.
require __DIR__ . '/vendor/autoload.php';
use \Curl\Curl;
$curl = new Curl();
// check the following line
$curl->setHeader('x-access-token', 'YOUR-TOKEN-HERE');
$curl->post('http://localhost:3011/user/reset-password',array('x-access-token'=>$user['token']), array(
'newPassword' => $_POST['newPassword'],
'confirmPassword' => $_POST['confirmPassword']
));
if ($curl->error) {
$response = array(
'status' => $curl->errorCode,
'message' => $curl->errorMessage,
'response' => $curl->response
);
echo json_encode($response);
} else {
$response = array(
'status' => $curl->httpStatusCode,
'message' => 'Successfylly Login',
'response' => $curl->response
);
echo json_encode($response);
}

Paypal express checkout integration with PayPal REST API

I'm trying to integrate Paypal express checkout integration with PayPal REST API.
JS file looks like this:
paypal.Button.render({
env: 'sandbox', // sandbox | production
commit: true,
payment: function() {
var CREATE_URL = 'createpayment';
return paypal.request.get(CREATE_URL)
.then(function(res) {
return res.paymentID;
});
},
onAuthorize: function(data, actions) {
var EXECUTE_URL = 'executepayment';
var data = {
paymentID: data.paymentID,
payerID: data.payerID
};
return paypal.request.post(EXECUTE_URL, data)
.then(function (res) {
window.alert('Payment Complete!');
});
}
}, '#paypal-button-container');
Now I can't figure out what should be in CREATE_URL and EXECUTE_URL url's php files. I have read a lot of their docs. I understand that in php I should make first request to get token, then I should make create-payment request and after execute-payment curl reuqest. But I have no-luck.
I also don't understand how should I pass payment value to CREATE_URL php file.
Several days ago I found here some good answer of my question with all examples, but it's about two days I can't find it.
Update: ALso I need to pass input amount to php file from payment request. And I can't do that using return paypal.request.get(CREATE_URL, data)
request.
Can anybody help me please.
Thank you.
Share some examples for server PHP code.
PHP Code in CREATE_URL:
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_URL, 'https://api-3t.sandbox.paypal.com/nvp');
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array(
'USER' => 'xxxxx',
'PWD' => 'xxxxx',
'SIGNATURE' => 'xxxxx',
'METHOD' => 'SetExpressCheckout',
'VERSION' => '108',
'LOCALECODE' => 'en_US',
'PAYMENTREQUEST_0_AMT' => 100,
'PAYMENTREQUEST_0_CURRENCYCODE' => 'USD',
'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
'PAYMENTREQUEST_0_ITEMAMT' => 100,
'L_PAYMENTREQUEST_0_NAME0' => 'test name',
'L_PAYMENTREQUEST_0_DESC0' => 'test desc',
'L_PAYMENTREQUEST_0_QTY0' => 1,
'L_PAYMENTREQUEST_0_AMT0' => 100,
'CANCELURL' => 'http://localhost/cancel.html',
'RETURNURL' => 'http://localhost/success.html'
)));
$response = curl_exec($curl);
curl_close($curl);
$nvp = array();
if (preg_match_all('/(?<name>[^\=]+)\=(?<value>[^&]+)&?/', $response, $matches)) {
foreach ($matches['name'] as $offset => $name) {
$nvp[$name] = urldecode($matches['value'][$offset]);
}
}
if (isset($nvp['ACK']) && $nvp['ACK'] == 'Success') {
echo $nvp['TOKEN'];
} else {
//failed
}
?>
PHP Code in EXECUTE_URL:
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_URL, 'https://api-3t.sandbox.paypal.com/nvp');
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array(
'USER' => 'xxxxx',
'PWD' => 'xxxxx',
'SIGNATURE' => 'xxxxx',
'METHOD' => 'DoExpressCheckoutPayment',
'VERSION' => '108',
'LOCALECODE' => 'en_US',
'TOKEN' => $_POST['TOKEN'],
'PayerID' => $_POST['PAYERID'],
'PAYMENTREQUEST_0_AMT' => 100,
'PAYMENTREQUEST_0_CURRENCYCODE' => 'USD',
'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
'PAYMENTREQUEST_0_ITEMAMT' => 100,
'L_PAYMENTREQUEST_0_NAME0' => 'test name',
'L_PAYMENTREQUEST_0_DESC0' => 'test desc',
'L_PAYMENTREQUEST_0_QTY0' => 1,
'L_PAYMENTREQUEST_0_AMT0' => 100,
)));
$response = curl_exec($curl);
curl_close($curl);
$nvp = array();
if (preg_match_all('/(?<name>[^\=]+)\=(?<value>[^&]+)&?/', $response, $matches)) {
foreach ($matches['name'] as $offset => $name) {
$nvp[$name] = urldecode($matches['value'][$offset]);
}
}
print_r($nvp);

Mailchimp Does not work on production [laravel on azure]

I deployed a small laravel app that subscribes a user to a mailchimp list.
it's very basic yet it does not work on production
NOTE: EVERYTHING is fine in Localhost env and a CONTACT FORM WORKS FINE(uses SMTP)
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=AUSERNAME
MAIL_PASSWORD=APASSWORD
MAIL_ENCRYPTION=TLS
MAILCHIMP_APIKEY=APIKEYHERE
MAILCHIMP_LIST_ID=LISTIDHERE
Controller
Newsletter::subscribe($request->email, [
'firstName' => 'test',
'lastName' => 'tessst',
'listName' => 'whishlist' ], 'subscribers');
return response()->json([
'status' => 'success',
'msg' => 'Subscribed successfully']);
laravel-newsletter Config file
<?php
return [
'apiKey' => env('MAILCHIMP_APIKEY'),
'defaultListName' => 'subscribers',
'lists' => [
'subscribers' => [
'id' => '5920168294',
],
'whishlist' => [
'id' => '8e553f3d39',
],
],
];
My i guess is that this has something to do with HTTPS (i fixed the issue by adding a file cacert.pem and referencing it in php.ini )
if this is the issue how can i fix this on azure?
And sorry there is no error output since it returns success to the ajax call.(if how can i get the response from mailchimp to check the error?)
Thanks in advance.
Well i dont know what is the issue here.
but i managed to make this work when i got rid of the package i'm using which is spatie/laravel-newsletter and used CURL instead and API V3.
$email = $request->email;
$listid = env('MAILCHIMP_LIST_ID');
$apikey = env('MAILCHIMP_APIKEY');
$server = substr($apikey, strpos($apikey, '-') + 1);
$auth = base64_encode('user:' . $apikey);
$data = array(
'apikey' => $apikey,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => 'test1',
'LNAME' => 'test2',
),
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://'. $server.'api.mailchimp.com/3.0/lists/'. $listid .'/members/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic ' . $auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
curl_close($ch);
return $result;

Telegram Bot custom keyboard in PHP

I'm trying to make a Telegram Bot in PHP with a custom keyboard. The message is delivered, but the custom keyboard won't work. $keyb = array('keyboard' => array(array("A", "B"))); also no succes.
The sendMessage method referrers to ReplyKeyboardMarkup for the object. Making an array for ReplyKeyboardMarkup doesn't work. Also tried to json_encode($keyb) but that's also not the solution.
I searched in GitHub for examples but I haven't found one where the custom keyboard is used. Telegram runs on iPhone and desktop, both up-to-date.
Sample code:
$url = "https://api.telegram.org/bot<token>/sendMessage";
$keyb = array('ReplyKeyboardMarkup' => array('keyboard' => array(array("A", "B"))));
$content = array('chat_id' => <chat_id>, 'reply_markup' => $keyb, 'text' => "Test");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //fix http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
var_dump($server_output);
The docs seem to indicate you need to provide the reply_markup parameter as a JSON serialised object... kinda stupid for a form POST endpoint:
$replyMarkup = array(
'keyboard' => array(
array("A", "B")
)
);
$encodedMarkup = json_encode($replyMarkup);
$content = array(
'chat_id' => <chat_id>,
'reply_markup' => $encodedMarkup,
'text' => "Test"
);
Does this one work?
$keyboard = array(array("[Destaques]","[Campinas e RMC]","[esportes]"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
$url = $GLOBALS[website]."/sendmessage?chat_id=".$chatId."&text=oi&reply_markup=".$reply;
file_get_contents($url);
This code works fine!
$keyboard = [
'keyboard' => [
[
['text' => 'hourly', 'callback_data' => 'Every hour'],
['text' => 'daily', 'callback_data' => 'Every day'],
['text' => 'weekly', 'callback_data' => 'Every week'],
['text' => 'monthly', 'callback_data' => 'Every month'],
['text' => 'yearly', 'callback_data' => 'Every year'],
] ]
];
this works for me

Categories