File: /var/www/vhost/disk-apps/pwa.sports-crowd.com/src/app/services/gateway-payment.service.ts
import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { StorageService } from './storage.service';
import { ApiService } from "./api.service";
@Injectable({
providedIn: "root",
})
export class GatewayPaymentService {
paymentString: string;
post_url: any;
merchant_id: string;
account_id: string;
code: string;
amount: string;
currency: string;
buyerEmail: string;
response_url: string;
confirmation_url: string;
signature: string;
constructor(public http: HttpClient, public api: ApiService, private storage: StorageService) { }
goToGateway(order) {
this.getGatewayInfo(order["id"]).then((res: any) => {
if (res.gw.is_productive) {
this.post_url = res.gw.gw_url_prd;
} else {
this.post_url = res.gw.gw_url_sandbox;
}
this.merchant_id = res.gw.merchant_id;
this.account_id = res.gw.account_id;
this.code = order.code;
this.amount = order.total_price;
this.currency = res.gw.currency;
this.buyerEmail = res.u.email;
this.response_url = res.gw.page_confirm_user;
this.confirmation_url = res.gw.page_response_for_gateway;
this.signature = res.signature;
this.paymentString = `
<html>
<body>
<form action="${this.post_url}" method="post" id="payu_form">
<input name="merchantId" type="hidden" value="${this.merchant_id}" >
<input name="accountId" type="hidden" value="${this.account_id}" >
<input name="description" type="hidden" value="Pago pedido codigo: ${this.code}" >
<input name="referenceCode" type="hidden" value="${this.code}" >
<input name="amount" type="hidden" value="${this.amount}" >
<input name="tax" type="hidden" value="0" >
<input name="taxReturnBase" type="hidden" value="0" >
<input name="currency" type="hidden" value="${this.currency}" >
<input name="signature" type="hidden" value="${this.signature}" >
<input name="test" type="hidden" value="1" >
<input name="buyerEmail" type="hidden" value="${this.buyerEmail}" >
<input name="responseUrl" type="hidden" value="${this.response_url}" >
<input name="confirmationUrl" type="hidden" value="${this.confirmation_url}" >
<input name="Submit" type="submit" value="Enviar" >
<button type="submit" value="submit" #submitBtn></button>
</form>
<script type="text/javascript">
document.getElementById("payu_form").submit();
</script>
</body>
</html>`;
this.paymentString = "data:text/html;base64," + btoa(this.paymentString);
});
}
getGatewayInfo(id) {
return new Promise((resolve, reject) => {
this.storage.get("token").then(async (token) => {
if (token) {
let seq = await this.api.get("cart/gw/" + id, token.access_token);
seq.subscribe(
(res: any) => {
if (res.status == "success") {
resolve(res);
}
},
(err) => {
reject();
console.error("Error provider gateway-payment getGatewayInfo", err);
}
);
}
});
});
}
paymentSuccess() { }
paymentFailure() { }
getActivatedPaymentGateways(type: string, gateway?: number) {
return this.api.getFree("paymentGateway/getActived/" + type + "/" + gateway);
}
}