Я хочу передать детали пользовательской информации вместе с идентификатором заказа, но когда я передаю детали данных в woocommerce api. Это дает мне 401 несанкционированный доступ, пожалуйста, помогите мне
В функции WooCommerce.postAsyncЯ уже пытаюсь передать данные, но это не предоставляет мне информацию о пользователе, поэтому, пожалуйста, скажите мне, как я могу исправить мою ошибку здесь.
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, AlertController } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { WoocommerceProvider } from '../../providers/woocommerce/woocommerce';
import { HttpModule } from '@angular/http';
import { HomePage } from '../../pages/home/home';
import { HttpClientModule } from '@angular/common/http';
//import { APIControllers, Constants, utils, constants } from 'authorizenet';
import * as WC from 'woocommerce-api';
/**
* Generated class for the CheckoutPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
@IonicPage({})
@Component({
selector: 'page-checkout',
templateUrl: 'checkout.html',
})
export class CheckoutPage {
WooCommerce: any;
newOrder: any;
order:any;
paymentMethods: any[];
paymentMethod: any;
billing_shipping_same: boolean;
userInfo: any;
constructor(public navCtrl: NavController, public navParams: NavParams,
public alertCtrl: AlertController, public storage: Storage, private WP: W
oocommerceProvider,public http:HttpModule) {
this.order={};
this.newOrder = {};
this.newOrder.billing = {};
this.newOrder.shipping = {};
this.billing_shipping_same = false;
this.paymentMethods = [
/*{ method_id: "bacs", method_title: "Direct Bank Transfer" },
{ method_id: "cheque", method_title: "Cheque Payment" },
{ method_id: "cod", method_title: "Cash on Delivery" },*/
{ method_id: "paypal", method_title: "PayPal" },
{ method_id: "Authorize.Net", method_title: "Authorize.Net" },
{ method_id: "Free", method_title: "Free" }
];
this.WooCommerce = WP.init(true);
this.storage.get("userLoginInfo").then((userLoginInfo) => {
this.userInfo = userLoginInfo;
//console.log(this.userInfo);
let email = userLoginInfo.user_email;
//let id = userLoginInfo.user.id;
let id = 1;
this.WooCommerce.getAsync("customers/"+id).then((data) => {
this.newOrder = JSON.parse(data.body);
})
})
}
setBillingToShipping() {
this.billing_shipping_same = !this.billing_shipping_same;
if (this.billing_shipping_same) {
this.newOrder.shipping = this.newOrder.billing;
}
}
placeOrder() {
let orderItems: any[] = [];
let data: any = {};
let paymentData: any = {};
this.paymentMethods.forEach((element, index) => {
if (element.method_id == this.paymentMethod) {
paymentData = element;
//console.log(paymentData);
}
});
this.WooCommerce.post('order', data, function(err, data, res) {
console.log(res);
console.log(data);
});
data = {
//Fixed a bug here. Updated in accordance with wc/v2 API
payment_method: paymentData.method_id,
payment_method_title: paymentData.method_title,
set_paid: true,
billing: this.newOrder.billing,
shipping: this.newOrder.shipping,
customer_id: this.userInfo.id || '',
line_items: orderItems
};
//console.log(data);
if(paymentData.method_id == "Authorize.Net") { /* If authorize payment gateway */
/*var ApiContracts = require('authorizenet').APIContracts;
var ApiControllers = require('authorizenet').APIControllers;
var utils = require('authorizenet/lib/utils.js');
var constants = require('authorizenet/lib/constants.js');
var merchantAuthenticationType = new ApiContracts.MerchantAuthenticationType();
merchantAuthenticationType.setName('77m58nSPM');
merchantAuthenticationType.setTransactionKey('82Tsk4RcGk5Rr38M');
console.log(merchantAuthenticationType);
var opaqueData = new ApiContracts.OpaqueDataType();
opaqueData.setDataDescriptor('COMMON.ACCEPT.INAPP.PAYMENT');
opaqueData.setDataValue('9471471570959063005001');
console.log(opaqueData);
var payment = new ApiContracts.PaymentType();
payment.setOpaqueData(opaqueData);
console.log(payment);*/
} else if(paymentData.method_id == "paypal") {
this.alertCtrl.create({
title: 'Paypal under maintenance',
subTitle: 'Paypal is currently under maintenance. Please use other Payment gateway option(s).',
buttons: ['OK']
}).present();
} else if(paymentData.method_id == "Free") {
this.storage.get("cart").then((cart) => {
console.log(cart);
/* foreach start */
cart.forEach((element, index) => {
if(element.variation) {
orderItems.push({ product_id: element.product.id, variation_id: element.variation.id, quantity: element.qty });
} else {
orderItems.push({ product_id: element.product.id, quantity: element.qty });
}
});
/* End foreach */
data.line_items = orderItems;
console.log(orderItems);
let orderData: any = {};
//orderData.order = data;
console.log(orderData.order);
console.log(data);
this.WooCommerce.postAsync("orders",data).then((data) => {
console.log(data);
let response = (JSON.parse(data.body));
console.log(response);
this.alertCtrl.create({
title: "Order Placed Successfully",
message: "Your order has been placed successfully. Your order number is " + response.id,
buttons: [{
text: "OK",
handler: () => {
this.navCtrl.push(HomePage);
}
}]
}).present();
})
})
}
}
ionViewDidLoad() {
//console.log('ionViewDidLoad CheckoutPage');
}
}