How to get success or failure response from paypal? - angularjs

Iam trying to do the paypal payment.after completion of the payment how can we get the details of the transaction and how to get the success or failure response from paypal payment in angularjs?

I'm using this PHP Code to make it. So, after you complete a transaction in Paypal, they will return to you a response, (even if the transaction was a failure). Just check the the parameter "ACK" at the array.
This response is an array with the informations about your transaction.
If you want to confer the information of your transaction. Just use the method "GetTransactionDetails", and the paypal server will return you an array with all informations(you must the transaction ID to make your Query, not your token). Just adapt this code to angularJS
<?php
function verify_Transaction($idTrans,$show){
$nvp = array(
'METHOD' => 'GetTransactionDetails',
'VERSION' => '124.0',
'PWD' => 'YOUR PASS',
'USER' => 'YOUR USER',
'SIGNATURE' => 'YOUR SIGNATURE' ,
'TRANSACTIONID' => yourTransactionID
);
$curl = curl_init();
curl_setopt( $curl , CURLOPT_URL , 'https://api-3t.sandbox.paypal.com/nvp' );
curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER , false );
curl_setopt( $curl , CURLOPT_RETURNTRANSFER , 1 );
curl_setopt( $curl , CURLOPT_POST , 1 );
curl_setopt( $curl , CURLOPT_POSTFIELDS , http_build_query( $nvp ) );
$response = urldecode( curl_exec( $curl ) );
$responseNvp = array();
if ( preg_match_all( '/(?<name>[^\=]+)\=(?<value>[^&]+)&?/' , $response , $matches ) ) {
foreach ( $matches[ 'name' ] as $offset => $name ) {
$responseNvp[ $name ] = $matches[ 'value' ][ $offset ];
}
}
if($show){
echo '<pre>';print_r($responseNvp);echo'</pre>';
}
if($responseNvp['ACK']=='Success'){
//echo "true";
return true;
}else{
//echo "false";
return false;
}
}
?>

Related

Is a meta_key record in my Wordpress database enough as a proof of consent for marketing purposes?

Good afternoon!
I added an optional checkbox in my Wordpress user registration form, that the user is supposed to tick if he agrees to receive commercial offers from the website. This data is then stored in my database. I am now wondering... As I can modify the database meta_values is this enough as a proof of consent for legal purposes?
This is the code I added to functions.php:
// Remove "(optional)" label for this checkbox
add_filter( 'woocommerce_form_field' , 'remove_optional_custom_field_label', 10, 4 );
function remove_optional_custom_field_label( $field, $key, $args, $value ) {
if( 'commercial_offers' === $key && is_wc_endpoint_url( 'edit-account' ) ) {
$optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$field = str_replace( $optional, '', $field );
}
return $field;
}
// Display a custom checkbox in registration form and My Account > Account details
add_action( 'woocommerce_register_form', 'add_account_newsletter_checkbox_field' );
add_action( 'woocommerce_edit_account_form', 'add_account_newsletter_checkbox_field' );
function add_account_newsletter_checkbox_field() {
woocommerce_form_field( 'commercial_offers', array(
'type' => 'checkbox',
'class' => array('form-row-wide'),
'label' => __( 'Acconsento al trattamento dei miei dati personali per finalità di marketing dirette, come previsto al par. 3.4. dell’informativa privacy', 'woocommerce' ),
'clear' => true,
), get_user_meta(get_current_user_id(), 'commercial_offers', true ) );
}
// Save registration checkbox field value
add_action( 'woocommerce_created_customer', 'save_account_registration_field' );
function save_account_registration_field( $customer_id ) {
$value = isset( $_POST['commercial_offers'] ) ? '1' : '0';
update_user_meta( $customer_id, 'commercial_offers', $value );
}
// Save checkbox field value for My Account > Account details
add_action( 'woocommerce_save_account_details', 'save_account_details_newsletter_checkbox_field', 10, 1 );
function save_account_details_newsletter_checkbox_field( $user_id ) {
$value = isset( $_POST['commercial_offers'] ) ? '1' : '0';
update_user_meta( $user_id, 'commercial_offers', $value );
}

drupal_mail() returns true but unable to send mail

I have written a drupal module with custom form api which will send email to my inbox on each submit. I have written a condition under drupal_mail which returns true but shows an error message "Unable to send e-mail. Contact the site administrator if the problem persists."
Below my code:
function my_module_name_mail($key, &$message, $params)
{
$headers = array(
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=UTF-8;',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal'
);
foreach ($headers as $key => $value) {
$message['headers'][$key] = $value;
}
$message['subject'] = $params['subject'];
$message['body'] = $params['body'];
}
function my_module_name_form_submit($form, &$form_state)
{
$from = $form_state['values']['email'];
$body= 'Name: '.$name.'<br />Email: '.$email;
$to = "my_mail_id#example.com";
$params = array(
'body' => $body,
'subject' => 'Website Information Request',
);
drupal_mail('my_module_name', 'some_mail_key', $to, language_default(), $params, $from, TRUE);
}
Check your Drupal logs for more clues. Enable devel / devel-admin modules. Once you do, drupal_mail will pipe emails to your temp directory, which will help you narrow down if the problem is with your email server or the config.

how to fetch data from database in drupal7 in table form

how to fetch data from database in drupal7:
fields i have name:
subject:
email:
message:
give me the code for drupal 7 i want it in table form.
my insert code is this:
function form_example_form_submit($form, &$form_state) {
echo $name = $form_state['values']['textfield'];
echo $email = $form_state['values']['mail'];
echo $subject = $form_state['values']['subject'];
echo $message = $form_state['values']['message'];
echo $ip=ip_address();
echo $cb=$name;
//echo $timestamp = REQUEST_TIME;
echo $time=time();
$nid=db_insert('form') // Table name no longer needs {}
->fields(array(
'name' => $name,
'email' => $email,
'subject' => $subject,
'message' => $message,
'ip' => $ip,
'created_by' => $cb,
//'created_at' => $time,
))
->execute();
//print_r($nid);
drupal_set_message(t('The form has been submitted.'));
}
how can i fetch the data from database in drupal 7 in the form of table ,
give me the code .i m newbie in drupal 7 so its very difficult forme
Here is the snippet to fetch all content from "form" table with table format and pager. Also you can add condition which I have added in comment if required.
<?php
// Set header
$header = array(
array('data' => t('Name'), 'field' => 'name'),
array('data' => t('Email'), 'field' => 'email'),
array('data' => t('Subject'), 'field' => 'subject'),
array('data' => t('Message'), 'field' => 'message'),
);
//query to fetch all content
$query = db_select('form', 'f');
$query->fields('f');
//$query->condition('f.name', $search_name, '=') //if needed
$table_sort = $query->extend('TableSort') // Table sort extender
->orderByHeader($header); // Order by headers
$pager = $table_sort->extend('PagerDefault')
->limit(20); // Set page limit
$arr_result = $pager->execute();
$rows = array();
foreach($arr_result as $result) {
$rows[] = array(
$result->name,
$result->email,
$result->subject,
$result->message,
);
}
// Set empty output
$output = '';
if (!empty($rows)) {
$output = theme('table', array(
'header' => $header,
'rows' => $rows,
/*'attributes' => array(
'id' => 'sort-table' // add if want to add sorting
) */
));
$output .= theme('pager');
}
else {
$output .= t("No results found.");
}
return $output;
?>
Let me know if any query/confusion occurs for the same.

Update user meta - array

I'm developing comments sections for my user profile and I'm giving an option to user to make comment visible or not.
I've created an array:
$comment = array($comment_name, $comment_text, $time, $visible);
Where $visible is false value at default.
And then I add_user_meta
add_user_meta($user->ID, 'recommend_comment', $comment);
This is working perfect for me, I've got an array of comments displayed.
Now I want to update array with $visible = true if user clicks on button but not sure how to access specific array row with update_user_meta.
I tried with:
update_user_meta($user->ID, 'recommend_comment', $prikazi, [2]);
But that's not working. Any idea how to make this?
You can use update_user_meta() for adding and/or updating, see reference: update_user_meta
To update visible you can do:
$comment = get_user_meta( $user->ID, 'recommend_comment', TRUE );
if( !empty( $comment ) ) {
$comment[3] = FALSE;
update_user_meta( $user->ID, 'recommend_comment', $comment );
}
To improve it a bit you could instead use keys in the array, for example:
$comment = array( 'name' => $comment_name, 'text' => $comment_text, 'time' => $time, 'visible' => $visible);
// And then you can access with:
$comment['visible'] = TRUE;
UPDATE: example with a list of comments:
$comments = array(
array( 'name' => 'AAA', 'text' => 'Just a comment', 'time' => '12:50', 'visible' => FALSE ),
array( 'name' => 'BBB', 'text' => 'Another one', 'time' => '14:10', 'visible' => TRUE ),
);
// Create/updates the comments
update_user_meta( $user->ID, 'recommend_comment', $comments );
// ...
// Load the comments
$comments = get_user_meta( $user->ID, 'recommend_comment', TRUE );
if( !empty( $comment ) ) {
// then you can manipulate them with:
$comments[1]['visible'] = FALSE;
// and update the meta as before
update_user_meta( $user->ID, 'recommend_comment', $comments );
}
This works for me.
//delete_user_meta(get_current_user_id(), 'watchlist');
$new_value = $_GET['id'];
if(empty($new_value)){
echo 'No value';
die();
}
$watchlist = get_user_meta( get_current_user_id(), 'watchlist', true);
if( !empty( $watchlist ) ) {
$check_value = unserialize( $watchlist );
// Check if value exists
if( in_array( $new_value, $check_value ) ){
echo 'Already exists';
}else{
$check_value[] = $new_value;
update_user_meta( get_current_user_id(), 'watchlist', serialize( $check_value ) );
}
}else{
update_user_meta( get_current_user_id(), 'watchlist', serialize( [$new_value] ));
}
print_r(unserialize( $watchlist ));

