使用Apple / Android支付Strular在AngularJs网站上

时间:2017-08-18 13:47:19

标签: angularjs stripe-payments android-pay applepay

我正在尝试在我的网站上设置条带以进行信用卡付款。 我成功地设法让条纹薪酬像这样工作:

function directive() {
    return {
        restrict: 'A',
        controller: 'StripePayController',
        controllerAs: 'controller',
        bindToController: true,
        templateUrl: 'app/directives/stripe-pay/stripe-pay.html'
    };
};

function controller(stripePayService) {
    var self = this;

    // Method binding
    self.checkout = stripePayService.checkout;

    init();

    //////////////////////////////////////////////////

    function init() {
        stripePayService.configure();
    };
};


function service($rootScope, $localStorage, $q, $document, paymentService) {
    var handler;
    return {
        configure: configure,
        checkout: checkout
    };

    //////////////////////////////////////////////////

    function configure() {

        // Load our script
        loadScript().then(function () {                
            handler = StripeCheckout.configure({
                key: 'my-code',
                locale: 'auto',
                token: function (token) {
                    token.transaction = transaction();
                    paymentService.create({ token: token.id }).then(function (response) {
                        var authorizationData = $rootScope.authorizationData;
                        authorizationData.stripeCardId = response.stripeCardId;
                        authorizationData.stripeCustomerId = response.stripeCustomerId;
                        authorizationData.stripeSubscriptionId = response.stripeSubscriptionId;
                        $localStorage.set('authorizationData', angular.toJson(authorizationData));
                    });
                }
            });
        });
    };

    function checkout(e) {
        handler.open(transaction());
        e.preventDefault();
    };

    function transaction() {
        var email = $rootScope.authorizationData.userName;
        return {
            name: 'acme Ltd',
            description: 'Results',
            email: email,
            zipCode: false,
            currency: 'gbp',
            amount: 199
        };
    };

    function loadScript() {

        // Create our script
        var deferred = $q.defer();
        var doc = $document[0];
        var script = doc.createElement('script');

        // Set the url
        script.src = 'https://checkout.stripe.com/checkout.js';

        // Bind our methods
        script.onload = function () {
            deferred.resolve();
        };

        // Bind our methods
        script.onreadystatechange = function () {
            var rs = this.readyState;
            if (rs === 'loaded' || rs === 'complete')
                deferred.resolve();
        };

        // Bind our methods
        script.onerror = function () {
            deferred.reject(new Error('Unable to load checkout.js'));
        };

        // Get the head
        var container = doc.getElementsByTagName('head')[0];

        // Append our script to our page
        container.appendChild(script);

        // Return our promise
        return deferred.promise;
    };
};

这似乎工作正常。 但是设置应用薪酬似乎要复杂得多。有没有人有任何之前使用过的angularjs代码可以帮助我?

1 个答案:

答案 0 :(得分:0)

Stripe在他们的文档中解释如何执行此操作非常出色。他们使用vanilla javascript,但你应该能够很容易地将它转换为Angular。

https://stripe.com/docs/apple-pay/web