I made custom Woocommerce payment gateway and everything is working until I have to return into the website with api response from the external payment link. I get -1 blank page and the network status is 400 error (bad request). Any help is appreciate it. Thanks
$this->notify_url = str_replace( 'https:', 'http:', add_query_arg( 'wc-api', 'pm_wc_ncm', home_url( '/' ) ) );
add_action( 'woocommerce_api_pm_wc_ncm', array( $this, 'check_notify_response' ) );
function check_notify_response(){
if (isset($_POST['txtIndex'])) {
$order_id = $_POST['txtIndex'];
$order = new WC_Order($order_id);
$order_total = $order->get_total();
$amount_paid = $_POST['txtAmount'];
$RespVal = $_POST['RespVal'];
$RespMsg = $_POST['RespMsg'];
$txtIndex = $_POST['txtIndex'];
$txtMerchNum = $_POST['txtMerchNum'];
$txtNumAut = $_POST['txtNumAut'];
$Signature = $_POST['signature'];
$txtCurrency = $_POST['txtCurrency'];
$sha = $this->sha_key;
$SignatureElts = $txtMerchNum . $order_id . $amount_paid . $txtCurrency . $txtNumAut . $RespVal . $RespMsg . $sha;
$ReturnSignature = hash('sha256', $SignatureElts);
if ($ReturnSignature != $Signature) {
echo "Security violation was detected! <br>";
echo "Order ID: ".$txtIndex."<br>";
$message1 = "Signature error";
if (function_exists('wc_add_notice')) {
wc_add_notice($message1, 'error');
}
}else{
if($RespVal=="1")
{
echo "Your Transaction was successful <br>";
echo "Order ID: ".$txtIndex."<br>";
echo "Authorization no.: ".$txtNumAut."<br>";
echo "Amount: ".$amount_paid."<br>";
}
elseif ($RespVal=="0")
{
echo "Unfortunately your transaction was refused <br>";
echo "Order ID: ".$txtIndex."<br>";
echo "Amount: ".$amount_paid."<br>";
}
}
}
}
Related
I have added the following code to send attachments using wp_mail() function. The Runcloud server on which the code is hosted is configured with Amazon SES. On that, the attachment is not sent with the mail, the same code runs perfectly on the local environment with Flywheel.
Code-
add_action(
'phpmailer_init', function( $phpmailer ) use ( $connection_data ) {
$phpmailer->IsSMTP();
$phpmailer->Host = smtp.gmail.com;
$phpmailer->Port = 587;
$phpmailer->Username = //username;
$phpmailer->Password = //password;
$phpmailer->SMTPAuth = true;
$phpmailer->SMTPSecure = 'tls';
$phpmailer->Timeout = 30;
}
);
add_filter(
'wp_mail_content_type', function() {
return 'text/html';
}
);
$file = 'https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg';
$file_name = basename( $file );
$file_name = ! strrpos( $file_name, '.' ) ? $file_name . '.txt' : $file_name;
$content = #file_get_contents( trim( $file ) ) ? wp_remote_get( trim( $file ) ) : $file;
file_put_contents( $file_name, $content['body'] );
$attachments[] = $file_name;
$headers[] = 'From: //Name <//email>';
$send_email = wp_mail( wp_get_current_user()->user_email, 'STMP connection validation', 'STMP connection validation.', $headers, $attachments );
You have 2 problems, One is to configure correctly the wp_mail function.
I recommend you to use this plugin and configure it to your needs: https://es.wordpress.org/plugins/easy-wp-smtp/
Another problem is to send the attachment. This thread may help you:
How to send an email with attachment in wordpress?
I'm new in plugin development. I would like to use this simple function in Wordpress to send mail to users. I saw this code in the documentation everything is simple and straight forward but this code returns false. Why ?
require_once explode( 'wp-content', __FILE__ )[0] . 'wp-load.php';
function send_mail() {
$to = 't.testmail#gmail.com';
$subject = 'The subject';
$body = 'The email body content';
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
$send_message = wp_mail( $to, $subject, $body, $headers );
if ( $send_message ) {
echo 'Email was sent';
} else {
echo 'Email sending was aborted';
}
}
send_mail();
According to the documentation (https://developer.wordpress.org/reference/functions/wp_mail/) the mail could not be sent.
Reasons could be many. Have you checked the debug log of WordPress (https://wordpress.org/support/article/debugging-in-wordpress/)?
I have a few restricted pages. If a user tries to access those, I warn them by email using wp_mail and alert admin with some data such as username, URL, time, etc. For that, I am using the template_redirect hook.
All is working fine but wp_mail sending the email twice. I am not sure what is wrong in my code and why it is sending twice.
Callback Function and Hook
cp_write_log() is a custom log function that write a log to debug.log. Entries also adding twice to the debug.log file.
function module_permission_check() {
if ( is_page() ) {
$template = get_page_template_slug();
$post_type = get_cpt_for_template( $template );
if ( ! is_current_user_granted_for_module( $post_type ) ) {
$user = wp_get_current_user();
//$headers = [ 'From: ' . get_bloginfo( 'name' ) . ' <' . get_bloginfo( 'admin_email' ) . '>' ];
// user message
$message = str_replace(
[ '%%name%%', '%%sitename%%' ],
[
$user->data->user_login,
get_bloginfo( 'name' ),
],
cp_get_setting( 'warning_unauthorized_email_message' )
);
if ( wp_mail( $user->data->user_email, 'Warning: Unauthorized access', $message ) ) {
cp_write_log( 'Email sent to ' . $user->data->user_email );
}
global $wp;
$current_url = home_url( add_query_arg( [], $wp->request ) );
$admin_message = "Hello,\n\nA member has tried to access unauthorized content.\n\nUsername: {$user->data->user_login}\nURL: {$current_url}\nLocal Time: " . current_time( 'l, j F Y \a\t H:i ' ) . "\nUTC Time: " . current_time( 'l, j F Y \a\t H:i ', TRUE ) . "\n\nRegards,\n" . get_bloginfo( 'name' );
if ( wp_mail( get_bloginfo( 'admin_email' ), 'Alert: Unauthorized access', $admin_message ) ) {
cp_write_log( 'Email sent to admin' );
}
wp_redirect(home_url());
exit();
}
}
}
add_action('template_redirect', 'module_permission_check');
Update
I found it is only happening on Safari browser. Very strange.
I am sending all my WordPress emails using Mailgun SMTP.
Other emails from WordPress arrive to the inbox fine, but I have just created a new function to automatically generate an email when a post is published and it always gets caught by the spam filter.
Can you see why?
Maybe something to do with my headers config?
I have tried sending with and without the attachment, it makes no difference.
add_action( 'publish_post', 'notify_on_publish' );
function notify_on_publish( $post_id ) {
global $post;
$from_name = get_bloginfo( 'name' );
$from_email = get_bloginfo( 'admin_email' );
$to_email = get_post_meta( $post_id, 'contact_email', true );
$attachment = get_post_meta( $post_id, 'post_image', true );
$headers = "From: '$from_name' <$from_email> \r\n Content-type: text/html; charset=" . get_option('blog_charset') . "\r\n";
$subject = "This is a test subject";
$message = "<p>This is some test body text</p>";
$attachment_url = $attachment['guid'];
wp_mail( $to_email, $subject, $message, $headers, $attachment_url );
}
some time ago when the customers made a new order, in the mail header the "reply-to" was not shown in the mail that came to me (as an admin), but now if, and I need to avoid showing it, I have tested with
add_filter( 'woocommerce_email_headers', 'add_reply_to_wc_admin_new_order', 10, 3 );
function add_reply_to_wc_admin_new_order( $headers = '', $id = '', $order ) {
if ( $id == 'new_order' ) {
$reply_to_email = $order->billing_email;
$headers .= "Reply-to: <custom#custom.com>\r\n";
}
return $headers;
}
but the client's email keeps appearing in the reply-to, any idea? How to solve this?
here is your solution to keep only the header type without Reply to:
add_filter('woocommerce_email_headers', 'add_reply_to_wc_admin_new_order', 10, 3);
function add_reply_to_wc_admin_new_order($header = '', $id = '', $order)
{
$wc_email = new WC_Email(); //instantiate wc meail
if ($id == 'new_order') {
$reply_to_email = $order->billing_email;
$header = 'Content-Type: ' . $wc_email->get_content_type() . "\r\n";
}
return $header;
}