Not Getting access token from of Gmail Contacts API using Oauth2 while code is in Google App Engine Server

I am using zend-framework2 and google-app-engine server to import all gmail contact list. I am getting authorization code from gmail after login. When hitting curl using this authorization code to get access token, it gives blank array as response.
Also this app is a billed app in google-app-engine. and already set
extension = "curl.so"
in php.ini file. and the credentials which i have given are correct.
// Import Gmail Contacts
$client_id='xxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
$client_secret='xxxxxxxxxxxxxx-xxxxxxxxx';
$redirect_uri='http://xxxxxxxxxxxxxxx.appspot.com/u/invite/gmailCallback';
$max_results = 1000;
if(isset($_GET["code"]) && $_GET["code"] != '') {
$auth_code = $_GET["code"];
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($client_id),
'client_secret'=> urlencode($client_secret),
'redirect_uri'=> urlencode($redirect_uri),
'grant_type'=> urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,FALSE);
$result = curl_exec($curl);
//$info = curl_getinfo($curl);
//echo "<pre>"; print_r($info); die;
curl_close($curl);
$response = json_decode($result);
//echo "<pre>"; var_dump($response); die;
if(isset($response->access_token) && $response->access_token != '') {
$accesstoken = $response->access_token;
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&oauth_token='.$accesstoken;
//$url = 'https://www.google.com/m8/feeds/contacts/default/full?oauth_token='.$accesstoken;
$xmlresponse = $this->curl_file_get_contents($url);
//At times you get Authorization error from Google.
if((strlen(stristr($xmlresponse,'Authorization required'))>0) && (strlen(stristr($xmlresponse,'Error '))>0)) {
//$returnArr['error_msg'] = "Something went wrong to import contacts!!";
echo '<h2>Something went wrong to import contacts !!</h2>';die;
} else {
$xml = new SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
$result = $xml->xpath('//gd:email');
//echo "<pre>"; print_r($result); die;
foreach ($result as $title) {
$emailsArr[] = (string) $title->attributes()->address;
}
}
}
}
Please help.
From previous experience (and this answer) I don't think cURL is enabled on App Engine as C extensions aren't supported.
You'll have to use URL Fetch instead e.g.
$data = ['data' => 'this', 'data2' => 'that'];
$data = http_build_query($data);
$context = [
'http' => [
'method' => 'GET',
'header' => "custom-header: custom-value\r\n" .
"custom-header-two: custom-value-2\r\n",
'content' => $data
]
];
$context = stream_context_create($context);
$result = file_get_contents('http://app.com/path?query=update', false, $context);
App engine supports cURL, but it seems to be buggy. I have the same problem with OAuth authentication via Google. The POST cURL call just returns FALSE without any error message. Here is the official bug report: https://code.google.com/p/googleappengine/issues/detail?id=11985
What you can do now, is to switch to url fetch functions. To make a POST request you can use following function:
protected function _call($url, array $params, $method = 'GET', array $headers = [])
{
$query = http_build_query($params);
$headers_str = '';
if ($headers) {
foreach ($headers as $name => $value) {
$headers_str .= $name . ': ' . $value . "\r\n";
}
}
$headers_str .= "Content-type: "."application/x-www-form-urlencoded"."\r\n";
$options =
array('http'=>
array(
'method' => $method,
'header' => $headers_str,
'content' => $query,
'ignore_errors' => true,
)
);
if ($method === 'POST') {
$options['http']['content'] = $query;
} else if ($query) {
$url .= '?' . $query;
}
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
// check for zipped response and decode it
if (array_search('Content-Encoding: gzip', $http_response_header)) {
$response = gzinflate(substr($response, 10, -8));
}
return $response;
}
Or you can try to use this cURL wrapper library https://github.com/azayarni/purl, which was implemented before GAE provided cURL support. Hope it is helpful:)

Resources