php Stripe Payments - WordCamp ATL 2015

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Stripe Payments - WordCamp ATL 2015相关的知识,希望对你有一定的参考价值。

<?php

function stripe_process_webhook( $request ) {
    $body = @file_get_contents('php://input');
    $stripe_payload = json_decode( $body );
	
    if ( !empty( $stripe_payload->id ) ) {
	    try {
		    $secret_key = 'secret-key';
		    Stripe::setApiKey( $secret_key );
			$stripe_event = Stripe_Event::retrieve( $stripe_payload->id );
	
			$stripe_object = $stripe_event->data->object;
	
			//https://stripe.com/docs/api#event_types
			switch( $stripe_event->type ) {
	
				case 'charge.succeeded' :
					//Record the transaction for the customer...
					break;
				case 'charge.failed' :
					//Record the failure for the customer...
					break;
				case 'charge.refunded' :
					//Record the refund for the customer...
					break;
				case 'charge.dispute.created' :
				case 'charge.dispute.updated' :
				case 'charge.dispute.closed' :
					//Record the dispute for the customer...
					break;
				case 'customer.deleted' :
					//Delete the Stripe ID from the customer...
					break;				
	
			}
		}
		catch( Exception $e ) {
			error_log( $e->getMessage() );
		}
    }
}
<?php

function stripe_process_transaction( $status, $transaction_object ) {
	// Verify nonce
	if ( ! empty( $_REQUEST['_stripe_nonce'] ) && ! wp_verify_nonce( $_REQUEST['_stripe_nonce'], 'stripe-checkout' ) ) {
		error_log( 'Transaction Failed, unable to verify security token.' );
		return false;
	}

	// Make sure we have the correct $_POST argument
	if ( ! empty( $_POST['stripeToken'] ) ) {
		try {

			$secret_key = 'secret-key';
			$customer_email = '';
			Stripe::setApiKey( $secret_key );

			// Set stripe token
			$token = $_POST['stripeToken'];

			$customer_array = array(
				'email' => $customer_email,
				'card'  => $token,
			);
			$stripe_customer = Stripe_Customer::create( $customer_array );

			// Now that we have a valid Customer ID, charge them!
			$args = apply_filters( 'it_exchange_stripe_addon_charge_args', array(
				'customer'    => $stripe_customer->id,
				'amount'      => number_format( cart_total(), 2, '', '' ), //no decimal!
				'currency'    => 'usd',
				'description' => $description,
			) );
			$charge = Stripe_Charge::create( $args );
			return $charge->id;

		}
		catch ( Exception $e ) {
			error_log( $e->getMessage() );
			return false;
		}
	}
	return false;

}

function cart_total() {
	return '50.00';
}
<?php

// Custom forms: https://stripe.com/docs/tutorials/forms

function stripe_payment_button() {
	$payment_image = false;

    $publishable_key = 'key';
	$transaction_return_page = get_permalink( 1 ); //Whatever page ID you're using for your transaction return page
	$description = 'S-Mart';
	$image = 'http://domain.tld/assets/image.png'; //If you want a custom image to appear on the Stripe popup

	$payment_form  = '<form action="' . $transaction_return_page .'" method="POST">';
	$payment_form .= '<input type="hidden" name="transaction-method" value="stripe" />';
	$payment_form .= wp_nonce_field( 'stripe-checkout', '_stripe_nonce', true, false );
	$payment_form .= '<script';
	$payment_form .= ' 	src="https://checkout.stripe.com/checkout.js" class="stripe-button"';
	$payment_form .= ' 	data-key="' . esc_js( $publishable_key ) . '"';
	$payment_form .= ' 	data-amount="' . esc_js( number_format( cart_total(), 2, '', '' ) ) . '"';
	$payment_form .= ' 	data-name="' . esc_js( strip_tags( $company_name ) ) . '"';
	$payment_form .= ' 	data-description="' . esc_js( strip_tags( $description ) ) . '"';
	$payment_form .= '	data-image="' . esc_js( strip_tags( $image ) ) . '">';
	$payment_form .= '</script>';
	$payment_form .= '</form>';

    return $payment_form;
}

以上是关于php Stripe Payments - WordCamp ATL 2015的主要内容,如果未能解决你的问题,请参考以下文章

在 stripe.NET-payments 基本示例(Xamarin)中找不到类型

PayPal Adaptive Payments 和 Stripe 的更便宜的替代品

Stripe Payments - 将现有卡添加到客户

使用 Firebase 'Run Subscription Payments with Stripe' 扩展购买多个订阅

通过 Braintree + Braintree Webhooks 从 PayPal Payments Pro(w/h 定期计费)+ IPN 迁移到 PayPal Payments

在 WooCommerce PayPal Payments 处理交易时添加加载图标