简体   繁体   English

如何在 Angular 2 中集成 Razorpay?

[英]How to Integrate Razorpay in Angular 2?

I'm using angular 2. I've tried below URL's to integrate Razorpay [ https://docs.razorpay.com/docs/checkout-form][1]我正在使用 angular 2。我尝试在 URL 下方集成 Razorpay [ https://docs.razorpay.com/docs/checkout-form][1]

When i follow this URL, i got these errors like In my '.ts' file code当我跟随这个 URL 时,我得到了这些错误,比如在我的 '.ts' 文件代码中

var options = {
    "key": "YOUR_KEY_ID",
    "amount": "2000", // 2000 paise = INR 20
    "name": "Merchant Name",
    "description": "Purchase Description",
    "image": "/your_logo.png",
    "handler": function (response){
        alert(response.razorpay_payment_id);
    },
    "prefill": {
        "name": "Harshil Mathur",
        "email": "harshil@razorpay.com"
    },
    "notes": {
        "address": "Hello World"
    },
    "theme": {
        "color": "#F37254"
    }
};
var rzp1 = new Razorpay(options);

document.getElementById('rzp-button1').onclick = function(e){
    rzp1.open();
    e.preventDefault();
}

ORIGINAL EXCEPTION: Cannot Find name 'Razorpay' of undefined原始例外:找不到未定义的名称“Razorpay”

Ok am a little late to the party , but i was stuck in same problem.好吧,我参加聚会有点晚了,但我遇到了同样的问题。

Razorpay is undefined because its defined on window object, so you need something like window.Razorpay Razorpay是未定义的,因为它定义在 window 对象上,所以你需要像window.Razorpay这样的东西

And as @Pradeep said in comment declare var Razorpay:any , no this won't work either正如@Pradeep 在评论中所说的 declare var Razorpay:any ,不,这也行不通

And @Rajesh Keerthi In '.ts' file we can't access '.js' file so, 'checkout.js' included in html file you can't put script tags in angular 2 components html , because of sanitization ref this并且@Rajesh Keerthi In '.ts' file we can't access '.js' file so, 'checkout.js' included in html file不能将脚本标签放在 angular 2 组件 html 中,因为清理引用了这个

Now coming to solution, how to do it.现在来到解决方案,如何去做。 First put先放

<script src="https://checkout.razorpay.com/v1/checkout.js"></script>

at index.html Now you have Razorpay object on window , but how to access it in .ts file ?index.html现在您在窗口上有 Razorpay 对象,但如何在 .ts 文件中访问它? Follow this link and create a WindowRef service and inject it in provider as article says, then in your MyComponent.ts file put按照此链接创建一个WindowRef服务并将其注入提供者,如文章所述,然后在您的 MyComponent.ts 文件中放入

import { WindowRef } from './WindowRef';
@Component({...})
class MyComponent {

    constructor(private winRef: WindowRef) {
    }
    rzp1:any;
    options = {
       "key": "rzp_test_HTQz79bVMhpN4L",
       "amount": "2000",
       "name": "Merchant Name",
       ....
       .....
    };

    public initPay():void {
       this.rzp1 = new this.winRef.nativeWindow.Razorpay(this.options);
       this.rzp1.open();
    }
}

And MyComponent.html will have而 MyComponent.html 将有

<button id="rzp-button1" (click)="initPay();">Pay</button>

And Voila!!瞧!! you have razorpay integrated你已经集成了razorpay

Even if you are trying out some other payment gateways like paytm or so, this approach will help即使您正在尝试其他一些支付网关,如 paytm 左右,这种方法也会有所帮助

Step 1:第1步:

Declare razorPay at where you imported modules.在您导入模块的位置声明 razorPay。 declare var Razorpay: any;

 import { FormBuilder, FormGroup, Validators, FormArray, FormControl } from '@angular/forms'; declare var Razorpay: any; @Component({ selector: 'app-checkout', templateUrl: './checkout.component.html', styleUrls: ['./checkout.component.css'] })

Step 2:第2步:

Add script file checkout.js to index将脚本文件 checkout.js 添加到索引

Step 3: Add following required function第 3 步:添加以下所需功能

 payNow( ) { var options = { "key": "rzp_test_dveDexCQKoGszl", "amount":((this.buyNow==1)?this.shared.totalAmountWithDisocuntBuyNow:this.shared.totalAmountWithDisocunt)*100, // 2000 paise = INR 20 "name": " MARKET", "description": "Order #", "handler": function (response){ console.log(response); alert(response.razorpay_payment_id); }, "prefill": { "name": this.shared.orderDetails.billing_firstname + " " + this.shared.orderDetails.billing_lastname, "email": this.shared.customerData.customers_email_address, "contact": this.shared.customerData.customers_telephone, }, "notes": { }, "theme": { "color": "blue" } }; var rzp1 = new Razorpay(options); rzp1.open(); // body... } paid() {alert();}

Add anywhere in your component html file:在组件 html 文件中的任何位置添加:

<button  (click)="initPay()">Pay</button>

and update your your component.ts file like so:并像这样更新你的 component.ts 文件:

import { Component } from '@angular/core';

declare var Razorpay: any; 
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
    title = 'client';
    options = {
        "key": 'your app id', // Enter the Key ID generated from the Dashboard
        "amount": "50000", // Amount is in currency subunits. Default currency is INR. Hence, 50000 refers to 50000 paise or INR 500.
        "currency": "INR",
        "name": "Acme Corp",
        "description": "A Wild Sheep Chase is the third novel by Japanese author  Haruki Murakami",
        "image": "https://example.com/your_logo",
        "handler": function (response) {
            alert(response.razorpay_payment_id);
        },
        "prefill": {
            "name": "amin uddin",
            "email": "amidenf9701@gmail.com",
            "contact": "7992239847"
        },
        "notes": {
            "address": "note value"
        },
        "theme": {
            "color": "#F37254"
        }
    };
    initPay() {
        var rzp1 = new Razorpay(this.options);
        rzp1.open();
        console.log("works");
    }
}

declare Razorpay inside polyfills.ts file声明Razorpaypolyfills.ts文件

// polyfills.ts file
declare global {
    interface Window {
        Razorpay: any;
    }
 }

MyComponent.ts file MyComponent.ts 文件

@Component({...})
class MyComponent {

constructor() {
}
rzp1:any;
options = {
   "key": "rzp_test_HTQz79bVMhpN4L",
   "amount": "2000",
   "name": "Merchant Name",
   ....
   .....
};

public initPay():void {
   this.rzp1 = new window.Razorpay(this.options);
   this.rzp1.open();
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM