I’m trying to send data to php using the http library through post, but when php tries to retrieve it, it can’t find it. Here’s the code
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController } from 'ionic-angular';
import { Stripe } from '@ionic-native/stripe';
import { Http, Headers } from '@angular/http';
@IonicPage()
@Component({
selector: 'page-pay',
templateUrl: 'pay.html',
})
export class PayPage {
cardinfo: any = {
number: '4242424242424242',
expMonth: '11',
expYear: '20',
cvc: '110'
}
constructor(public navCtrl: NavController, public navParams: NavParams, public stripe: Stripe, public http: Http,
public toastCtrl : ToastController) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad PagoPage');
}
pay(amount){
this.stripe.setPublishableKey('[mytoken]');
this.stripe.createCardToken(this.cardinfo).then((token) => {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
var body = {
stripetoken: token
};
var myData = JSON.stringify({stripetoken: token});
var url = 'http://192.168.1.2/service/pago.php';
this.http.post(url, body, {headers: headers})
.subscribe( (data) =>{
if(data){
console.log(data);
}
});
})
}
}
And my php server code
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
require_once('./Stripe/init.php');
if ($_POST) {
\Stripe\Stripe::setApiKey("[myprivatekey]");
$error = '';
$success = '';
try {
if (!isset($_POST['stripetoken'])){
throw new Exception("The Stripe Token was not generated correctly");
}
\Stripe\Charge::create(array(
"amount" => 1000,
"currency" => "CRC",
"source" => $_POST['stripetoken'],
"description" => "Charge for daniel.wilson@example.com"
));
$success = 'Your payment was successful.';
echo json_encode($success);
}
catch (Exception $e) {
$error = $e->getMessage();
echo json_encode($error);
}
}else{
echo json_encode("nothing was executed");
}
?>
I keep getting the nothing was executed as response in IONIC