added Stripe service + implementation of stripe service in home component
parent
373177e8cf
commit
7d201400ae
@ -0,0 +1,34 @@
|
|||||||
|
import { Component } from '@angular/core';
|
||||||
|
import { StripeService } from 'src/app/services/stripe.service';
|
||||||
|
import { environment } from 'config';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-home',
|
||||||
|
templateUrl: './home.component.html',
|
||||||
|
styleUrls: ['./home.component.scss']
|
||||||
|
})
|
||||||
|
|
||||||
|
export class HomeComponent {
|
||||||
|
constructor(private stripeService: StripeService) { }
|
||||||
|
|
||||||
|
payment(): void {
|
||||||
|
const priceId = environment.PriceIdKey; // Replace with your Stripe price object ID
|
||||||
|
const quantity = '1'; // Replace with your desired quantity
|
||||||
|
|
||||||
|
this.stripeService.createPaymentLink(priceId, quantity)
|
||||||
|
.subscribe(
|
||||||
|
(response) => {
|
||||||
|
console.log('Payment link created successfully:', response);
|
||||||
|
// You can handle the payment link here
|
||||||
|
// Open the URL in a new window
|
||||||
|
if (response.url) {
|
||||||
|
window.open(response.url, '_blank');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
(error) => {
|
||||||
|
console.error('Error creating payment link:', error);
|
||||||
|
// Handle the error here
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +0,0 @@
|
|||||||
import { Component } from '@angular/core';
|
|
||||||
|
|
||||||
@Component({
|
|
||||||
selector: 'app-home',
|
|
||||||
templateUrl: './home.component.html',
|
|
||||||
styleUrls: ['./home.component.scss']
|
|
||||||
})
|
|
||||||
export class HomeComponent {
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,16 @@
|
|||||||
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { StripeService } from './stripe.service';
|
||||||
|
|
||||||
|
describe('StripeService', () => {
|
||||||
|
let service: StripeService;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({});
|
||||||
|
service = TestBed.inject(StripeService);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be created', () => {
|
||||||
|
expect(service).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
@ -0,0 +1,31 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
import { environment } from 'config';
|
||||||
|
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
|
export class StripeService {
|
||||||
|
private apiUrl = 'https://api.stripe.com/v1';
|
||||||
|
private apiKey = environment.stripeApiKey; // Replace with your Stripe secret key
|
||||||
|
|
||||||
|
constructor(private http: HttpClient) { }
|
||||||
|
|
||||||
|
private getHeaders() {
|
||||||
|
const headers = new HttpHeaders()
|
||||||
|
.set('Authorization', `Bearer ${this.apiKey}`)
|
||||||
|
.set('Content-Type', 'application/x-www-form-urlencoded');
|
||||||
|
return { headers };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create payment link
|
||||||
|
createPaymentLink(priceId: string, quantity: string): Observable<any> {
|
||||||
|
const body = new URLSearchParams();
|
||||||
|
body.set('line_items[0][price]', priceId); // Add line_items parameter
|
||||||
|
body.set('line_items[0][quantity]', quantity); // Add line_items parameter
|
||||||
|
|
||||||
|
return this.http.post(`${this.apiUrl}/payment_links`, body.toString(), this.getHeaders());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue