I’m trying to send data to php from ionic 3 using the http library through post, but when php tries to retrieve it, it can’t find it. Here’s the Typescript file that creates the token that its going to be send to php, and calls the php file on localhost (xampp)
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 this is my php file that tries to obtain the token, but it can't get past the first if, saying that there's nothing on post
<?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("sk_test_KsGJt7QwRWkNodymdTJmnrYr");
$error = '';
$success = '';
try {
if (!isset($_POST['stripetoken'])){
throw new Exception("The Stripe Token was not generated correctly");
}
\Stripe\Charge::create(array(
"amount" => 100,
"currency" => "usd",
"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 send");
}
?>
Anyone with an idea of why it is not working? or any different approach i might try